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

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
主站蜘蛛池模板: 陈巴尔虎旗| 皋兰县| 屯昌县| 鱼台县| 邵阳市| 伊宁市| 英山县| 灵丘县| 泽库县| 贵定县| 凤翔县| 青浦区| 巴东县| 福贡县| 蒙自县| 开阳县| 特克斯县| 铜山县| 浮山县| 浮山县| 保定市| 山阴县| 赤壁市| 静安区| 房山区| 吉林市| 金塔县| 托克托县| 虎林市| 伽师县| 通州市| 且末县| 大理市| 集贤县| 白山市| 泰兴市| 桓台县| 凤翔县| 宣恩县| 龙门县| 宜都市|