码迷,mamicode.com
首页 > Windows程序 > 详细

06.C#泛型约束和高级泛型(三章3.3-3.4)

时间:2014-12-28 12:55:59      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:

  吃午饭前继上篇泛型再写一篇关于泛型的文章,虽然总是被博客园移出首页,文章不精确实是大问题啊,会再接再厉的。进入正题。

  先来说下泛型约束。当我们在使用泛型的时候通常会希望使用泛型实参时,参数能具备某一些特性,这时"泛型约束"来了,它能帮助我们在传入泛型参数,该参数要实现先前指定的约束。有4种约束可用,如下:

  1. 引用类型约束:确保使用的类型参数是引用类型(T:class,且必须是类型参数指定的第一个约束),类型实参任何类、接口、数组、委托、或者已知是引用类型的另一个类型参数。
  2. 值类型约束:表示成(T:struct),可以确保使用的实参是值类型,包括枚举(enums),但是它将可空类型排队在外。
  3. 构造类型约束:表示成(T:new()),必须是约束的最后一个约束,它检查类型是否具有一个可用于创建实例的无参构造函数。
  4. 转换类型约束:允许你指定另一个类型,类型参数可以通过一致性引用或隐式转换或装箱转换转换为该类型。
1 public struct DoDo<T> where T : class
2 {}
3 public class BiBi<T> where T : struct
4 {}
5 public struct CoCo<T> where T : new()
6 {}
7 public struct DkDk<T> where T : System.Data.IDbConnection
8 {}

  上述四个分别对应四种类型约束(仅仅在单个使用),下面来说下在组合使用的情况。

  组合使用有两点要注意:

  1.  如果存在多个轮换类型约束,并且其中一个是类,那么它应该出现在接口的前面,不能多次指定同一个接口。
  2. 当包含多个类型参数时,每一个类型约束都单独使用一个where。
 1 //不能同时指定类型参数既是引用类型又是值类型
 2 //public struct DoDo<T> where T : class, struct
 3 //{}
 4 
 5 public class BiBi<T> where T : Stream, new()
 6 {}
 7 
 8 //使用构造类型约束,new()要放在最后
 9 //public class LiLi<T> where T : new(), Stream
10 //{}
11 
12 public struct CoCo<T> where T : class, IDisposable , new()
13 {}
14 
15 //如下一个约束是类,要放在接口的前面
16 //public struct FeFe<T> where T : IDisposable, class, new()
17 //{}

  使用泛型约束知识都在上面了,更多的去理解和消化。在使用泛型方法时,让编译器是推断能让我们的代码更简短,但可读性可能不高。

  ------------------------------------------------------------------------------------------------------------------------------------------

  Point 1 关于静态字段和静态构造方法,每一个封闭类型有一个静态字段,如果有的话

1 StaticConstraint<int> intStatic = new StaticConstraint<int>();
2 StaticConstraint<int>.StaticValue = "int";
3 
4 StaticConstraint<double> doubleStatic = new StaticConstraint<double>();
5 StaticConstraint<double>.StaticValue = "double";
6 
7 StaticConstraint<char> charStatic = new StaticConstraint<char>();
8 StaticConstraint<char>.StaticValue = "char";

   Point 2 使用typeof获取类型

  typeof可通过两种方式作用于泛型类型,一种用来获取未绑定泛型类型,一种用来获取特定的已构造类型。前一种需要提供泛型的名称,去除所有类型参数的名称,但要保留逗号,后一种需要采取与声明泛型类型变量相同的方式指定类型参数即可。

  

 1 static void GetTypeOfConstraint<X>()
 2 {
 3         //获取未绑定泛型类型
 4         Console.WriteLine(typeof(X));
 5         Console.WriteLine(typeof(List<>));
 6         Console.WriteLine(typeof(Dictionary<,>));
 7 
 8         //获取已构造类型
 9         Console.WriteLine(typeof(List<int>));
10         Console.WriteLine(typeof(Dictionary<int, double>));
11 }

  请斧正。

 

06.C#泛型约束和高级泛型(三章3.3-3.4)

标签:

原文地址:http://www.cnblogs.com/a2htray/p/4189831.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!