A detailed description of the constraint graphic for C # generics

Source: Internet
Author: User
This article will detail the constraints of C # generics: Referential type constraints, value type constraints, constructor type constraints, transformation type constraints, and related knowledge of composite constraints. Have a good reference value, follow the small series together to see it

1. Reference type constraints

The struct refsample<t> where T:class reference type represents the constraint with class, and the other reference type is the specific constraint.

Indicates that the constraint must be a class (reference type) that cannot be a value type (int,char,datatime,struct), which can be an interface interface

The array is a reference type because a new object is required to define the array.

Although defined as refsample<t>, the incoming must be a reference type but Refsample is still a value type

2. Value type constraints

Class valsample<t> where t:struct

is a reference type because the types such as Int,char are struct

Valsample<int>

3. Constructor type constraints

Public T createinstance<t> () where T:new () {  return new T ();}

The specified type T must have constructors,createinstance<int> and createinstance<object> are valid. However, Createinstance<strings> does not have a constructor function.

4. Conversion type constraints

One constraint allows you to specify another type, and the type argument must be implicitly convertible to that type through a consistency, reference, or boxing conversion. You can also specify that one type argument must be convertible to another type argument-this is called a type parameter constraint.

To be understood: interchangeable, that is, the type that we can convert to the target type by boxing or coercion type can be used for type parameter passing in.

Class sample<t> where T:stream

Effective:sample<stream> this itself conforms to the constraints

Invalid:sample<string> because the String type cannot be cast to stream by reference or boxing unboxing,

struct sample<t> where t:idisposable

Specifies that T must be a reference type of type IDisposable

Valid:sample<sqlconnection> reference conversions

Invalid:sample<stringbuilder>

Analysis: Why SqlConnection can and StringBuilder not? They are all reference types

The 1.SqlConnection implements the IDisposable interface, so it can be covariant

2.StringBuilder only implements the ISerializable interface and cannot be converted to IDisposable via the path

Class sample<t> where t:icomparable<t>

Because the icomparable<t> is treated as a constraint, the type of the icomparable<t> can be evaluated with Type.isvaluetype, true as the value type, and false as the reference type

typeof (Icomparable<t>). Isvaluetype result is false for reference type

Valid:sample<int> (boxing conversion)

Invalid:sample<fileinfo>

You can also specify multiple constraints:

Class sample<t> where t:stream,ienumerable<string>,icomparable<int>

Class sample<t,u> where t:u

Effective:sample<stream,idisposable>

Invalid:sample<string,idiposable>

Summary: To see whether the passed-in class parameters can be converted, to see whether the specified parameters and incoming class parameters implement the same interface, if the implementation is possible, otherwise not.

Cannot be the following: System.object,system.enum,system.valuetype,system.delegate, struct or sealed class (String)

5. Combining constraints

There are multiple constraints on type parameters, note: Only one type, value type and reference type cannot exist at the same time, no one type is a reference type, and the value type.

Since each value type has a non-constructor, there is no longer a constructor constraint

Effective:

Class sample<t> where T:class,idisposable,new ()
Class sample<t,u> where T:stream where u:idispsable

Invalid:

Class sample<t> where t:class,struct (no type of immediate reference type is a value type, so it is invalid)

Class sample<t> where T:stream,class (the reference type constraint should be the first constraint, put at the top, so the invalid) stream just constrains the incoming parameter to the stream concrete type , And the class constraint is a reference type, and at first I understood it wrong.
Class sample<t> where T:new (), Stream (new () must be placed last )

Class sample<t> where T:idisposable,stream ( classes must be placed in front of the interface, so it is invalid )

Class sample<t> where t:xmlreader,icomparable,icomparable ( the same interface cannot appear multiple times for a conversion type constraint )

Class sample<t,u> where t:struct where u:class,t ( type parameter "T" has a "struct" constraint, so "T" cannot be used as a "U" constraint, so it is invalid )

Class sample<t,u> where t:stream, u:idisposable syntax error

It's also valid to see that this version is available on the Internet. I do not understand:

Class sample<t> where T:struct,idisapsable idisapsable is a value type?

Class sample<t,u> where T:class where u:struct, T T is the reference type constrained U with the value type?

I hope I can correct

The above is the C # generic constraint graphic details of the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.