泛型学习笔记

匿名 (未验证) 提交于 2019-12-02 22:06:11

泛型的好处:类型安全性能高,代码重用扩展好。

泛型的使用:如果我们需要使用多个泛型来实例化一个类型,那么我们就需要使用说明性的名称,比如TId,TFirstName之类的。

泛型的约束

  where T : struct -类型T必须是值类型

  where T : class -类型T必须是引用类型

  where T : Ifoo -类型T必须执行接口Ifoo

  where T : foo -类型T必须继承自 foo

  where T : new() -类型T必须有一个默认构造函数

  where T : U -指定泛型类型T1派生于T2。即多个泛型类型之间有继承关系

泛型继承:类可以继承自泛型基类,泛型类也可以继承自泛型基类。有一个限制,在继承的时候,必须显示指定基类的泛型类型,型如:public class Child<T>:Base<int>{…}

泛型接口:需要注意的是,在实现接口时需要指定接口的泛型类型。

泛型方法:跟泛型类差不多,方法在定义的时候使用泛型类型定义参数。调用的时候使用实际类型替换。

泛型委托:如果我们需要定义的只是一个功能,但是功能的实现要到具体的地方才能确定,我们就可以使用委托,但是使用委托我们的方法返回值和参数类型就确定了,我们可以让委托具有更高等级的抽象,返回值,参数类型都到具体的地方制定。这里的具体地方就是我们要实现的方法。这样,我们的委托就具有更高级别的抽象。我们设计的类就具有更高级别的可以用性,我们只需要实现方法的细节就可以了。方法的细节怎么实现,可以使用匿名方法,或者lamda表达式。

 

这里举一个自动算工资的例子来讲一下泛型委托。先定义一个员工类SalaryPerson,里面包含了一个薪资累加的方法:

 1 public class SalaryPerson  2 {  3         private int _id;  4   5         public int Id  6         {  7             get { return _id; }  8             set { _id = value; }  9         } 10  11         private string _name; 12   13         public string Name 14         { 15             get { return _name; } 16             set { _name = value; } 17         } 18  19         private decimal _salary; 20   21         public decimal Salary 22         { 23             get { return _salary; } 24             set { _salary = value; } 25         } 26  27         public SalaryPerson() { } 28  29         public SalaryPerson(int id, string name, decimal salry) 30         { 31             this._id = id; 32             this._name = name; 33             this._salary = salry; 34         } 35  36         /// <summary> 37         /// 薪资累加 38         /// </summary> 39         /// <param name="p"></param> 40         /// <param name="d"></param> 41         /// <returns></returns> 42         public static decimal AddSalary(SalaryPerson p, decimal d) 43         { 44             d += p.Salary; 45             return d; 46         } 47 }
View Code

定义泛型委托类GenericDelegate:

 1 public class GenericDelegate  2  {  3         //定义泛型委托  4         public delegate TResult Action<TInput, TResult>(TInput input, TResult result);  5   6         public static TResult GetSalary<TInput, TResult>(IEnumerable<TInput> e, Action<TInput, TResult> action)  7         {  8             TResult result = default(TResult);  9  10             foreach (TInput t in e) 11             { 12                 result = action(t, result); 13             } 14  15             return result; 16         } 17 }

调用代码:

 1             List<SalaryPerson> list = new List<SalaryPerson>();  2             list.Add(new SalaryPerson(1, "Edrick", 5000));  3             list.Add(new SalaryPerson(1, "Meci", 3000));  4             list.Add(new SalaryPerson(1, "Jun", 1000));  5   6             //GenericAndDelegate.Action<SalaryPerson, Decimal> a = new GenericAndDelegate.Action<SalaryPerson, Decimal>((m, n) => n += m.Salary);  7             GenericDelegate.Action<SalaryPerson, Decimal> a = delegate (SalaryPerson sp, decimal s) { return s += sp.Salary; };  8             //等同于 list.ForEach(m => d += m.Salary);  9             decimal d = GenericDelegate.GetSalary<SalaryPerson, Decimal>(list, a); 10             Console.WriteLine(d); 11             Console.Read();

 

参考资料:.net中的泛型全面解析

转载请标明出处:泛型学习笔记
文章来源: 泛型学习笔记
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!