ความรู้พื้นฐานเกี่ยวกับ C#

Download Report

Transcript ความรู้พื้นฐานเกี่ยวกับ C#

ความรูพ
้ น
ื้ ฐานเกีย
่ วก ับ C#
C# is a simple, modern, object oriented, and type-safe programming
language derived from C and C++. It will immediately be familiar to C
and C++ programmers. C# aims to combine the high productivity of
Visual Basic and the raw power of C++
From Microsoft C# Language Specification, Microsoft Corporation
Visual C# .NET

เครือ
่ งมือพัฒนาโปรแกรมด ้วยภาษา C#
2
ว ัตถุประสงค์หล ักของภาษา C#

ิ ธิภาพการทางาน
เป็ นภาษาใหม่ทม
ี่ ป
ี ระสท
เทียบเท่า หรือเหนือกว่า C++ แต่ไม่ยงุ่ ยากและ
ั ซอนเท่
้
้
ซบ
า โดยสามารถใชงานได
้ง่ายเหมือน
ภาษา Visual Basic ทาให ้สามารถพัฒนาแอป
ั ในระดับสูงได ้ดียงิ่ กว่าการใชภาษา
้
พลิเคชน
Visual Basic
3
คาอธิบาย (Comment)


// comment
/* multiline
comment */
4
บล็อก (Block)



{…}
ิ้ สุดการทางานแต่ละสว่ น
กาหนดจุดเริม
่ ต ้นและสน
้
ภายในบล็อก สามารถมีบล็อกย่อยๆ ซอนลงไปอี
ก
5
้ ล็อก
ต ัวอย่างการใชบ
1
2
3
4
5
6
Class myClass {
if (…) {
for (…) {
…
} // end for block
} // end if block
7
else {
8
…
9
} // end else block
10
…
11 } // end class block





Class block
If block
If…then block
Else block
For block
6
ิ้ สุดคาสง่ ั (;)
เครือ
่ งหมายสน

ิ้ สุดแต่ละคาสงั่
; ตัวแสดงจุดสน
ต ัวอย่าง 1
x = 10;
y = 2 + x;

ต ัวอย่าง 2
x = 10; y = 2 + x;
จบบล็อก ไม่ต ้องใส่ ;
7
Keywords
abstract
case
continue
else
finally
if
is
object
protected
sealed
struct
typeof
using
as
catch
decimal
enum
fixed
implicit
lock
operator
public
short
switch
uint
virtual
base
char
default
event
float
in
long
out
readonly
sizeof
this
ulong
void
bool
checked
delegate
explicit
for
int
namespace
override
ref
stackalloc
throw
unchecked
volatile
break
class
do
extern
foreach
interface
new
params
return
static
true
unsafe
while
byte
const
double
false
goto
internal
null
private
sbyte
string
try
ushort
8
ื่ ต ัวแปร
หล ักการตงช
ั้ อ



ขึน
้ ต ้นด ้วยตัวอักษร, underscore (_) หรือ @
้ ัวเลข หรืออ ักขระเป็นต ัวเริม
ห้ามใชต
่ ต้น
รูปแบบตัวอักษรพิมพ์ตา่ งกัน ถือเป็ นคนละตัว
่ myvar, myVar, MYVAR
(case sensitive) เชน
ถือเป็ นคนละตัว
9
การประกาศต ัวแปร (Variable)
ื่ ต ัวแปร;
วิธ1
ี [ชนิดของข้อมูล] ชอ
ื่ ต ัวแปร = [ค่าเริม
วิธ2
ี [ชนิดของข้อมูล] ชอ
่ ต้นของต ัวแปร];



int x;
int y, z;int Y, Z = 3;
Double d = 10.99;
String D = "Hello World",str;
หมายเหตุ:
ไม่สามารถประกาศตัวแปรซ้าภายในบล็อก {…} เดียวกัน
10
การประกาศค่าคงที่ (Constant)
ื่ ต ัวแปร = ค่าคงที;่
const [ชนิดของข้อมูล] ชอ


const int x = 1;
const double pi = 3.14;
หมายเหตุ:
ไม่สามารถเปลีย
่ นแปลงค่าคงทีไ่ ด ้
11
ชนิดของข้อมูล





Numeric (เลขจานวนเต็ม และเลขทศนิยม)
Char (ตัวอักษร)
String (ข ้อความ)
Boolean (บูลน
ี )
Object
12
เลขจานวนเต็ม
Data type
ขนาด
ค่าของข้อมูล
sbyte (System.SByte)
1 byte
-128 ถึง 127
short (System.Int16)
2 bytes
-32,768 ถึง 32,767
int (System.Int32)
4 bytes
-2,147,483,648 ถึง
2,147,483,647
long (System.Int64)
8 bytes
-263 ถึง (263 – 1)
13
เลขจานวนเต็มบวก
Data type
ขนาด
ค่าของข้อมูล
byte (System.Byte)
1 byte
0 ถึง 255
ushort (System.UInt16)
2 bytes 0 ถึง 65,535
uint (System.UInt32)
4 bytes 0 ถึง 4,294,967,295
ulong (System.UInt64)
8 bytes 0 ถึง 264 -1
14
เลขทศนิยม
Data type
ขนาด
float (System.Single)
4 bytes
ค่าของข้อมูล
±1.5 x 10-45 ถึง ±3.4 x 1038
ความแม่นยาของทศนิยมอยูท
่ ี่ 7 ตาแหน่ง
double
(System.Double)
8 bytes
±5.0 x 10-324 ถึง ±1.7 x 10308
ความแม่นยาของทศนิยมอยูท
่ ี่ 15 ตาแหน่ง, มีชว่ งของข ้อมูลกว ้างทีส
่ ด
ุ
decimal
(System.Decimal)
16 bytes ±1.0 x 10-28 ถึง ±7.9 x 1028
ความแม่นยาของทศนิยมอยูท
่ ี่ 28 ตาแหน่ง, ชว่ งข ้อมูลแคบกว่า double
แต่ให ้ค่าละเอียดมากในสว่ นของทศนิยม เหมาะกับการคานวณเรือ
่ งเงิน
15
ประเภทข้อมูลทีไ่ ม่ใช่ Numeric
Data type
char
(System.Char)
ขนาด
ค่าของข้อมูล
2 bytes ตัวอักษรแบบ Unicode มี
เครือ
่ งหมาย ' (single quote)
่ 'A', '1'
คร่อมตัวอักษร เชน
string
(System.String)
ไม่
ตัวอักษรแบบ Unicode หลายตัว
แน่นอน มารวมกัน มีเครือ
่ งหมาย "
(double quote) คร่อม
่ "Hello"
เชน
bool
(System.Boolean)
1 bit
มีคา่ ทีเ่ ป็ นไปได ้ 2 ค่า คือ
true และ false
16
ั
สญล
ักษณ์พเิ ศษ
char
Meaning
Value
char
Meaning
Value
\’
Single quote
0x0027
\f
Form feed
0x000C
\”
Double quote
0x0022
\n
New line
0x000A
\\
Backslash
0x005C
\r
Carriage return
0x000D
\0
Null
0x0000
\t
Horizontal tab
0x0009
\a
Alert
0x0007
\v
Vertical tab
0x000B
\b
Backspace
0x0008
17
คาสง่ ั แสดงผล
วิธ1
ี Console.Write (ข้อความ);
วิธ2
ี Console.WriteLine (ข้อความ);
Console.Write ("Hello");
Console.WriteLine ("Hello World");
string str = "Hello";
Console.Write (str + " World");
18
คาสง่ ั ร ับข้อมูล
วิธ1
ี Console.Read (); หรือ
Console.ReadLine ();
วิธ2
ี ต ัวแปร = Console.Read (); หรือ
ต ัวแปร = Console.ReadLine ();
Console.Write ("What is your name? : ");
string name = Console.ReadLine ( ); // Laksamee
Console.Write ("My name is " + name);
Console.Read ( );
19
operator

Arithmetic Operators





A = 2+3;
B = 4 % 3;
X++; ++X; X--; --X;
Y += 3;

Message = "Hello "+Name

If (A=5 && B>6) {…}


String Operators


+ - * / %
++ -= += -= *= /= %=
+
Boolean Operators


< <= > >= == !=
! && || ^
20
Conditional Operator
(เงือ
่ นไข) ? (ค่าเมือ
่ เงือ
่ นไขเป็นจริง) : (ค่าเมือ
่ เงือ
่ นไขเป็นเท็จ)


้
เป็ นวิธล
ี ัดทีใ่ ชในการเปรี
ยบเทียบ
n = 2;
int a = (n = = 0) ? 1 : n;
// a = ___
21
ลาด ับความสาค ัญของ Operators
ลาด ับ
ประเภท
Operator
1
Primary
( ), x++, x--
2
Unary
+x, -x, ++x, --x, !, ~, Cast,
value at address: *, address of value: &
3
Multiplicative
*, /, %
4
Additive
+, -
5
Shift
shift bits left: <<, shift bits right: >>
6
Relational
<, <=, >, >=, is, as
7
Equality
= =, !=
8
Logical bitwise
&, |, ^
9
Logical Boolean &&, ||, ?:
10
Assignment
=, +=, =,*=, /=, %=, <<=, >>=, &=, ^=, |=
22
การแปลงประเภทข้อมูล

Implicit Conversion
เป็ นการแปลงข ้อมูลทีเ่ กิดขึน
้ เองโดยอัตโนมัต ิ จาก
ข ้อมูลทีม
่ ข
ี นาดเล็กกว่าไปเป็ นขนาดทีใ่ หญ่กว่า
่ จาก int ไปเป็ น double
เชน

int a = 123;
long b = a;
23
การแปลงประเภทข้อมูล (ต่อ)

Explicit Conversion
(หรือ Cast)
เป็ นการแปลงข ้อมูลตามทีผ
่ ู ้พัฒนาต ้องการ
่ จาก double ไปเป็ น int
เชน

int x = 123;
short z = (short) x;
short y = short.Parse(X);
float p = 7.5F;

24
การแปลงประเภทข้อมูล Numeric
ต ัวอ ักษร
U
L
UL
F or f
D or d
M or m
แทนข้อมูล
uint or ulong
long or ulong
ulong
float
double
decimal




float n1 = 7.5F;
double n2 = 7.5;
double n3 = 7.5D;
decimal n4 = 7.5M;
25
การแปลงประเภทข้อมูลใดๆ
[type ใหม่][ต ัวแปร] = Convert.To [type ใหม่] ([ค่าเดิม])



int x = Convert.ToInt32 ("123");
// x = 123
double y = Convert.ToDouble (x);
// y = (double) 123, x = (int) 123
string s = Convert.ToString (x);
// s = "123", x = (int) 123
26
การกาหนดเงือ
่ นไข



Selection statements :
if และ switch…case
Iteration statements :
while, do…while และ for
Jump statements :
break, continue, goto, return และ throw
27
if-else statement
if (condition) {
…
}
if (condition) {
…
}
else {
…
}
ต ัวอย่าง
bool isEven (int x) {
if (x%2 ==0)
return true;
else
return false;
}
28
else if & Nested if statement
if (condition1) {
if (condition1) {
ต ัวอย่าง
…
…
char Result; if (condition2) {
}
…
if (Score > 80)
else if (condition2) {
if (condition3) {
Result = ‘A’;
…
…
else if (Score > }
50)
}
}
Result
=
‘B’;
else {
}
else
…
Result = ‘F’;
}
29
แบบฝึ กห ัด if statement (1)

จงเขียนโปรแกรมสาหรับคานวณเกรด
โดยรับคะแนนจากผู ้ใช ้ โดยกาหนดว่า
Grade
A
Score
81 – 100
B
C
D
F
70 – 80
60 – 69
50 – 59
0 – 49
เฉลย
เฉลย
(เรียก method)
30
แบบฝึ กห ัด if statement (2)

ค่าบริการน้ าประปาแห่งหนึง่
มีอต
ั ราดังตาราง
หน่วยที่
ราคา / หน่วย
(บาท)
จงเขียนโปรแกรมให ้
สามารถทาการคานวณค่าใช ้
บริการน้ าประปาของบริษัท
แห่งนี้
10
หน่วยแรก
11 - 20
21 - 30
10
> 30
20
ตัวอย่าง
ถ ้าใชน้ ้ าประปาทัง้ หมด 34 หน่วย
้ การทัง้ หมด 450 บาท
ต ้องชาระค่าใชบริ
12
15
เฉลย
31
แบบฝึ กห ัด if statement (3)

จงเขียนโปรแกรมสาหรับพิจารณาเงินเดือนจาก
ประสบการณ์ทางานโดยให ้ผู ้ใชป้้ อนประสบการณ์
ทางาน โดยกาหนดว่า



ถ ้าประสบการณ์ 5 ปี ขน
ึ้ ไป ได ้รับเงินเดือน 20,000 บาท
ถ ้าประสบการณ์ 2 ปี ขน
ึ้ ไป ได ้รับเงินเดือน 15,000 บาท
่ นัน
มิเชน
้ ได ้รับเงินเดือน 12,000 บาท
เฉลย
32
แบบฝึ กห ัด if statement (4)

จงเขียนโปรแกรมแสดงผลยอดเงินคงเหลือในบัญช ี
เงินฝาก ซงึ่ ฝากมาแล ้ว 1 ปี โดยให ้ผู ้ใชป้้ อนเงินต ้น
โดยกาหนดอัตราดอกเบีย
้ ดังนี้



เงินต ้น 100,000 บาทขึน
้ ไป อัตราดอกเบีย
้ 3%
เงินต ้น 50,000 บาทขึน
้ ไป อัตราดอกเบีย
้ 2%
เงินต ้น ตา่ กว่า 50,000 บาท อัตราดอกเบีย
้ 1%
เฉลย
(เรียก method)
33
switch statement
switch (expression) {
case value1 :
statement1;
break;
case value2 :
statement2;
break;
case valueN :
statementN;
break;
default : statement;
break;
}
expression: ตัวเลข or string
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
14.
15.
string str, color;
Console.Write("Enter day of week:");
str = Console.ReadLine();
switch (str) {
case "Sunday" : color = "red";
break;
case “monday"
"Monday" : color="yellow";
case "Monday" : break;
color="yellow";
break;
...
...
default :
color="invalid";
default :
color="invalid";
break;
break;
}
}Console.WriteLine("color of " +
Console.WriteLine("color
str.ToUpper() + " is "of+" +
str.ToUpper()
+ " is " +
color.ToUpper());
color.ToUpper());
Console.Read
();
Console.Read ();
34
แบบฝึ กห ัด switch statement (1)

จงเขียนโปรแกรมแสดงเมนูการทางาน
ดังหน ้าจอ



เลือก 1 ให ้แสดงข ้อความ "Sub Menu 1 : ADD"
เลือก 2 ให ้แสดงข ้อความ "Sub Menu 2 : SUBTRACT"
เลือก 3 ให ้แสดงข ้อความ "Sub Menu 3 : EXIT"
เฉลย
35
for statement
for (counter = first value; counter condition; adjust counter value) {
…
}
for (…) {
…
for (…) {
…
for (…) {
…
}}}
36
ต ัวอย่าง for statement (1)
1.
2.
3.
4.
5.
6.
int i, sum;
for (i=1; i<5; i++){
sum += i;
}
Console.Write(sum+" "+i);
// sum=_____
i=_____
1.
2.
3.
4.
5.
6.
int sum;
for (int i=1; i<5; i++){
sum += i;
}
Console.Write(sum+" "+i);
// sum=_____
i=_____
37
ต ัวอย่าง for statement (2)
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
int num; string s;
Console.Write ("Please Enter number : ");
s=Console.ReadLine( );
num=int.Parse(s);
for (int i=1; i<=num; i++){
for (int j=1;j<=i;j++){
Console.Write(j+" ");
} // end for j
Console.WriteLine(" ");
} // end for i
Console.Read( );
38
while statement
while (condition) {
…
}
ต ัวอย่าง
1. int Result = 0; int i = 0;
2. while (i < 2) {
3.
i += 1;
4.
Result += i;
5. } // end while
6. Console.Write (Result + " " + i); // Result=____ i=____
39
do-while statement
do {
…
} while (condition);
ต ัวอย่าง
while (condition) {
1. int Result = 0; int i = 0;
…
2. do {
}
3.
i += 1;
4.
Result += i;
5. } while (i < 2);
6. Console.Write (Result + " " + i); // Result=____ i=____
40
แบบฝึ กห ัด for & while statement (1)

จงเขียนโปรแกรมเพือ
่ แสดงผลลัพธ์ดงั นี้
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
เฉลย
(for statement)
เฉลย
(while statement)
41
แบบฝึ กห ัด for & while statement (2)

จงเขียนโปรแกรมนับว่าหากต ้องการเพิม
่ ค่า
จาก 3 จนมากกว่าหรือเท่ากับ 12 จะต ้อง
บวกด ้วย 2 ทัง้ หมดกีร่ อบ
เฉลย
(for statement)
เฉลย
(while statement)
42
แบบฝึ กห ัด for & while statement (3)

จงเขียนโปรแกรมนับว่าหากต ้องการลดค่า
จาก 100 จนน ้อยกว่าหรือเท่ากับ 3 จะต ้อง
หารด ้วย 4 ทัง้ หมดกีร่ อบ
เฉลย
(for statement)
เฉลย
(while statement)
43
แบบฝึ กห ัด for & while statement (4)

พีม
่ อสมีแฟนคลับ 2,857 คน โดยแต่ละปี จะ
มีแฟนคลับเพิม
่ ขึน
้ ปี ละ 8%
้
จงเขียนโปรแกรมคานวณว่าจะใชเวลากี
ป
่ ี พี่
มอสจึงจะมีแฟนคลับมากกว่า 5,000 คน
เฉลย
(for statement)
เฉลย
(while statement)
44
แบบฝึ กห ัด for & while statement (5)

้ ้ อนต ัวเลขจานวนเต็ม 1
จงเขียนโปรแกรมให้ผใู ้ ชป
จานวน แล้วหาผลล ัพธ์สต
ู รคูณ โดยแสดงผลล ัพธ์
้ ้ อนเลข 5)
เป็นตารางด ังนี้ (สมมติผใู ้ ชป
5
5
…
5
*
*
1
2
=
=
*
12
=
เฉลย
(for statement)
5
10
…
60
เฉลย
(while statement)
45
break
break; // คาสง่ ั ให้ออกจากลูป
โดยหยุดการตรวจสอบเงือ
่ นไขถ ัดไป
1.
2.
3.
4.
5.
6.
7.
int x=0, i=0;
while (i<15) {
x++;
if ((x%5)==0) { break; }
Console.WriteLine("i = " + i + ", x = " +x);
i++;
} // end while
8.
46
continue
continue; // คาสง่ ั ให้วนลูปถ ัดไป
โดยไม่ทาคาสง่ ั ทีเ่ หลือในลูปนน
ั้
1.
2.
3.
4.
5.
6.
7.
int x=0, i=0;
while (i<15) {
x++;
if ((x%5)==0) { continue; }
Console.WriteLine
("i=" + i + ", x=" +x);
i++;
} // end while
8.
47
goto
วิธ ี 1 goto statement-label;
วิธ ี 2 goto case-statement;
วิธ ี 3 goto default;

คาสงั่ ให ้โปรแกรมกระโดดไปยังตาแหน่งทีต
่ ้องการ
48
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
ต ัวอย่าง : goto
string s;
enterTitle:
while ((s=Console.ReadLine()) != "x") {
switch (s) {
case "dr" : Console.Write("INVALID. ");
goto default;
case "girl" : goto case "mrs";
case "boy" :
case "mr" : Console.WriteLine("You are male.");
break;
case "miss": goto case "mrs";
case "ms" :
case "mrs" : Console.WriteLine("You are female.");
break;
default : Console.Write("Please enter your title\n");
goto enterTitle;
} // end switch
} // end while
Console.WriteLine("Exit Program");
49
return
return expression;



คาสงั่ ออกจาก method พร ้อมการสง่ ค่า
ั พันธ์กบ
ค่าทีส
่ ง่ กลับ สม
ั ประเภทข ้อมูลทีป
่ ระกาศ
int CalcX (int a) {
int x = a * 100;
return x;
}
50
Method
[modifier] void [name]([param1]…) {
…
}
void sayHello ( ) {
Console.Write("Hello");
}
1.
2.
void main ( ) (string[ ] args){
sayHello ( );
// Hello
sayHello ("Peter"); // Hello Peter
3.
4.
}
void sayHello (string name) {
Console.Write("Hello " + name);
}
51
Method
[modifier][type][name]([param1]…) {
…
return [value];
}
int add ( ) {
return 2 + 3;
}
int add (int x, int y) {
return x + y;
}
void main ( ) (string[ ] args) {
int a = add ( );
// a = _____
int b = add (3,5);
// b = _____
double c = add (3,5); // c = _____
string d = add ( );
// d = _____
}
52
แบบฝึ กห ัด Method (1)

จงเขียนโปรแกรมให ้ผู ้ใชป้้ อนตัวเลขจานวนเต็ม
1 จานวน แล ้วหาว่าค่าทีร่ ับนัน
้ เป็ นเลขคูห
่ รือ
่
เลขคี่ เชน
รับ 5 -> เลขคี่
 รับ 8 -> เลขคู่

เฉลย
(ใน main)
เฉลย
(เรียก method)
53