You are on page 1of 25

C# เบื้องตน กอนการเขียนเกมดวย XNA

สุุพจน สวัตติวงศ
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

You might also like