C # generic constraints

Source: Internet
Author: User

Six Types of Constraints:

T: Structure

The type parameter must be a value type. You can specify any value type other than Nullable. For more information, see Working with Nullable Types (C # Programming Guide).

T: Class

The type parameter must be a reference type, including any class, interface, delegate, or array type.

T:new ()

The type parameter must have a public constructor with no parameters. When used with other constraints, thenew () constraint must be specified last.

t:< base class name >

The type parameter must be the specified base class or derive from the specified base class.

t:< Interface Name >

The type parameter must be the specified interface or implement the specified interface. You can specify multiple interface constraints. The constraint interface can also be generic.

T:u

The type parameter provided for T must be either a parameter supplied for u or a parameter derived from U. This is called a bare type constraint.

Example:

1. Interface constraints.

For example, you can declare a generic class mygenericclass, so that the type parameter T can implement the Icomparable<t> interface:

 Public class where t:icomparable {}

2. base class constraints.

Indicates that a type must use the specified class as the base class (or the class itself) to be used as a type parameter for the generic type. Once such a constraint is used, it must appear before all other constraints on that type parameter.

class Myclassy<t, u>whereclasswherestruct{}

3. constructor constraints.

To create an instance of the type parameter using the new operator, but the type parameter must be constrained by the constructor constraint new (). The new () constraint allows the compiler to know that any type parameter supplied must have an accessible parameterless (or default) constructor. The new () constraint appears at the end of the WHERE clause.

 Public class where New () {// The following line was not possible without new () constraint:         New T ();}

4. For multiple type parameters, each type parameter uses a WHERE clause.

Interface MyI {} class Dictionary<tkey,tval>where  tkey:icomparable, IEnumerablewhere  tval:myi{  Public void Add (TKey key, Tval val) {}}

5. You can also attach constraints to the type parameters of a generic method.

 Public BOOL where T:imyinterface {}

6. Bare type constraints

A generic type parameter used as a constraint is called a bare type constraint. A bare type constraint is useful when a member function that has its own type parameter needs to constrain the parameter to a type parameter that contains a type.

class List<t>{voidwhere u:t {/*... */ }}

The bare type constraints of a generic class are very limited because the compiler does not make any assumptions other than assuming that a bare type constraint derives from System.Object. You can use a bare type constraint on a generic class in cases where you want to enforce an inheritance relationship between two type parameters.

7.default keywords

The default keyword is used because it is necessary to assign an initial value to an object instance without knowing whether the type parameter is a value type or a reference type. Consider the following code:

class Testdefault<t>    {        public  T foo ()        {            null//  ???            return t;        }    }

If we use the int type to bind the generic parameter, then T is the int, then the comment line becomes int t = NULL; it is obviously meaningless. To solve this problem, the default keyword was introduced:

class Testdefault<t>    {        public  T foo ()        {                return default (T);        }    }

Reference: http://www.jb51.net/article/37658.htm

Http://www.cnblogs.com/ottox/archive/2009/03/02/1401307.html

Http://www.jb51.net/article/41143.htm

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.