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

  • C# 7 and .NET Core Cookbook
  • Dirk Strauss
  • 174字
  • 2021-07-03 00:12:00

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
主站蜘蛛池模板: 怀化市| 奈曼旗| 武邑县| 平南县| 洱源县| 噶尔县| 辽阳县| 溧水县| SHOW| 北辰区| 白朗县| 枞阳县| 巴彦淖尔市| 军事| 茶陵县| 商河县| 田林县| 苏尼特右旗| 吉安市| 阿拉善左旗| 蒙自县| 望城县| 涟水县| 九龙县| 白水县| 诸城市| 什邡市| 嘉义市| 浦江县| 永德县| 锦屏县| 敦煌市| 江孜县| 柳江县| 扶沟县| 广东省| 汕尾市| 旺苍县| 屯昌县| 通州市| 城固县|