Basic: //利用out, 不用給i,j assign初始值 int i,j; f(out i, out j) {} //using, 括號外自動destroy對象 using (Font theFont == new Font("ArIEl", 10.0f)){} //constants const int i = 32; //Enumeration enum E {a=5,b=8} int i = (int) E.a; //turn buff data to string Encoding.ASCII.GetString(buff, 0, bytesRead) // read data string s = Console.ReadLine() int j= Convert.ToInt32(s); //get textbox value and convert to double double left = double.Parse(textBox1.Text); Application.DoEvent(); //this will let app deal with event array: A[] a = new A[5]; // a[0]to a[5] is null int[] arr = new int[2] {1,2} int[] arr = {2,3,4,5} foreach(A a in arr_A) {} for(int i=0; i<arr.Length; i++) {} //Rectangular Arrays int[,] rectArray = new int[4,5] int[,] arr = {{1,2,3,4},{1,2,3,4}{1,2,3,4}}; //init //Jagged array, 數組中包含數組,各維不等長 // ja[0].Length is length of ja[0] int [][] ja = new int[4][]; //using params transfer a array to a function //using f(1,2,3,4) to call the function f(params int[] values){ foreach (int[] a in values) {} } Collection interface: indexers: //define a indexers public Myclass this[int offset] {get{};set{}} //call it Employee joe = bostonOffice["Joe"]; IEnumerable: //return a IEnumerator object GetEnumerator() IEnumerator: Reset(){} Current(){} MoveNext(){} IComparable: //Class inherit IComparable interface //Implement CompareTo() method CompareTo() ArrayLists: Count Get number of elements Add() Add an object Clear() Remove all objects Reverse() Reverse order of elements Sort() Sort the elements function: //default by value f(int x){} //by reference f(ref int x){} class: //存取: public, private, protected, internal, protected internal this, static //static member must be init in class //繼承 class A: B {} //多態 virtual,override //init對象 Employee emp1 = new Employee();
//先隱式轉換3為Roman Roman r4 = r1 + 3; //先顯式轉換7.5為Roman Roman r5 = r1 +(Roman)7.5;
// 運算符重載,先返回int, 然後用隱式轉換 public static Roman Operator+ (Roman l, Roman r) { return (l.val+r.val); } //顯式轉換 public static explicit Operator Roman(float val) { return new Roman(val); } //隱式轉換 public static implicit Operator Roman(int val) { return new Roman(val); } //propertIEs, 注意大小寫 public int Age { get {return age;} set {age = value;} }
//Interface //interface methods must be implement in class public interface IA {} //is 測試類是否從接口繼承 if (a is IA) {IA c = (IA) a; } //as 測試類是否從接口繼承 IA c = a as IA; if (c != null) {} //interface propertIEs pubiic interface IAA: IA { float F{get;set;} } //mutiface interface inheritance public class C: IA,