泛型

泛型目的:约束容器中能放何种类型的数据,使编程变成类型安全的了。如:
•对比ArrayList和List<T>,ArrayList中的数据放进去以后再拿出来都是object标签,因此需要类型转换,而且很难阻止非法类型的加入。
List<T>则可以指定盛放的数据的类型。
 
 
编写泛型类:
View Code
 1 class MyList<T>
 2     {
 3         private ArrayList list = new ArrayList();
 4 
 5         public T this[int index]
 6         {
 7             get
 8             {
 9                 return (T)list[index];
10             }
11 
12             set
13             {
14                 list[index] = value;
15             }
16         }
17 
18         public void Add(T item)
19         {
20             list.Add(item);
21         }
22 
23         public int Count
24         {
25             get
26             {
27                 return list.Count;
28             }
29         }
30     }

 

View Code
 1  class MyDictionary<K, V>
 2     {
 3         private Hashtable data = new Hashtable();
 4 
 5         public V this[K key]
 6         {
 7             get
 8             {
 9                 return (V)data[key];
10             }
11             set
12             {
13                 data[key] = value;
14             }
15         }
16 
17         public void Add(K key, V value)
18         {
19             data.Add(key, value);
20         }
21 
22         public ICollection<K> Keys
23         {
24             get
25             {
26                 List<K> list = new List<K>();
27                 foreach (object obj in data.Keys)
28                 {
29                     list.Add((K)obj);
30                 }
31                 return list;
32             }
33         }
34     }

 

•泛型方法:方法也可以定义泛型,在方法名后用<>将类型标注即可
 static IList<T> ToList<T>(ArrayList list)
        {
            IList<T> retList = new List<T>();
            foreach (object obj in list)
            {
                retList.Add((T)obj);
            }
            return retList;
        }定义成扩展方法
•(dropped).net内置的两个常用的类型转换的泛型方法,
所有实现了IEnumerable接口的对象都可以调用:Cast<T>将源序列转换为指定类型的范型序列;
OfType<T>将序列中类型为T的数据挑出来,结果以T类型序列返回。
 
将非泛型的数据转换为泛型的Cast或ofType方法:
View Code
 1 static void Main(string[] args)
 2         {
 3             ArrayList list = new ArrayList();
 4             list.Add(1);
 5             list.Add(2);
 6             list.Add(8);
 7 
 8             var e1 = list.Cast<int>();
 9             
10             //Hello2<int,string>(
11 
12             Console.ReadKey();
13         }

 泛型委托:

.Net框架为我们提供了一些常用的委托,Func<>()、........等;

View Code
 1 class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             My1Delegate m1 = H1;
 6 
 7             My2Delegate<string, int> m2 = H1;
 8 
 9            // m1 = H2;
10             My2Delegate<bool, int> m3 = H2;
11 
12             //Fuckway
13             Func<int> f1 = H3;
14             Func<string> f2 = H4;
15 
16             //Func<int,string,bool>
17         }
18 
19         static int H3()
20         {
21             return 0;
22         }
23 
24         static string H4()
25         {
26             return "";
27         }
28 
29         static void H1(string s1, int i2)
30         {
31         }
32 
33         static void H2(bool b, int i)
34         {
35         }
36     }
37 
38     delegate void My1Delegate(string s1,int i2);
39 
40     delegate void My2Delegate<T1,T2>(T1 t1,T2 i2);

 泛型约束:

View Code
 1    class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             //Person p1 = new Person();
 6             CreateList<Dog>(3);
 7             //CreateList<Person>(5);
 8         }
 9 
10         //where T:new()约束T必须有无参的构造函数
11         //约束对于泛型委托、泛型类都是一样的
12         static List<T> CreateList<T>(int n) where T:new()
13         {
14             List<T> list = new List<T>();
15             for (int i = 0; i < n; i++)
16             {
17                 //因为类可能没有无参的构造函数,所以不能保证一定成功
18                 //进行约束后,通过编译
19                 T p = new T();
20                 list.Add(p);
21             }
22             return list;
23         }
24     }
25 
26     class Person
27     {
28         public Person(int age)
29         {
30             this.Age = age;
31         }
32         public int Age { get; set; }
33     }
34 
35     class Dog
36     {
37         //public Person(int age)
38         //{
39         //    this.Age = age;
40         //}
41         public int Age { get; set; }
42     }

泛型约束之继承:

View Code
 1  class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5            // PersonList<int> list1 = new PersonList<int>();//不行
 6             PersonList<Chinese> listc = new PersonList<Chinese>();//OK
 7         }
 8     }
 9 
10     class PersonList<T> where T:Person //约束了T必须继承自Person
11     {
12         public void DotIt(T p1)
13         {
14             //p1.Name //因为约定了T必须继承自Person,所以可以在类内部调用Person的成员
15         }
16     }
17 
18     class Person
19     {
20         public string Name { get; set; }
21     }
22 
23     class Chinese : Person
24     {
25         public string HuKou { get; set; }
26     }
27 
28     class Dog
29     {
30     }


泛型约束的应用,用户自定义控件的事件中:

View Code
 1 public partial class UserControl1 : UserControl
 2     {
 3         public UserControl1()
 4         {
 5             InitializeComponent();
 6         }
 7 
 8         private void button1_Click(object sender, EventArgs e)
 9         {
10             if (OnSanQiang != null)
11             {
12                 Point p1 = new Point();//...
13                 SanQiangArgs args = new SanQiangArgs();
14                 args.CurPoint = p1;
15                 OnSanQiang(this, args);
16             }
17         }
18 
19         //public event SanQiangDelegate OnSanQiang;
20         public event EventHandler<SanQiangArgs> OnSanQiang;
21     }
22 
23     public delegate void SanQiangDelegate(object sender,Point p);
24 
25     public class SanQiangArgs : EventArgs//待定类型必须继承自EventArgs
26     {
27         public Point CurPoint{get;set;}
28     }

 

//定义自己的可空类型
    class Program
    {
        static void Main(string[] args)
        {
            //int? i1 = 0;
            //i1.

            //int?编译器会翻译成Nullable<int>类型

            MyNullable<int> i1 = new MyNullable<int>();
            i1.HasValue = true;
            i1.Value = 3;
        }
    }

    class MyNullable<T> where T : struct
    {
        public bool HasValue { get; set; }
        public T Value { get; set; }
    }

 

409号

 

posted @ 2013-03-17 19:13  Big.Eagle  阅读(108)  评论(0)    收藏  举报