C# Tutorial ตอนที่ 3 (โครงสร้างพื้นฐานของภาษา(ต่อ))
posted on 06 Jun 2005 01:59 by tidno1 in csharp-and-dotnetฉลอง entry ที่ 100 ด้วยตอนที่ 3 ของบทความชุดนี้ล่ะครับ เอาล่ะ เรามาเริ่มกันเลยดีกว่า
Operator(Cont')
7. String operator มี +, =, ==, != และ += โดย เครื่องหมาย + มีไว้ต่อ string โดยที่จะต่างกับ + ทั่ว ๆ ไปตรงที่ไม่มีคุณสมบัติสลับที่ นอกนั้นก็มีความหมายเหมือนปกติ
class StringOperator {
static void Main() {
string s = "Hello";
Console.WriteLine(s == "hello");
Console.WriteLine(s + " world");
s += " ";
s += "world";
Console.WriteLine(s);
}
}
Primitive Data type
| Data type | Description | Aliased From |
|---|---|---|
| sbyte | 1-byte signed integer เก็บค่าตั้งแต่ -128 ถึง 127 | public struct System.SByte |
| byte | 1-byte unsigned integer เก็บค่าตั้งแต่ 0 ถึง 255 | public struct System.Byte |
| short | 2-byte signed integer เก็บค่าตั้งแต่ -32768(-215) ถึง 32767(215 - 1) | public struct System.Int16 |
| ushort | 2-byte unsigned integer เก็บค่าตั้งแต่ 0 ถึง 65536(216 - 1) | public struct System.UInt16 |
| int | 4-byte signed integer เก็บค่าตั้งแต่ -2,147,483,648(-231) ถึง 2,147,483,647(231 - 1) | public struct System.Int32 |
| uint | 4-byte unsigned integer เก็บค่าตั้งแต่ 0 ถึง 4,294,967,295(232 - 1) | public struct System.UInt32 |
| long | 8-byte signed integer เก็บค่าตั้งแต่ -9,223,372,036,854,775,808(-263) ถึง -9,223,372,036,854,775,807(263 - 1) | public struct System.Int64 |
| ulong | 8-byte signed integer เก็บค่าตั้งแต่ 0 ถึง 18,446,744,073,709,551,616(264 - 1) | public struct System.UInt64 |
| float | 4-byte floating-point number เก็บค่าในช่วง ±(1.401298E-453.4028235E+38 ถึง 3.4028235E+38) | public struct System.Single |
| double | 8-byte floating-point number เก็บค่าในช่วง ±(4.94065645841246544E-324 ถึง 1.79769313486231570E+308) | public struct System.Double |
| bool | Boolean type เก็บค่า true และ false | public struct System.Boolean |
| char | 2-byte Unicode character | public struct System.Char |
โดย ทุกตัวนี้ถูก define ไว้ใน System.ValueType หมดครับ เนื่องจาก ทุก primitive type นี้เป็น struct หมดเลย ดังนั้นเราจะไม่สามารถทำให้ type เหล่านี้รับค่า null ได้เลย หากต้องการให้ทำได้คงต้องรอความสามารถ Nullable type ใน .NET 2.0 ล่ะครับ
คงสงสัยใช่มั้ยล่ะครับ ว่าช่องสุดท้ายของตารางคืออะไร มันคือชื่อเต็ม ๆ ของ datatype แต่ละตัวน่ะครับ โดยมันจะบรรจุ ฟังก์ชันและคุณสมบัติบางอย่างเอาไว้ ลองดูจากตัวอย่างดีกว่าครับ
class PrimitiveType {
static void Main() {
Console.WriteLine(ushort.MaxValue);
Console.WriteLine(Double.NaN);
Console.WriteLine(Double.NegativeInfinity);
Console.WriteLine(Single.Epsilon);
float f = Single.PositiveInfinity;
Console.WriteLine(Single.IsInfinity(f));
Console.WriteLine(Boolean.FalseString);
Console.WriteLine(Decimal.One);
string s = "2605";
int i = Int32.Parse(s);
Console.WriteLine(i + 421);
}
}
Data type อื่น ๆ ที่ใช้บ่อย
| Data type | Description | Aliased From |
|---|---|---|
| decimal | floating-point แบบละเอียด เก็บค่าตั้งแต่ ((-296 ถึง 296)/ 10(0 ถึง 28)) เวลาจะระบุว่า literal ใด ๆ ว่าเป็น decimal ให้ใช้ ตัว M หรือ m ต่อท้าย เช่น 0.001m | public struct System.Decimal |
| DateTime | เก็บค่าข้อมูล วันที่และเวลาในช่วง 00:00:00 1 มกราคม 0001 จนถึง 23:59:59 31 ธันวาคม 9999 โดยที่มีความละเอียดในการเก็บ 0.1 millisecond(100 nanosecond) | public struct System.DateTime |
| string | ลำดับของ character ที่ถูก encode แบบ UTF-16 | public class System.String |
| object | เป็น type ที่ใหญ่ที่สุดในนี้ คือทุก ๆ type สามารถเปลี่ยนมาเป็น type นี้ได้หมด (คล้าย ๆ กับ variant ใน VB® ล่ะครับ (แต่ประหยัด mem และ พลังงานในการใช้กว่าเยอะ) | public class System.Object |
class DateTimeTest {
static void Main() {
DateTime dtCurrent = DateTime.Now;
Console.WriteLine(dtCurrent.ToShortDateString());
Console.WriteLine(dtCurrent.ToLongDateString());
Console.WriteLine(dtCurrent.ToShortTimeString());
DateTime dtTomorrow = dtCurrent.AddDays(1);
Console.WriteLine("Tomorrow is {0}", dtTomorrow.ToShortDateString());
DateTime dtTodayInNextYear = dtCurrent.AddYears(1);
Console.WriteLine("Today is {0} and this day in next year is {1}", dtCurrent.DayOfWeek, dtTodayInNextYear.DayOfWeek);
}
}
Type Casting
คือการเปลี่ยน type น่ะครับ ลองทบทวนถึงคณิตศาสตร์ตอนม.ปลายนะครับ คงจำกันได้ว่า จำนวนเต็มเป็น subset ของจำนวนจริง ดังนั้นการเขียนอย่างนี้
float f = i;
int i = f;
int i = (int)f;
โดยกรณีที่สามารถทำการ implicitly convert ได้ก็คือ type ที่เป็นตัวรับค่า(lvalue) จะต้องมีขนาดใหญ่กว่า(เป็น superset ของ) อีก type นึงเช่น จาก int เป็น float, float เป็น double, byte เป็น short หรือ char เป็น int(จะได้ค่า ordinal ของ unicode)
ในกรณีที่ไม่สามารถทำได้ ก็ให้เราทำการ casting มันซะ โดยการใส่ (ชื่อ type) นำหน้า ถ้าหากเรายังเจอมันด่าอย่างนี้มา
int i = Convert.ToInt32(s);
string t = Convert.Tostring(i);
โดย method Parse()นี่มีไว้ใช้เปลี่ยน string เป็นตัวเลขนะครับ มีอยู่ใน type ที่เป็น ตัวเลขทุก type(รวมทั้ง char และ bool ก็มี) หรือการเปลี่ยน type ทั้งหลายให้เป็น string นี่สามารถใช้ ToString() ซึ่งจะมีอยู่ในทุก ๆ type ไม่ว่ามันจะเป็น type พิสดาร ขนาดไหน
Console.WriteLine(x.ToString() + y.ToString());
Console.WriteLine(x + y);
- จำตัวอย่างวันแรกได้ใช่มั้ยครับ ที่เรารับค่าอายุมาเป็น string วันนี้เราลองเปลี่ยนตัวแปร age ให้เป็น int ดู แล้วใช้ method Int32.Parse() ทำการเปลี่ยนค่าที่ได้มาจากการ ReadLine ให้เป็น int
- ลองเขียนโปรแกรมที่ใช้คลาส DateTime ดูครับ
เรื่อง control statement อย่างพวก if clause หรือการวนลูปทั้งหลายคงต้องขอยกยอดไปตอนต่อไปอีกครั้งล่ะครับ
เรื่องของ Unicode ผู้สนใจสามารถไปหาอ่านได้ใน wikipedia , unicode.org
เรื่องของ compiler ภาษาไทย ผมไม่ได้เขียนเล่น ๆ นะ ใครว่ามันไม่มีจริง ๆ ล่ะครับ .Net Programming Language สัญชาติไทย(โดยเว็บ ThaiSharp.net)

#1 By nonsense on 2005-06-06 04:50