- 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
推薦閱讀
- Vue.js 3.x從入門到精通(視頻教學版)
- PyTorch自然語言處理入門與實戰
- Python網絡爬蟲從入門到實踐(第2版)
- Java開發入行真功夫
- INSTANT MinGW Starter
- Mastering Scientific Computing with R
- Python Network Programming Cookbook(Second Edition)
- Python高效開發實戰:Django、Tornado、Flask、Twisted(第3版)
- Mastering ServiceNow(Second Edition)
- INSTANT Django 1.5 Application Development Starter
- Instant Ext.NET Application Development
- 微信小程序全棧開發技術與實戰(微課版)
- ServiceNow:Building Powerful Workflows
- Azure Serverless Computing Cookbook
- 微信小程序開發實戰:設計·運營·變現(圖解案例版)