你希望你的泛型类型在创建的时候必须有一个支持某个接口(比如IDisposable接口)成员的类型参数。
解决方法:
使用约束去强制泛型的类型参数实现某一个或多个特定的接口。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
这个DisposableList类只允许实现了IDisposable接口的对象作为参数传递给它。那是因为,每次你从DisposableList对象中删除一个对象时,都要调用Dispose方法。这就使得你可以轻松管理DisposableList中的对象。
下面的代码就运用了DisposableList对象:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
讨论:
Where关键字被用来约束类型参数只能接受那些满足给定约束的参数。比如,DisposableList有一个约束就是任何类型参数T必须实现IDisposable接口:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
这意味着,下面的代码将编译成功:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
但这个就不行了:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
这是因为string并没有实现IDisposable接口,而StreamReader实现了。
除了要求一个或多个接口被实现,其他对于类型参数的约束也是被允许的。你可以要求一个类型参数必须继承自一个特定的基类,比如Textreader类:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
你也可以约束这个类型参数只能是值类型或者只能是引用类型。下面声明的类的约束是其只能使用值类型:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
而这个是只能使用引用类型:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
除此之外,你也可以要求任何类型参数去实现一个公共的默认构造函数:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
使用约束使得你可以编写的泛型只能接受那些更具体的有用的参数类型。如果在这个示例中,IDsiposable约束被省略,那么一个编译时错误将会发生。这是因为,不是所有的类型它都可以被用作是实现了IDisposable接口的DisposableList类的参数的。如果你忽略这个编译时检查,一个DisposableList对象也许会包含那些并没有一个公共的无参数的Dispose方法的对象。在这种情况下,一个运行时异常将会发生。泛型和约束在一定程度上要求严格的参数类型检查,这使你在编译时就可以发现这些问题,而不是运行时。