c#-泛型基础认知

 1 /// <summary>
 2         /// Name: Generic/泛型
 3         /// .NET Framework 2.O 时CLR升级的
 4         /// 泛型使用:泛型接口/泛型类/泛型方法/泛型委托/泛型
 5         /// 约束:class引用类型约束/struct值类型约束/new()无参数构造函数约束/接口约束/类约束
 6         /// 深入》》》》》》》
 7         /// 范围:只能在接口和委托 c# 4.0出现
 8         /// 泛型的逆变(in)修饰传入参数 作为传入参数 不能作为返回值
 9         /// 泛型的协变 (out) 修饰返回值  作为返回值参数 不能作为传入参数
10         /// </summary>
11         /// <returns></returns>

 

泛型使用:泛型接口/泛型类/泛型方法/泛型委托/泛型(只有这四种情况)

 1     /// <summary>
 2     /// 泛型类
 3     /// 描述:就是在类的后面加上<> ,<>里面有一个占位符,你可以任意定义
 4     /// 可以有多个占位符,如: GenericClass2<T, W, S>
 5     /// </summary>
 6     /// <typeparam name="T">占位符(`)这里的"T"可以任意的自己定义</typeparam>
 7     public class GenericClass<T>
 8     {
 9 
10     }
11 
12     public class GenericClass2<T, W, S>
13     {
14 
15     }
16 
17     public class GenericClass3
18     {
19         /// <summary>
20         /// 泛型方法 
21         /// 描述:返回值/传入的参数都可是泛型
22         /// 返回值是泛型的时候 需要默认的返回T :return default(T);
23         /// </summary>
24         /// <typeparam name="T"></typeparam>
25         /// <param name="t"></param>
26         /// <returns></returns>
27         public T SayHi<T>(T t)
28         {
29             return default(T);
30         }
31 
32         public void SayHi2<T>(T t)
33         {
34 
35         }
36 
37         public T SayHi3<T>()
38         {
39             return default(T);
40         }
41     }
42 
43     /// <summary>
44     /// 泛型接口
45     /// 描述:同泛型类相同
46     /// </summary>
47     /// <typeparam name="T"></typeparam>
48     public interface IMyGeneric<T>
49     {
50 
51     }
52 
53     /// <summary>
54     /// 泛型委托 
55     /// 描述:和方法的类似
56     /// </summary>
57     /// <typeparam name="T"></typeparam>
58     public delegate void Mydelegate<T>();

泛型的约束:class引用类型约束/struct值类型约束/new()无参数构造函数约束/接口约束/类约束

 1     /// <summary>
 2     /// 泛型类
 3     /// 描述:就是在类的后面加上<> ,<>里面有一个占位符,你可以任意定义
 4     /// 可以有多个占位符,如: GenericClass2<T, W, S>
 5     /// 约束:带来了权力,也带来了义务
 6     /// 描述:在类,或者方法的后面 加上where关键字 然后加上泛型的占位符(这里是T)然后加上冒号:冒号后面就是约束的条件
 7     /// 约束条件只有五种:
 8     /// class引用类型约束/struct值类型约束/new()无参数构造函数约束/接口约束/类约束(自己定义的类)
 9     /// 五种约束按照你自己的使用来给方法/类/接口/委托约束
10     /// </summary>
11     /// <typeparam name="T">占位符(`)这里的"T"可以任意的自己定义</typeparam>
12     public class GenericClass<T> where T : class //这里表示class引用类型约束
13     {
14 
15     }
16 
17     public class GenericClass2<T, W, S> where T : class where W : struct where S : new()
18     {
19 
20     }
21 
22     public class GenericClass3
23     {
24         /// <summary>
25         /// 泛型方法 
26         /// 描述:返回值/传入的参数都可是泛型
27         /// 返回值是泛型的时候 需要默认的返回T :return default(T);
28         /// </summary>
29         /// <typeparam name="T"></typeparam>
30         /// <param name="t"></param>
31         /// <returns></returns>
32         public T SayHi<T>(T t) where T : class, new()
33         {
34             return default(T);
35         }
36 
37         public void SayHi2<T>(T t) where T : IMyGeneric<int>
38         {
39 
40         }
41 
42         public T SayHi3<T>()
43         {
44             return default(T);
45         }
46     }
47 
48     /// <summary>
49     /// 泛型接口
50     /// 描述:同泛型类相同
51     /// </summary>
52     /// <typeparam name="T"></typeparam>
53     public interface IMyGeneric<T>
54     {
55 
56     }
57 
58     /// <summary>
59     /// 泛型委托 
60     /// 描述:和方法的类似
61     /// </summary>
62     /// <typeparam name="T"></typeparam>
63     public delegate void Mydelegate<T>() where T : class, new();

泛型的逆变(in)和协变(out)--- 只有委托和接口存在
在实际开发中很少遇到,这里只讲解一下 c#封装的使用到的地方

泛型的协变 (out) 修饰返回值  作为返回值参数 不能作为传入参数

 1 #region 程序集 mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
 2 // C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\mscorlib.dll
 3 #endregion
 4 
 5 using System.Runtime.CompilerServices;
 6 
 7 namespace System.Collections.Generic
 8 {
 9     //
10     // 摘要:
11     //     公开枚举数,该枚举数支持在指定类型的集合上进行简单迭代。若要浏览此类型的.NET Framework 源代码,请参阅参考源。
12     //
13     // 类型参数:
14     //   T:
15     //     要枚举的对象的类型。此类型参数是协变。即可以使用指定的类型或派生程度更高的类型。有关协变和逆变的详细信息,请参阅 泛型中的协变和逆变。
16     [TypeDependencyAttribute("System.SZArrayHelper")]
17     public interface IEnumerable<out T> : IEnumerable
18     {
19         //
20         // 摘要:
21         //     返回一个循环访问集合的枚举器。
22         //
23         // 返回结果:
24         //     用于循环访问集合的枚举数。
25         IEnumerator<T> GetEnumerator();
26     }
27 }

 

 1     /// <summary>
 2     /// 泛型的协变 (out) 修饰返回值  作为返回值参数 不能作为传入参数
 3     /// 解释:就是声明的占位符只能是返回值
 4     /// </summary>
 5     /// <typeparam name="T"></typeparam>
 6     public interface IMyList<out T>
 7     {
 8         T Get(); //这里的返回值只能是这样
 9 
10         //void Get<T>(T t);//不允许这样
11     }
12 
13     public class MyList<T> : IMyList<T>
14     {
15         public T Get()
16         {
17             return default(T);
18         }
19     }

泛型的逆变(in)修饰传入参数 作为传入参数 不能作为返回值

 1 #region 程序集 mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
 2 // C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\mscorlib.dll
 3 #endregion
 4 
 5 namespace System
 6 {
 7     //
 8     // 摘要:
 9     //     封装一个方法,该方法只有一个参数并且不返回值。若要浏览此类型的 .NET Framework 源代码,请参阅引用源。
10     //
11     // 参数:
12     //   obj:
13     //     此委托封装的方法的参数。
14     //
15     // 类型参数:
16     //   T:
17     //     此委托封装的方法的参数类型。此类型参数是逆变。即可以使用指定的类型或派生程度更低的类型。有关协变和逆变的详细信息,请参阅 泛型中的协变和逆变。
18     public delegate void Action<in T>(T obj);
19 }
 1  /// <summary>
 2     /// 泛型的逆变(in)修饰传入参数 作为传入参数 不能作为返回值
 3     /// 解释:就是声明的占位符只能是传入的参数
 4     /// </summary>
 5     /// <typeparam name="T"></typeparam>
 6     public interface IMyList2<in T>
 7     {
 8         //T Get();//不允许这样
 9 
10         void Get<T>(T t); //这里只能是这样,不予许返回值是泛型
11     }
12 
13     public class MyList3<T> : IMyList2<T>
14     {
15         public void Get<T1>(T1 t)
16         {
17             throw new NotImplementedException();
18         }
19     }

以上是我对泛型的理解,如有疑问加我QQ:443069755

欢迎讨论和指教

 

posted @ 2018-03-25 19:41  Torylon  阅读(114)  评论(0编辑  收藏  举报