2013.10.20开始学习C#
1. 单继承;
2. 值类型:
基本:整数(byte,short,int, long), 布尔类型,实数,字符型;
结构体;
枚举类型;
3. 引用型数据:
类、代表、接口、数组
4. 类
域、属性、方法;
5. 代表: 安全的C#"指针“
delegate int MyDelegate();
using System; delegate int MyDelegate(); class P { public int Fun1(){return 0;} public static int Fun2(){return 0;} } class Q { static void Main() { P p = new P(); MyDelegate d; d = new MyDelegate(p.Fun1); d(); d = new Mydelegate(P.Fun2); d(); } }
6. 数组. System.Array
int []a1 = new []int {1,2}; //一维数组 int [,]a2 = new [,]int {{1,2},{3,4}}; //二维数组 int [,,]a3 = new int[1,2,3]; //三维数组 int [][]a4 = new int[3][]; //二维可变数组
7. 装箱,拆箱: object <-> 值类型
8. 参数:
值参数: int a
引用参数: ref int b
输出参数: out int c
9. 变量
与其他变量不同,局部变量不会被自动初始化,所以也没有默认值;
10. 操作符
exp is T
exp as T : 返回T类型,不成功则返回NULL
typeof : 获取类型
11. switch 中的 case 都是以 break 或者 goto case 或者 goto default 或者 throw 或者 return 来结束的;
------------------------------------------------------------------------------------------------------
微软官方文档
1. 经常变长的字符串用 StringBuilder;
2. Implicit Typed local variable
// When the type of a variable is clear from the context, use var // in the declaration. var var1 = "This is clearly a string."; var var2 = 27; var var3 = Convert.ToInt32(Console.ReadLine());
3. try-catch-throw
try { } catch(Exception e) { ... throw; }
4. try-finally 当 finally 中的块是Dispose方法时,可以用 using 语句来代替
Font font1 = new Font("Arial", 10.0f); try { byte charset = font1.GdiCharSet; } finally { if (font1 != null) { ((IDisposable)font1).Dispose(); } } // You can do the same thing with a using statement. using (Font font2 = new Font("Arial", 10.0f)) { byte charset = font2.GdiCharSet; }
5. object initializer
// Object initializer. var instance3 = new ExampleClass { Name = "Desktop", ID = 37414, Location = "Redmond", Age = 2.3 };