


Detailed graphic and text introduction to C# generic constraints
本文将详细介绍C# 泛型的约束:引用类型约束;值类型约束;构造函数类型约束;转换类型约束;组合约束的相关知识。具有很好的参考价值,下面跟着小编一起来看下吧
1.引用类型约束
struct RefSample
表示对于的约束必须为一个类(引用类型)不能是值类型(int,char,datatime,struct),可以是接口interface
区分,数组为引用类型,因为定义数组时需要new出一个对象。
虽然定义成 RefSample
2.值类型约束
class ValSample
为引用类型,因为int,char等类型都是struct
ValSample
3.构造函数类型约束
public T CreateInstance<T>() where T:new() { return new T(); }
指定的类型T必须有构造函数,CreateInstance
4.转换类型约束
一种约束允许你指定另一个类型,类型实参必须可以通过一致性、引用或装箱转换隐式地转换为该类型。你还可以规定一个类型实参必须可以转换为另一个类型实参——这称为类型参数约束。
理解的意思:可以互换,就是我们可以通过装箱或者强制类型转换成目标类型的 类型都可以用于类型参数传入。
class Sample
有效:Sample
无效:Sample
struct Sample
规定T必须为IDisposable 类型的 引用类型
有效:Sample
无效:Sample
分析:为什么SqlConnection 可以而StringBuilder不可以?它们都是引用类型
1.SqlConnection实现了IDisposable接口,所以可以协变
2.StringBuilder只实现了ISerializable接口,无法通过途径转换为IDisposable
class Sample
因为将IComparable
typeof(IComparable
有效:Sample
无效:Sample
也可以指定多种约束:
class sample
class Sample
有效:Sample
无效:Sample
总结:要看传入类参数是否可以转换,查看规定参数和传入类参数是否实现同一接口,如果实现则可以,否则不可以。
不可以是以下:System.Object,System.Enum,System.ValueType,System.Delegate,结构或密封类(String)
5.组合约束
对类型参数的约束有多个,注意:只能是一种类型,值类型和引用类型不能同时存在,没用一个类型即是引用类型,又是值类型。
Since every value type has a constructorless function, there can be no constructor constraints thereafter
Valid:
class Sample
class Sample
Invalid:
class Sample
class Sample
class Sample
class Sample
class Sample
class Sample< ;T, U> where T: struct where U: Class, T (The type parameter "T" has the "struct" constraint, so "T" cannot be used as a constraint of "U", so it is invalid)
class Sample
This is also true when I see this version on the InternetValidI don’t understand:
class Sample
##class Sample
Hope you can correct me
The above is C# generics The content of the constraints is introduced in detail with pictures and texts. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.
