Generic (C # programming guide)

Source: Internet
Author: User
C # programming guide: Http://msdn.microsoft.com/zh-cn/library/512aeb7t (vs.80). aspx Generic (C # programming guide)

Generic is a new function in the C # language and Common Language Runtime Library (CLR) of version 2.0. Generic Type introduces the concept of type parameters to. NET Framework. type parameters make it possible to design the following classes and Methods: these classes and Methods delay the specified one or more types to the client.CodeWhen declaring and instantiating this class or method. For example, by using the generic type parameter T, you can write a single class that can be used by other client code without introducing the cost or risk of force conversion or packing during runtime, as shown below:

C # copy code
 // Declare the generic class  Public   Class Genericlist <t> { Void Add (T input ){}} Class Testgenericlist { Private  Class Exampleclass {} Static   Void Main (){ // Declare a list of Type int Genericlist < Int > List1 = New Genericlist < Int > (); // Declare a list of Type string Genericlist < String > List2 = New Genericlist < String > ();// Declare a list of Type exampleclass Genericlist <exampleclass> list3 = New Genericlist <exampleclass> ();}}
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 through reflection at runtime.

Related chapters

For more information:

    • Generic introduction (C # programming guide)

    • Advantages of generics (C # programming guide)

    • Generic parameters (C # programming guide)

    • Type parameter constraints (C # programming guide)

    • Generic class (C # programming guide)

    • Generic interface (C # programming guide)

    • Generic method (C # programming guide)

    • Generic delegation (C # programming guide)

    • Default keywords in generic code (C # programming guide)

    • Differences between C ++ templates and C # generics (C # programming guide)

    • Generic and reflection (C # programming guide)

    • Generics in the Runtime Library (C # programming guide)

    • Generic In the. NET Framework class library (C # programming guide)

    • "Generic" example (C #)

C # Language Specification

For more information, see the following sections in the C # Language Specification:

    • 20 generic

See Reference

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.