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

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
主站蜘蛛池模板: 西充县| 太白县| 游戏| 商水县| 枝江市| 裕民县| 保康县| 名山县| 淅川县| 和硕县| 铁岭市| 江安县| 大安市| 盐津县| 阿克| 宁乡县| 金秀| 连江县| 乡城县| 介休市| 枞阳县| 县级市| 湘阴县| 新野县| 随州市| 罗山县| 南平市| 京山县| 栖霞市| 平潭县| 萝北县| 霍山县| 黄浦区| 闽侯县| 乡宁县| 微博| 文山县| 黄山市| 湾仔区| 陕西省| 湘潭县|