官术网_书友最值得收藏!

How it works...

Speaking of generic interfaces, we have seen that we can implement behavior on a generic class by implementing a generic interface. The power of using the generic class and generic interface is well illustrated earlier.

Having said that, we do believe that knowing when to use constraints is also important so that you can close down your generic classes to only accept specific types that you want. This ensures that you don't get any surprises when someone accidentally passes an integer to your generic class.

Finally, the constraints that you can use are as follows:

  • where T: struct: The type argument must be any value types
  • where T: class: The type argument must be any reference types
  • where T: new(): The type argument needs to have a parameterless constructor
  • where T: <base class name>: The type argument must derive from the given base class
  • where T: <T must derive from object>: T The type arg was must derive must derive from the object after the colon
  • where T: <interface>: The type argument must implement the interface specified
主站蜘蛛池模板: 双辽市| 资兴市| 柏乡县| 漳州市| 沙洋县| 海门市| 台安县| 武川县| 错那县| 浙江省| 大城县| 威远县| 淳安县| 大竹县| 栾川县| 双城市| 石河子市| 甘洛县| 本溪市| 滕州市| 固原市| 明光市| 云龙县| 垣曲县| 澎湖县| 大石桥市| 宜丰县| 天门市| 柞水县| 沙湾县| 清水县| 清镇市| 松桃| 搜索| 长宁区| 汉川市| 诏安县| 高邮市| 溧阳市| 胶南市| 黔南|