Professional Documents
Culture Documents
สุุพจน สวัตติวงศ
tohpus@hotmail.com
โครงสรางของ C#
โครงสรางของ
• Namespaces อาจจะมี structs, interfaces,
delegates และ enums
delegates, และ
• Simplest case: single class, single file, default
namespace
Unified Type System
Unified Type System
Value กับ Reference
Value กบ
Simple Type
Simple Type
การเปลี่ยนแปลงกันระหวาง Simple Types
การเปลยนแปลงกนระหวาง Simple Types
if Statement
if Statement
switch Statement
switch Statement
• ตัวั แปร
ป condition ใน
di i ใ switch สามารถใช
i h ใ ได
ไ ท ัง้ numeric, char, enum และ
i h S i
String
• ทุก case หากตองการไมใหท าตอดานลางตองมี break, return, throw, และ goto
switch with gotos
switch with gotos
Loops
foreach Statement
Enumerations
• การกําหนด Constant แบบ Enum
• กําหนดโดยตรงใน namespace ดั
p งนี้
enum Color { red, blue, green } // values: 0, 1, 2
enum Access { personal = 1 group = 2 all = 4 }
enum Access { personal = 1, group = 2, all = 4 }
enum Access1 : byte { personal = 1, group = 2, all = 4 }
• วธใชงานดงตอไปน
วิธีใชงานดังตอไปนี้
Color c = Color.blue; // enumeration constants must be qualified
Access a = Access.personal | Access.group;
if ((Access.personal & a) != 0)
Console WriteLine("access
Console.WriteLine( access granted
granted");
);
การใชเครื่องหมายของ enum ใน C#
การใชเครองหมายของ
ไ ส ามารถใช
• EEnumerations ไม
ti ใ int แทนได
i t ไ (ยกเวน ถูกกําํ หนด Type ไว
T ไ
กอน)
• Enumeration types ถูก inherit จาก object จึงใช method
( q
(Equals, ToString, ...)ได
, g, )
• Class System.Enum ครอบคลุมการทํางานบน enumerations
• (GetName, Format, GetValues, ...).
(G N F G V l )
Arrays
• การใช Array 1 มิติ
การใช Array หลายมต
การใช Array หลายมิติ
การใชงาน System.String
การใชงาน System String
• สามารถใช
ใ string ได
t i ไ โดยตรงดัังนีี้
string s = "Suphot";
• สามารถใช + เพื่อบวก string ได เชน
s = s + " Sawattiwong
s = s + Sawattiwong";;
• สามารถใช index ได เชน s[i]
• String มี length เชน s.Length
Strings เปนตัวแปร reference type แบบพเศษ
• Strings เปนตวแปร reference type แบบพิเศษ ซงทาหนาท
ซึ่งทําหนาที่
เปน reference เฉพาะพฤติกรรมของมัน
การใชงาน System.String
การใชงาน System String
• สามารถใช
ใ การเปรี
ป ียบเทีียบแบบ == และ!= ได
ไ เชน
(
if (s == "Alfonso") )
• Class String มี Methodอื่นเชน CompareTo,
I d Of St t With S b t i เปปนตน
IndexOf, StartsWith, Substring
Structs
• การกําหนด Struct
struct Point
{
public int x, y;// fields
public Point(int x, int y) { this.x = x; this.y = y; }// constructor
public void MoveTo(int a, int b) { x = a; y = b; }// methods
}
• การใชงาน
Point p = new Point(3, 4);// constructor initializes object on the stack
p.MoveTo(10, 20);// method call
Classes
• การกําหนด Classes
l
class Rectangle
{
Point origin;
public int width, height;
public Rectangle() { origin = new Point(0,0); width = height = 0; }
bli R t l () { i i P i t(0 0) idth h i ht 0 }
public Rectangle (Point p, int w, int h) { origin = p; width = w; height = h; }
public void MoveTo (Point p) { origin = p; }
}
การใชงาน
Rectangle r = new Rectangle(new Point(10, 20), 5, 5);
l l ( ( ) )
int area = r.width * r.height;
r.MoveTo(new Point(3, 3));
ความแตกตางระหวาง Classes และ
ความแตกตางระหวาง Classes และ Structs
Boxing /Unboxing
Boxing /Unboxing
• Boxing
object obj = 3;
object obj 3;
โดย 3 จะเขาไปอยูใน Heap ของ object
• Unboxing
int x = (int)obj;
int x = (int)obj;
คาในตัวแปร obj จะถูกเปลี่ยนเปน int แลวถูกเก็บมาในตัวแปร x
Contents of Classes or Structs
Contents of Classes or Structs
Fields และ Constants
Fields และ
Methods
Parameters