1.泛型和泛型强制转换
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace VS2005Demo2
6{
7
8 C# 编译器只答应将泛型参数隐式强制转换到 Object 或约束指定的类型#region C# 编译器只答应将泛型参数隐式强制转换到 Object 或约束指定的类型
9 public interface ISomeInterface
10 { }
11 class BaseClass
12 { }
13 class MyClass%26lt;T%26gt; where T : BaseClass, ISomeInterface
14 {
15 void SomeMethod(T t)
16 {
17 ISomeInterface obj1 = t;
18 BaseClass obj2 = t;
19 object obj3 = t;
20 }
21 }
22 #endregion
23
24 编译器答应您将泛型参数显式强制转换到其他任何接口,但不能将其转换到类#region 编译器答应您将泛型参数显式强制转换到其他任何接口,但不能将其转换到类
25 class SomeClass
26 { }
27 //class MyClass1%26lt;T%26gt;
28 //{
29 // void SomeMethod(T t)
30 // {
31 // ISomeInterface obj1 = (ISomeInterface)t; //Compiles
32 // SomeClass obj2 = (SomeClass)t; //Does not compile
33 // }
34 //}
35 #endregion
36
37
38 使用临时的 Object 变量,将泛型参数强制转换到其他任何类型#region 使用临时的 Object 变量,将泛型参数强制转换到其他任何类型
39 class MyClass2%26lt;T%26gt;
40 {
41 void SomeMethod(T t)
42 {
43 object temp = t;
44 SomeClass obj = (SomeClass)temp;
45 }
46 }
47 #endregion
48
49 使用is和as运算符#region 使用is和as运算符
50 public class MyClass3%26lt;T%26gt;
51 {
52 public void SomeMethod(T t)
53 {
54 if (t is int) { }
55 if (t is LinkedList%26lt;int, string%26gt;) { }
56 string str = t as string;
57 if (str != null) { }
58 LinkedList%26lt;int, string%26gt; list = t as LinkedList%26lt;int, string%26gt;;
59 if (list != null) { }
60 }
61 }
62 #endregion
63
64}
65
2.继续和泛型
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace VS2005Demo2
6{
7 继续和泛型#region 继续和泛型
8 public class BaseClass%26lt;T%26gt;
9 { }
10 public class SubClass : BaseClass%26lt;int%26gt;
11 { }
12
13
14 public class SubClass1%26lt;R%26gt; : BaseClass%26lt;R%26gt;
15 { }
16 #endregion
17
18 继续约束#region 继续约束
19 public class BaseClass1%26lt;T%26gt; where T : ISomeInterface
20 { }
21 public class SubClass2%26lt;T%26gt; : BaseClass1%26lt;T%26gt; where T : ISomeInterface
22 { }
23
24 //构造函数约束
25 public class BaseClass3%26lt;T%26gt; where T : new()
26 {
27 public T SomeMethod()
28 {
29 return new T();
30 }
31 }
32 public class SubClass3%26lt;T%26gt; : BaseClass3%26lt;T%26gt; where T : new()
33 { }
34
35 #endregion
36
37 虚拟方法#region 虚拟方法
38 public class BaseClass4%26lt;T%26gt;
39 {
40 public virtual T SomeMethod()
41 {
42 return default(T);
43 }
44 }
45 public class SubClass4 : BaseClass4%26lt;int%26gt;
46 {
47 public override int SomeMethod()
48 {
49 return 0;
50 }
51 }
52
53 public class SubClass5%26lt;T%26gt; : BaseClass4%26lt;T%26gt;
54 {
55 public override T SomeMethod()
56 {
57 return default(T);
58 }
59 }
60
61 #endregion
62
63 接口、抽象类继续#region 接口、抽象类继续
64 public interface ISomeInterface6%26lt;T%26gt;
65 {
66 T SomeMethod(T t);
67 }
68 public abstract class BaseClass6%26lt;T%26gt;
69 {
70 public abstract T SomeMethod(T t);
71 }
72 public class SubClass6%26lt;T%26gt; : BaseClass6%26lt;T%26gt;,ISomeInterface6%26lt;T%26gt;
73 {
74 public override T SomeMethod(T t)
75 { return default(T); }
76 }
77 #endregion
78
79 泛型抽象方法和泛型接口#region 泛型抽象方法和泛型接口
80 //public class Calculator%26lt;T%26gt;
81 //{
82 // public T Add(T arg1, T arg2)
83 // {
84 // return arg1 + arg2;//Does not compile
85 // }
86 // //Rest of the methods
87 //}
88
89 public abstract class BaseCalculator%26lt;T%26gt;
90 {
91 public abstract T Add(T arg1, T arg2);
92 //public abstract T Subtract(T arg1, T arg2);
93 //public abstract T Divide(T arg1, T arg2);
94 //public abstract T Multiply(T arg1, T arg2);
95 }
96 public class MyCalculator : BaseCalculator%26lt;int%26gt;
97 {
98 public override int Add(int arg1, int arg2)
99 {
100 return arg1 + arg2;
101 }
102 //Rest of the methods
103 }
104
105 public interface ICalculator%26lt;T%26gt;
106 {
107 T Add(T arg1, T arg2);
108 //Rest of the methods
109 }
110 public class MyCalculator1 : ICalculator%26lt;int%26gt;
111 {
112 public int Add(int arg1, int arg2)
113 {
114 return arg1 + arg2;
115 }
116 //Rest of the methods
117 }
118 #endregion
119
120}
121
3.泛型方法
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace VS2005Demo2
6{
7
8 泛型方法#region 泛型方法
9 public class MyClass
10 {
11 public void MyMethod%26lt;T%26gt;(T t)
12 { }
13 }
14
15 public class Class3
16 {
17 public void Test()
18 {
19 MyClass obj = new MyClass();
20 obj.MyMethod%26lt;int%26gt;(3);
21
22 obj.MyMethod(3);
23 }
24 }
25 #endregion
26
27 编译器无法只根据返回值的类型推断出类型#region 编译器无法只根据返回值的类型推断出类型
28 public class MyClass1
29 {
30 public T MyMethod%26lt;T%26gt;()
31 { return default(T); }
32 }
33
34 public class Class31
35 {
36 public void Test()
37 {
38
39 MyClass1 obj = new MyClass1();
40 int number = obj.MyMethod%26lt;int%26gt;();
41 }
42 }
43 #endregion
44
45 泛型方法约束#region 泛型方法约束
46 public class Class32
47 {
48 public T MyMethod%26lt;T%26gt;(T t) where T : IComparable%26lt;T%26gt;
49 { return default(T); }
50 }
51 #endregion
52
53 泛型虚拟方法#region 泛型虚拟方法
54 public class BaseClass33
55 {
56 public virtual void SomeMethod%26lt;T%26gt;(T t)
57 { }
58 }
59 public class SubClass33 : BaseClass33
60 {
61 public override void SomeMethod%26lt;T%26gt;(T t)
62 {
63 base.SomeMethod%26lt;T%26gt;(t);
64 }
65 }
66
67 public class BaseClass34
68 {
69 public virtual void SomeMethod%26lt;T%26gt;(T t) where T : new()
70 { }
71 }
72 public class SubClass34 : BaseClass34
73 {
74 public override void SomeMethod%26lt;T%26gt;(T t)// where T : IComparable%26lt;T%26gt;
75 { }
76 }
77
78 public class BaseClass35
79 {
80 public virtual void SomeMethod%26lt;T%26gt;(T t)
81 { }
82 }
83 public class SubClass35 : BaseClass35
84 {
85 public override void SomeMethod%26lt;T%26gt;(T t)
86 {
87 base.SomeMethod%26lt;T%26gt;(t);
88 base.SomeMethod(t);
89 }
90 }
91 #endregion
92
93 泛型静态方法#region 泛型静态方法
94 public class MyClass36%26lt;T%26gt;
95 {
96 public static T SomeMethod(T t)
97 { return default(T); }
98 }
99
100 public class Class36
101 {
102 public void Test()
103 {
104 int number = MyClass36%26lt;int%26gt;.SomeMethod(3);
105 }
106 }
107
108 public class MyClass37%26lt;T%26gt;
109 {
110 public static T SomeMethod%26lt;X%26gt;(T t, X x)
111 { return default(T); }
112 }
113 public class Class37
114 {
115 public void Test()
116 {
117 int number = MyClass37%26lt;int%26gt;.SomeMethod%26lt;string%26gt;(3, "AAA");
118 int number1 = MyClass37%26lt;int%26gt;.SomeMethod(3, "AAA");
119 }
120 }
121
122 public class MyClass38
123 {
124 public static T SomeMethod%26lt;T%26gt;(T t) where T : IComparable%26lt;T%26gt;
125 { return default(T); }
126 }
127
128 #endregion
129}
130
4.泛型委托
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace VS2005Demo2
6{
7 泛型委托#region 泛型委托
8 public class MyClass40%26lt;T%26gt;
9 {
10 public delegate void GenericDelegate(T t);
11 public void SomeMethod(T t)
12 { }
13 }
14
15 public class MyClassTest40
16 {
17 public void Tests()
18 {
19 MyClass40%26lt;int%26gt; obj = new MyClass40%26lt;int%26gt;();
20 MyClass40%26lt;int%26gt;.GenericDelegate del;
21
22 del = new MyClass40%26lt;int%26gt;.GenericDelegate(obj.SomeMethod);
23 del(3);
24
25 //委托推理
26 del = obj.SomeMethod;
27
28 }
29 }
30 #endregion
31
32 委托泛型参数#region 委托泛型参数
33 public class MyClass41%26lt;T%26gt;
34 {
35 public delegate void GenericDelegate%26lt;X%26gt;(T t, X x);
36 }
37
38 //外部委托
39 public delegate void GenericDelegate%26lt;T%26gt;(T t);
40
41 public class MyClass42
42 {
43 public void SomeMethod(int number)
44 { }
45 }
46
47 public class MyClassTest42
48 {
49 public void Test()
50 {
51 MyClass42 obj = new MyClass42();
52 GenericDelegate%26lt;int%26gt; del;
53 //del = new GenericDelegate%26lt;int%26gt;(obj.SomeMethod);
54
55 del = obj.SomeMethod;
56 del(3);
57
58 }
59 }
60
61 #endregion
62
63 委托泛型参数#region 委托泛型参数
64 public delegate void MyDelegate%26lt;T%26gt;(T t) where T : IComparable%26lt;T%26gt;;
65 #endregion
66
67 事件#region 事件
68
69 public delegate void GenericEventHandler%26lt;S, A%26gt;(S sender, A args);
70
71 public class MyPublisher
72 {
73 public event GenericEventHandler%26lt;MyPublisher, EventArgs%26gt; MyEvent;
74 public void FireEvent()
75 {
76 MyEvent(this, EventArgs.Empty);
77 }
78 }
79
80 public class MySubscriber%26lt;A%26gt; //Optional: can be a specific type
81 {
82 public void SomeMethod(MyPublisher sender, A args)
83 { }
84 }
85 public class MyClassTest43
86 {
87 public void Test()
88 {
89 MyPublisher publisher = new MyPublisher();
90 MySubscriber%26lt;EventArgs%26gt; subscriber = new MySubscriber%26lt;EventArgs%26gt;();
91 publisher.MyEvent += subscriber.SomeMethod;
92 }
93 }
94 #endregion
95}
96
记录一下
