หลังจากที่ Entry ที่แล้ว 10 คำสั่ง C# ย่อ Code ให้สั้นลง ทีนี้มาดูกันว่าแล้ว c# 4.0 มีอะไรใหม่กันบ้าง ความจริง C# 4.0 ออกมาได้สักพักแต่ยังเป็น version BEta อยู่ครับ ถ้าหากอยากดู Anders Hejlsberg (คนคิดค้นภาษา C#)ได้เปิดตัวและอธิบายความใหม่ของ C# 4.0 แล้วล่ะก็สามารถดูได้จากที่นี้
http://channel9.msdn.com/pdc2008/TL16/
หัวข้อหลักๆ แบ่งได้เป็น
1. Dynamic Lookup
ได้เพิ่ม static type ตัวใหม่ ที่มีชื่อว่า Dynamic ถ้าเราประกาศให้ object ตัวหนึ่งเป็น dynamic type เราสามารถเรียก object นั้นยังไงก็ได้ ตอน compile จะไม่ฟ้อง Error แต่จะถูกตรวจสอบและฟ้องตอน Runtime ตัวอย่างเช่น
dynamic d = GetDynamicObject(…);
d.M(7); // calling methods
d.f = d.P; // getting and settings fields and properties
d[“one”] = d[“two”]; // getting and setting thorugh indexers
int i = d + 3; // calling operators
string s = d(5,7); // invoking as a delegate
object o = "code"; string myName = o as string ; Console.Write(myName.Insert(myName.Length, "snippet"));
ถ้าเขียนด้วย C# 4.0 สามารถเขียนได้โดย
dynamic d = "code"; // implicit conversion Console.Write(d.Insert(d.Length, "snippet"));
ทำให้ง่ายและยืดหยุ่นต่อการเขียนมากขึ้น
2. Named and optional parameters
อันนี้น่าจะมีมาตั้งนานเพราะทำให้เรา กำหนด parameter ใน Methods ได้ง่ายขึ้นและสั้นลงจากเดิมที่ต้องทำoverloading
public void Insert(string name){ .. } public void Insert(string name,int age){ .. }
เราสามารถกำหนดค่า default ให้กับ parameter ได้
public void Insert(string name,int age="18"){ .. }
เวลาเรียกใช้งานก็สามารถเรียกได้แบบoptional ได้
Insert("codesnippet"); Insert("codesnippet",20);
3. COM specific interop
COM Methods ส่วนมากจะเป็นคืนค่ามาเป็น
variant types บ้างครั้งอาจจะทำให้เราไม่รู้จักว่า return value ตัวนั้นแท้จริงแล้วคืนค่าเป็น static type อะไรและอาจจะไม่สามารถ ทำ type casting นั้นได้ทำให้ยากต่อการเขียนดังนั้นใน C# 4.0 จึงนำหลัก type dynamic และ named and optional parameter นำมาใช้ เช่น
ใช้คำสั่ง excel.Cells[1, 1].Value = "Hello";
แทนคำสั่ง ((Excel.Range)excel.Cells[1, 1]).Value2 = "Hello"; และ ใช้คำสั่ง Excel.Range range = excel.Cells[1, 1]; แทนคำสั่ง Excel.Range range = (Excel.Range)excel.Cells[1, 1];
นอกจากนั้นได้ตัดคำสั่ง ref ออกไปซึ่งจากเดิมที่เราต้องใส่ parameter เป็น ref missing
4. Variance
ปกติถ้าเราเรียกใช้ type ที่เป็น generic เราจะเขียน
IList<string> strings = new List<string>(); IList<object> objects = strings;
โดนปกติแล้ว strings ไม่สามารถแปลง type ไปเป็น object ได้ แต่ใน c# 4.0 สามารถแปลง type ได้
objects[0] = 5; string s = strings[0];
การทำ variance ยังมีข้อจำกัดอีกนะครับ ทั้งเรื่องของ Covariance และ Contravariance อันนี้ลองดูตัวอย่างจาก code ด้านล่าง
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SimpleVariance { class Animal { } class Cat: Animal { } class Program { // To understand what the new CoVariance and ContraVariance code does for you // Try deleting or adding the words out and in from the following 2 lines of code: delegate T Func1<out T>(); delegate void Action1<in T>(T a); static void Main(string[] args) { Func1<Cat> cat = () => new Cat(); Func1<Animal> animal = cat; Action1<Animal> act1 = (ani) => { Console.WriteLine(ani); }; Action1<Cat> cat1 = act1; Console.WriteLine(animal()); cat1(new Cat()); } } }
และสามารถDownload เอกสารและ code ตัวอย่างได้จากที่นี้ครับ
http://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=csharpfuture&ReleaseId=1686&wa=wsignin1.0
