งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

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

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "ความรู้พื้นฐานเกี่ยวกับ C#"— ใบสำเนางานนำเสนอ:

1 ความรู้พื้นฐานเกี่ยวกับ 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

2 Visual C# .NET เครื่องมือพัฒนาโปรแกรมด้วยภาษา C#

3 วัตถุประสงค์หลักของภาษา C#
เป็นภาษาใหม่ที่มีประสิทธิภาพการทำงานเทียบเท่า หรือเหนือกว่า C++ แต่ไม่ยุ่งยากและซับซ้อนเท่า โดยสามารถใช้งานได้ง่ายเหมือนภาษา Visual Basic ทำให้สามารถพัฒนาแอปพลิเคชันในระดับสูงได้ดียิ่งกว่าการใช้ภาษา Visual Basic

4 คำอธิบาย (Comment) // comment /* multiline comment */

5 บล็อก (Block) {…} กำหนดจุดเริ่มต้นและสิ้นสุดการทำงานแต่ละส่วน
ภายในบล็อก สามารถมีบล็อกย่อยๆ ซ้อนลงไปอีก

6 ตัวอย่างการใช้บล็อก Class block If block If…then block Else block
1 Class myClass { 2 if (…) { 3 for (…) { 4 … 5 } // end for block 6 } // 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

7 เครื่องหมายสิ้นสุดคำสั่ง (;)
; ตัวแสดงจุดสิ้นสุดแต่ละคำสั่ง จบบล็อก ไม่ต้องใส่ ; ตัวอย่าง 1 x = 10; y = 2 + x; ตัวอย่าง 2 x = 10; y = 2 + x;

8 Keywords abstract as base bool break byte
case catch char checked class const continue decimal default delegate do double else enum event explicit extern false finally fixed float for foreach goto if implicit in int interface internal is lock long namespace new null object operator out override params private protected public readonly ref return sbyte sealed short sizeof stackalloc static string struct switch this throw true try typeof uint ulong unchecked unsafe ushort using virtual void volatile while

9 หลักการตั้งชื่อตัวแปร
ขึ้นต้นด้วยตัวอักษร, underscore (_) ห้ามใช้ตัวเลข หรืออักขระเป็นตัวเริ่มต้น รูปแบบตัวอักษรพิมพ์ต่างกัน ถือเป็นคนละตัว (case sensitive) เช่น myvar, myVar, MYVAR ถือเป็นคนละตัว

10 การประกาศตัวแปร (Variable)
วิธี1 [ชนิดของข้อมูล] ชื่อตัวแปร; วิธี2 [ชนิดของข้อมูล] ชื่อตัวแปร = [ค่าเริ่มต้นของตัวแปร]; int x; int y, z; int Y, Z = 3; Double d = 10.99; String D = "Hello World",str; หมายเหตุ: ไม่สามารถประกาศตัวแปรซ้ำภายในบล็อก {…} เดียวกัน

11 การประกาศค่าคงที่ (Constant)
const int x = 1; const double pi = 3.14; หมายเหตุ: ไม่สามารถเปลี่ยนแปลงค่าคงที่ได้

12 ชนิดของข้อมูล Numeric (เลขจำนวนเต็ม และเลขทศนิยม) Char (ตัวอักษร)
String (ข้อความ) Boolean (บูลีน) Object

13 เลขจำนวนเต็ม 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)

14 เลขจำนวนเต็มบวก 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 ถึง

15 เลขทศนิยม Data type ขนาด ค่าของข้อมูล float (System.Single) 4 bytes
±1.5 x ถึง ±3.4 x 1038 ความแม่นยำของทศนิยมอยู่ที่ 7 ตำแหน่ง double (System.Double) 8 bytes ±5.0 x ถึง ±1.7 x 10308 ความแม่นยำของทศนิยมอยู่ที่ 15 ตำแหน่ง, มีช่วงของข้อมูลกว้างที่สุด decimal (System.Decimal) 16 bytes ±1.0 x ถึง ±7.9 x 1028 ความแม่นยำของทศนิยมอยู่ที่ 28 ตำแหน่ง, ช่วงข้อมูลแคบกว่า double แต่ให้ค่าละเอียดมากในส่วนของทศนิยม เหมาะกับการคำนวณเรื่องเงิน

16 ประเภทข้อมูลที่ไม่ใช่ 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

17 สัญลักษณ์พิเศษ char Meaning Value \’ Single quote 0x0027 \”
Double quote 0x0022 \\ Backslash 0x005C \0 Null 0x0000 \a Alert 0x0007 \b Backspace 0x0008 char Meaning Value \f Form feed 0x000C \n New line 0x000A \r Carriage return 0x000D \t Horizontal tab 0x0009 \v Vertical tab 0x000B

18 คำสั่งแสดงผล วิธี1 Console.Write (ข้อความ);
วิธี2 Console.WriteLine (ข้อความ); Console.Write ("Hello"); Console.WriteLine ("Hello World"); string str = "Hello"; Console.Write (str + " World");

19 คำสั่งรับข้อมูล วิธี1 Console.Read (); หรือ Console.ReadLine ();
Console.Write ("What is your name? : "); string name = Console.ReadLine ( ); // Laksamee Console.Write ("My name is " + name); Console.Read ( );

20 operator Arithmetic Operators String Operators Boolean Operators
* / % = += -= *= /= %= String Operators + Boolean Operators < <= > >= == != ! && || ^ A = 2+3; B = 4 % 3; X++; ++X; X--; --X; Y += 3; Message = "Hello "+Name If (A=5 && B>6) {…}

21 (เงื่อนไข) ? (ค่าเมื่อเงื่อนไขเป็นจริง) : (ค่าเมื่อเงื่อนไขเป็นเท็จ)
Conditional Operator (เงื่อนไข) ? (ค่าเมื่อเงื่อนไขเป็นจริง) : (ค่าเมื่อเงื่อนไขเป็นเท็จ) เป็นวิธีลัดที่ใช้ในการเปรียบเทียบ n = 2; int a = (n = = 0) ? 1 : n; // a = ___

22 ลำดับความสำคัญของ 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 =, +=, =,*=, /=, %=, <<=, >>=, &=, ^=, |=

23 การแปลงประเภทข้อมูล Implicit Conversion
เป็นการแปลงข้อมูลที่เกิดขึ้นเองโดยอัตโนมัติ จากข้อมูลที่มีขนาดเล็กกว่าไปเป็นขนาดที่ใหญ่กว่า เช่น จาก int ไปเป็น double int a = 123; long b = a;

24 การแปลงประเภทข้อมูล (ต่อ)
Explicit Conversion (หรือ Cast) เป็นการแปลงข้อมูลตามที่ผู้พัฒนาต้องการ เช่น จาก double ไปเป็น int int x = 123; short z = (short) x; short y = short.Parse(X); float p = 7.5F;

25 การแปลงประเภทข้อมูล Numeric
ตัวอักษร แทนข้อมูล U uint or ulong L long or ulong UL ulong F or f float D or d double M or m decimal float n1 = 7.5F; double n2 = 7.5; double n3 = 7.5D; decimal n4 = 7.5M;

26 การแปลงประเภทข้อมูลใดๆ
[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

27 การกำหนดเงื่อนไข Selection statements : if และ switch…case
Iteration statements : while, do…while และ for Jump statements : break, continue, goto, return และ throw

28 if-else statement if (condition) { … } if (condition) { … } else {
ตัวอย่าง bool isEven (int x) { if (x%2 ==0) return true; else return false; } if (condition) { } else {

29 else if & Nested if statement
if (condition1) { } else if (condition2) { else { if (condition1) { if (condition2) { if (condition3) { } ตัวอย่าง char Result; if (Score > 80) Result = ‘A’; else if (Score > 50) Result = ‘B’; else Result = ‘F’;

30 แบบฝึกหัด if statement (1)
จงเขียนโปรแกรมสำหรับคำนวณเกรด โดยรับคะแนนจากผู้ใช้ โดยกำหนดว่า Grade Score A 81 – 100 B 70 – 80 C 60 – 69 D 50 – 59 F 0 – 49 เฉลย เฉลย (เรียก method)

31 แบบฝึกหัด if statement (2)
ค่าบริการน้ำประปาแห่งหนึ่ง มีอัตราดังตาราง จงเขียนโปรแกรมให้สามารถทำการคำนวณค่าใช้บริการน้ำประปาของบริษัทแห่งนี้ หน่วยที่ ราคา / หน่วย (บาท) 10 หน่วยแรก 10 12 15 > 30 20 ตัวอย่าง ถ้าใช้น้ำประปาทั้งหมด 34 หน่วย ต้องชำระค่าใช้บริการทั้งหมด 450 บาท เฉลย

32 แบบฝึกหัด if statement (3)
จงเขียนโปรแกรมสำหรับพิจารณาเงินเดือนจากประสบการณ์ทำงานโดยให้ผู้ใช้ป้อนประสบการณ์ทำงาน โดยกำหนดว่า ถ้าประสบการณ์ 5 ปีขึ้นไป ได้รับเงินเดือน 20,000 บาท ถ้าประสบการณ์ 2 ปีขึ้นไป ได้รับเงินเดือน 15,000 บาท มิเช่นนั้น ได้รับเงินเดือน 12,000 บาท เฉลย

33 แบบฝึกหัด if statement (4)
จงเขียนโปรแกรมแสดงผลยอดเงินคงเหลือในบัญชีเงินฝาก ซึ่งฝากมาแล้ว 1 ปี โดยให้ผู้ใช้ป้อนเงินต้น โดยกำหนดอัตราดอกเบี้ยดังนี้ เงินต้น 100,000 บาทขึ้นไป อัตราดอกเบี้ย 3 % เงินต้น 50,000 บาทขึ้นไป อัตราดอกเบี้ย 2 % เงินต้น ต่ำกว่า 50,000 บาท อัตราดอกเบี้ย 1 % เฉลย (เรียก method)

34 switch statement switch (expression) { case value1 : statement1;
break; case value2 : statement2; case valueN : statementN; default : statement; } expression: ตัวเลข or string string str, color; Console.Write("Enter day of week:"); str = Console.ReadLine(); switch (str) { case "Sunday" : color = "red"; break; case “monday" : case "Monday" : color="yellow"; ... default : color="invalid"; } Console.WriteLine("color of " + str.ToUpper() + " is " + color.ToUpper()); Console.Read (); string str, color; Console.Write("Enter day of week:"); str = Console.ReadLine(); switch (str) { case "Sunday" : color = "red"; break; case "Monday" : color="yellow"; ... default : color="invalid"; } Console.WriteLine("color of " + str.ToUpper() + " is " + color.ToUpper()); Console.Read ();

35 แบบฝึกหัด switch statement (1)
จงเขียนโปรแกรมแสดงเมนูการทำงาน ดังหน้าจอ เลือก 1 ให้แสดงข้อความ "Sub Menu 1 : ADD" เลือก 2 ให้แสดงข้อความ "Sub Menu 2 : SUBTRACT" เลือก 3 ให้แสดงข้อความ "Sub Menu 3 : EXIT" เฉลย

36 for statement for (counter = first value; counter condition; adjust counter value) { } for (…) { }}}

37 ตัวอย่าง for statement (1)
int i, sum; for (i=1; i<5; i++){ sum += i; } Console.Write(sum+" "+i); // sum=_____ i=_____ int sum; for (int i=1; i<5; i++){ sum += i; } Console.Write(sum+" "+i); // sum=_____ i=_____

38 ตัวอย่าง for statement (2)
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( );

39 while statement while (condition) { … } ตัวอย่าง
int Result = 0; int i = 0; while (i < 2) { i += 1; Result += i; } // end while Console.Write (Result + " " + i); // Result=____ i=____

40 do-while statement do { … } while (condition); while (condition) { … }
ตัวอย่าง int Result = 0; int i = 0; do { i += 1; Result += i; } while (i < 2); Console.Write (Result + " " + i); // Result=____ i=____ while (condition) { }

41 แบบฝึกหัด for & while statement (1)
จงเขียนโปรแกรมเพื่อแสดงผลลัพธ์ดังนี้ 3 4 5 4 5 5 เฉลย (for statement) เฉลย (while statement)

42 แบบฝึกหัด for & while statement (2)
จงเขียนโปรแกรมนับว่าหากต้องการเพิ่มค่าจาก 3 จนมากกว่าหรือเท่ากับ 12 จะต้องบวกด้วย 2 ทั้งหมดกี่รอบ เฉลย (for statement) เฉลย (while statement)

43 แบบฝึกหัด for & while statement (3)
จงเขียนโปรแกรมนับว่าหากต้องการลดค่าจาก 100 จนน้อยกว่าหรือเท่ากับ 3 จะต้องหารด้วย 4 ทั้งหมดกี่รอบ เฉลย (for statement) เฉลย (while statement)

44 แบบฝึกหัด for & while statement (4)
พี่มอสมีแฟนคลับ 2,857 คน โดยแต่ละปีจะมีแฟนคลับเพิ่มขึ้นปีละ 8% จงเขียนโปรแกรมคำนวณว่าจะใช้เวลากี่ปี พี่มอสจึงจะมีแฟนคลับมากกว่า 5,000 คน เฉลย (for statement) เฉลย (while statement)

45 แบบฝึกหัด for & while statement (5)
จงเขียนโปรแกรมให้ผู้ใช้ป้อนตัวเลขจำนวนเต็ม 1 จำนวน แล้วหาผลลัพธ์สูตรคูณ โดยแสดงผลลัพธ์เป็นตารางดังนี้ (สมมติผู้ใช้ป้อนเลข 5) 5 * 1 = 5 5 * 2 = 10 … … 5 * 12 = 60 เฉลย (for statement) เฉลย (while statement)

46 break break; // คำสั่งให้ออกจากลูป โดยหยุดการตรวจสอบเงื่อนไขถัดไป
int x=0, i=0; while (i<15) { x++; if ((x%5)==0) { break; } Console.WriteLine("i = " + i + ", x = " +x); i++; } // end while

47 continue continue; // คำสั่งให้วนลูปถัดไป โดยไม่ทำคำสั่งที่เหลือในลูปนั้น int x=0, i=0; while (i<15) { x++; if ((x%5)==0) { continue; } Console.WriteLine ("i=" + i + ", x=" +x); i++; } // end while

48 goto วิธี 1 goto statement-label; วิธี 2 goto case-statement;
วิธี 3 goto default; คำสั่งให้โปรแกรมกระโดดไปยังตำแหน่งที่ต้องการ

49 ตัวอย่าง : 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."); default : Console.Write("Please enter your title\n"); goto enterTitle; } // end switch } // end while Console.WriteLine("Exit Program");

50 return return expression; คำสั่งออกจาก method พร้อมการส่งค่า
ค่าที่ส่งกลับ สัมพันธ์กับประเภทข้อมูลที่ประกาศ int CalcX (int a) { int x = a * 100; return x; }

51 Method [modifier] void [name]([param1]…) { … } void sayHello ( ) {
void main ( ) (string[ ] args){ sayHello ( ); // Hello sayHello ("Peter"); // Hello Peter } void sayHello ( ) { Console.Write("Hello"); } void sayHello (string name) { Console.Write("Hello " + name); }

52 Method [modifier][type][name]([param1]…) { … return [value]; }
void main ( ) (string[ ] args) { int a = add ( ); // a = _____ int b = add (3,5); // b = _____ double c = add (3,5); // c = _____ string d = add ( ); // d = _____ } int add ( ) { return 2 + 3; } int add (int x, int y) { return x + y; }

53 แบบฝึกหัด Method (1) จงเขียนโปรแกรมให้ผู้ใช้ป้อนตัวเลขจำนวนเต็ม 1 จำนวน แล้วหาว่าค่าที่รับนั้นเป็นเลขคู่หรือเลขคี่ เช่น รับ 5 -> เลขคี่ รับ 8 -> เลขคู่ เฉลย (ใน main) เฉลย (เรียก method)


ดาวน์โหลด ppt ความรู้พื้นฐานเกี่ยวกับ C#

งานนำเสนอที่คล้ายกัน


Ads by Google