http://www.microsoft.com/china/msdn/library/langtool/vcsharp/vbconcprogramminglanguagefuturefeatures.mspx
1. Generic Type 泛型
定义一个MyList
1<t>,对这个MyList类设定方法,方法中,使用T而不是具体的类型float或者int什么的,忽略了类型的区别
2MyList<myclass> list1 = new MyList<myclass>();
3MyList<float> list2 = new MyList<float>();
4MyList<somestruct> list3 = new MyList<somestruct>();
5可以使用在Class、Struct和Interface
6可以在模板的定义中,具体的定义T属于的类型,
7where T : struct T是value type
8where T : class T是reference type
9where T : new() T具有一个无参数的构造函数
10where T : <base class="" name=""/> T必须是base class name设定的类或者是他的继承类
11where T : <interface name=""> T必须是interface name设定的接口或者它的实现类
12我的疑问是,这个Generic Type的意义何在?如果说忽略了类型,可是,如果没有类型设定,那么T就是object,只能够调用很少的一些方法,如果设定了类型,那么为什么我不直接写呢?何必还要绕一个圈子呢?
13我的回答是,
14第一,对于List<>这样的泛型,如果不使用泛型,要达到目的,就要使用一个array或者是list,如果是array,那么长度就是限定的,不是动态的,这不好,如果是list,那么类型是不知道的,需要在程序中进行编码进行转换,所以,使用了泛型,对么?
15第二,还是保证了一种类型上面的忽略,算法的统一
16
17**2\. Interator 迭代**
18Interator是一个方法,允许foreach在类上进行操作,Iterator代码定义了foreach循环遍历集合中的元素时的返回类型。
19避免了如果一个collection要支持foreach循环必须实现System.Collections.IEnumerable或者System.Collections.IEnumerator
20yield return, 返回值必须是System.Collections.IEnumerable或者System.Collections.IEnumerator
21
22**3\. Anonymous Method 匿名方法**
23btnOK.Click += delegate{MessageBox.Show("OK")}
24btnOK.Click += new EventHandler(object sender, EventArgs e)
25{
26MessageBox.Show(sender.ToString());
27}
28有点把一段代码作为参数传递给delegate
29这可就方便了在RunTime设定运行代码,外部脚本,注入啊,reject
30
31**4\. Partial Class 局部类**
32一个类,可以在一个assembly或者module(exe/dll)的各个cs文件中被不断的补充、定义,这很方便于多个开发员的开发,对于一个存在于几个人写的类中,Partial class还是很有用的,每个人都可以根据自己的需求对Partial Class进行补充扩展
33需要注意的是,第一,只能够在一个assembly或者module中;第二,不能够声明的方式不统一
34
35**5\. Nullable Types 空类型**
36声明的方式可以是
37int? x;
38System.Nullable x;
39很有用,例如,对于Db中的一个field如果要赋值为空,现在的写法可能是***.Value = null;可是这样子对于需要根据情况进行判断是有值还是为空的时候,就需要写两行代码来完成,很罗嗦,麻烦,有了Nullable Types就方便了
40int? x = null;
41int y = 10;
42if (chk.Checked == true)
43{
44x = y;
45}
46else
47{
48x = null;
49}
50***.Value = x;
51
52**6\. :: Operator ::操作符
53** 很有意思的一个东西,其中引入了global关键字,看看就可以了</interface></somestruct></somestruct></float></float></myclass></myclass></t>