C # basic series-generic

Source: Internet
Author: User

Generics are supported from version 2.0, which is closely integrated with the Il (intermediate language) code in the programming set. With generics, we do not need to write methods and classes with the same functions for different types. just create a method or class.

There is another purpose of using generics to reduce the use of object classes in code. object classes are not of type security, and discovery avoids the packaging and unpacking process during the use of object classes and improves performance.

Format of generic classes: Class myclass <t> {}

Generic method t function <t> ()

Interface myinterfac <t>

Some friends may say, "Why do we use t?" No, you can use B. But this t is almost universally accepted. When there are multiple generic types, we define the conventional header letter txx

  

Security. Packing and unpacking

Why does the generic model improve security and the packing and unpacking process? You can get a simple code.

  

VaR list = new arraylist (); list. Add (44); // boxed list. Add ("mystring"); list. Add (New myclass ());

Int I = (INT) list [0]; // unpack

This list is insecure. If we want to continue the type conversion for it, it will cause an exception. In addition, the Add int type requires packing when the string type is used.

 

Default Value

 

We can use the default keyword to set the default value for the generic type.

public T GetT(){    return default(T);}

  

Constraints

In generics, if we want to restrict some types of generic types, we can use the where keyword to restrict them.

The following table lists the six types of constraints:

 

Constraints

Description

T: struct

The type parameter must be a value type. You can specify any value type except nullable. For more information, see use a type that can be null (C # programming guide ).

T: Class

The type parameter must be of the reference type. This applies to any class, interface, delegate, or array type.

T: New ()

The type parameter must have a public constructor without parameters. When used with other constraints, the new () constraint must be specified at the end.

T: <Base Class Name>

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

T: <Interface Name>

The type parameter must be a specified interface or implement the specified interface. Multiple interface constraints can be specified. The constraint interface can also be generic.

T: u

The type parameter provided for T must be provided for U or derived from U.

class EmployeeList<T> where T : Employee, IEmployee, System.IComparable<T>, new(){    // ...}

This is a multi-class, interface, and various public constructors without parameters.

 

If the stitc variable is used in a generic class, if they assign values to this variable, are they all public static variables. Actually, no. Different generic types use their own static variables.

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.