C # generic constraints

Source: Internet
Author: User

This article will make a simple summary of the various types of generic constraints.

At the beginning of the article, a demo code manuscript is given (modified, demonstrated, and illustrated on this basis). )

mylist<t> {    list<t> List = new list<t> ();    This.list[i] = value;} }}set;   }}

Next, modify the demo code manuscript to illustrate the different types of generic constraints.

1 specifying a generic parameter as a value type

T:struct{... Code omit part}

Look at the effect of adding a constraint, instantiate the MyList class as follows:

MyList<person> list = new MyList<person> ();   

You will notice that the following error message will be generated:

The type "Gencconstraint.person" must be a non-nullable value type to be used as the parameter "T" in the generic type or method "Gencconstraint.mylist<t>".

Instantiate the MyList class using the following method, and everything is OK:

mylist<new mylist<int> (); 

2 specifying a generic parameter as a reference type

T:class {... Code omit part}

After modifying a generic constraint to a reference type, the preceding error message disappears because the person class is a reference type that satisfies the generic constraint.

3 specifying a common constructor for a generic parameter with no arguments

T:new () {... Code omit part}

Add a private parameterless constructor for the person class with the following code:

person{person    (//do Nothing}} 

Instantiate MyList < person > class:

MyList<person> list = new MyList<person> ();   

A compilation error occurred, prompting:

"Gencconstraint.person" must be a non-abstract type with a public parameterless constructor before it can be used as a parameter "T" in a generic type or method "Gencconstraint.mylist<t>".

Haha, the restraints are working.

4 Specifies that generic parameters must be sent to the specified base class

Add abstract class Senioranimal

Senioranimal//Advanced Animal {    Speak ();  Usetool ();  will use the tool}     

Specifies that generic parameters must derive from the base class Senioranimal

senioranimal{... Code omit part}

Instantiate MyList < person > class (at this point the person class has not yet inherited from the Senioranimal Class):

MyList<person> list = new MyList<person> ();   

A compilation error occurred, prompting:

The type "Gencconstraint.person" cannot be used as a type parameter "T" in a generic type or method "Gencconstraint.mylist<t>". There is no implicit reference conversion from ' Gencconstraint.person ' to ' gencconstraint.senioranimal '.

Modify the code so that the person class inherits from the Senioranimal class

senioranimal{    Speak () {Console.WriteLine (Usetool () { Console.WriteLine ("I'll use a machete!" "); }}

Compile again, everything is OK.

5 specifies that the generic parameter must implement the specified interface

If the demo is similar to the 4th, no demo code is provided.

6 specifies that generic parameters must be sent to the generic type U (bare type constraint)

mylist<void showinfo<t> () where T:u {}}

In addition, you can apply multiple constraints to the same generic parameter.

All right, Game over!.

C # generic constraints

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.