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

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
主站蜘蛛池模板: 花莲市| 武隆县| 综艺| 佳木斯市| 尤溪县| 海兴县| 安西县| 平潭县| 高要市| 吉木乃县| 进贤县| 滨海县| 紫阳县| 敦煌市| 富川| 辽宁省| 新沂市| 马鞍山市| 肥城市| 贺州市| 绥德县| 枝江市| 萨嘎县| 景德镇市| 太仆寺旗| 望谟县| 扎兰屯市| 兴宁市| 安平县| 伊宁市| 台东县| 瑞金市| 大名县| 抚宁县| 荥经县| 宣恩县| 斗六市| 乐业县| 诸城市| 昌平区| 托里县|