#眉標=VS2010、Lambda、.NET 4.0 #副標=Visual Studio 2010開發平台(1) #大標=C#與VB.NET語法的變化 #作者=文/圖 王寧疆 ===<反灰>============= Public Property Owner As String = "John" '宣告型態String,名稱為Owner,內容值為John的屬性’ ================ ===<反灰>============= Private _Owner As String = "John" '宣告變數,填入John Property Owner As String '宣告String,名為Owner Get Return _Owner '傳回內容值 End Get Set(ByVal value As String) _Owner = value '設定屬性內容值 End Set End Property ================ ===<反灰>============= Public Property Name As String '定義Name,型態String Public Property Owner As String = "John" '定義Owner,型態String,內容值為John的屬性 Public Property Items As New List(Of String) From {"A", "B", "C"} '定義Items,型態為字串集合,內容值分別為A、B、C Public Property ID As New Guid() '定義ID,內容值為Guid ================ ===<反灰>============= Dim winterMonths = {"December", "January", "February"} ================ ===<反灰>============= Dim numbers = {1, 2, 3, 4, 5} ================ ===<反灰>============= Dim JaggedArray = {({0, 1}), ({2, 3, 4}), ({5})} ================ ===<反灰>============= cmd.CommandText = _ "SELECT * FROM Titles JOIN Publishers " & _ "ON Publishers.PubId = Titles.PubID " & _ "WHERE Publishers.State = 'CA' ================ ===<反灰>============= cmd.CommandText = "SELECT * FROM Titles JOIN Publishers " & "ON Publishers.PubId = Titles.PubID " & "WHERE Publishers.State = 'CA' ================ ===<反灰>============= Dim increment1 = Function(x) x + 1 '定義名increment1的Lambda,負責將參數的內容值加1後傳回 Dim increment2 = Function(x) '定義名increment2的Lambda Return x + 2 '內容值加2後傳回 End Function '之後程式便可以進行呼叫,就像呼叫一般的程序 Console.WriteLine(increment1(1)) '印出2 Console.WriteLine(increment2(2)) '印出4 ================ ===<反灰>============= Dim notNothing = Function(num? As Integer) num IsNot Nothing ================ ===<反灰>============= Dim arg As Integer = 14 '宣告為Integer,名稱為arg Console.WriteLine(notNothing(arg)) '利用名為notNothing的Lambda判斷arg變數是否已經指派過內容值 ================ ======<反灰>============= int CalculateBMI(int weight, int height) { return (weight * 703) / (height * height); } ================== ===<反灰>============= Console.WriteLine(CalculateBMI(123, 64)); ================ =====<反灰>============= Console.WriteLine(CalculateBMI(weight: 123, height: 64)); //傳遞123給名為weight的參數,傳遞64給名稱為height的參數 Console.WriteLine(CalculateBMI(height: 64, weight: 123)); //傳遞123給名稱為weight的參數,傳遞64給名稱為height的參數 ================== ===<反灰>============= public void ExampleMethod(int required, string optionalstr = "default string", int optionalint = 10) ================ ===<反灰>============= dynamic d = 100; //宣告名為d的動態物件並填入100內容值 string s = string.Format("value: {0}, type: {1}", d, d.GetType()); //將d的值和型態寫入到變數s MessageBox.Show(s); //顯示s內容值 ================ ===<反灰>============= Dim d = 100 '宣告d動態物件並填入100內容值 Dim s As String=String.Format("value:{0},type:{1}",d,d.GetType()) '將d的值和型態寫入到s MessageBox.Show(s) '顯示s值 ================ ===<反灰>============= dynamic d = "Hello"; string s = string.Format("value: {0}, type: {1}", d, d.GetType()); MessageBox.Show(s); 'VB.NET版本 Dim d = "Hello" Dim s As String = String.Format("value: {0}, type: {1}", d, d.GetType()) MessageBox.Show(s) ================ ===<反灰>============= var excelApp = new Microsoft.Office.Interop.Excel.Application(); //建立Excel的物件 excelApp.Workbooks.Add(); //加入新活頁簿 excelApp.Visible = true; //顯示被啟動的Excel //準備儲存格欲使用的格式 var myFormat = Microsoft.Office.Interop.Excel.XlRangeAutoFormat.xlRangeAutoFormatAccounting1; excelApp.get_Range("A1", "B4").AutoFormat(myFormat, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); //將A1..B4間的儲存格設定成指定的格式 ================ ===<反灰>============= excelApp.Range["A1", "B4"].AutoFormat( Format: myFormat); //將A1..B4之間的儲存格設定成指定的格式 ================ ===<反灰>============= class Base //Base類別 { public static void PrintBases(IEnumerable bases) //列印IEnumerable泛型集合法 { foreach (Base b in bases) //取出bases集合中的元素 { Console.WriteLine(b); //列印元素內容 } } } class Derived :Base //衍生自Base類別的Derived { private int m_Data; //儲存物件內容變數 public Derived(int i) //建構函式 { m_Data = i; } } List dlist = new List() {new Derived(1), new Derived(2)};//建立Derived的泛型集合 Derived.PrintBases(dlist); //透過基底類別的PrintBases列印dlist集合的內容 ================ ===<反灰>============= abstract class Shape //Shape類別 { public abstract double Area { get; } //取得面積的屬性 } class Circle : Shape //衍生自Shape的Circle { private double r; //存放半徑的變數 public Circle(double radius) { r = radius; } //建構函式 public double Radius { get { return r; } } //取得半徑的屬性 public override double Area { get { return Math.PI * r * r; } } //override取得面積的屬性 } class ShapeAreaComparer : System.Collections.Generic.IComparer //實作IComparer介面的ShapeAreaComparer { int IComparer.Compare(Shape a, Shape b) //比較Shape物件的大小的方法 { if (a == null) //判斷a物件是否沒有指派內容值 return b == null ? 0 : -1; //如果b物件沒有指派內容值,則傳回0,否則傳回-1 return b == null ? 1 : a.Area.CompareTo(b.Area); //如果b物件沒有指派內容值,則傳回1,否則傳回a物件的面積和b物件的面積比較的結果 } } SortedSet circlesByArea = new SortedSet(new ShapeAreaComparer()) new Circle(7.2), new Circle(100), null, new Circle(.01) }; //建立元素型態為Circle的SortedSet泛型集合,並指定ShapeAreaComparer來比較大小 foreach (Circle c in circlesByArea) //取出circlesByArea集合中每個Circle { listBox1.Items.Add( c == null ? "null" : "Circle with area " + c.Area); //將Circle面積顯示到ListBox } ================