C # generics

Source: Internet
Author: User

Generic Overview:
You can use generic types to maximize code reuse, protect type security, and improve performance.

The most common use of generics is to create a collection class.

The. NET Framework class library contains several new Generic collection classes in the System. Collections. Generic namespace. Use these classes as much as possible to replace common classes, such as the ArrayList in the System. Collections namespace.

You can create your own generic interfaces, generic classes, generic methods, generic events, and generic delegation.

You can restrict generic classes to access specific data types.

Information about types used in generic data types can be obtained at runtime through reflection.

Test code:

[Csharp]
// Declare the generic class.
Public class GenericList
{
Void Add (T input ){}
}
Class TestGenericList
{
Private class ExampleClass {}
Static void Main ()
{
// Declare a list of type int.
GenericList List1 = new GenericList ();
 
// Declare a list of type string.
GenericList List2 = new GenericList ();
 
// Declare a list of type ExampleClass.
GenericList List3 = new GenericList ();
}
}

// Declare the generic class.
Public class GenericList
{
Void Add (T input ){}
}
Class TestGenericList
{
Private class ExampleClass {}
Static void Main ()
{
// Declare a list of type int.
GenericList List1 = new GenericList ();

// Declare a list of type string.
GenericList List2 = new GenericList ();

// Declare a list of type ExampleClass.
GenericList List3 = new GenericList ();
}
}


 

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.