C # Essence [original by month] C # What are the benefits of generics?

Source: Internet
Author: User

Note: Prepare a series of key points in C # language. This series is not sequential, but it should be refined as much as possible. It may be added, deleted, and sorted continuously. The most primitive source of this series is the csdn blog. Thank you for your attention.

C # Essence

Third, what are the benefits of C # generics?

Author: qingyueer

Home page:Http://blog.csdn.net/21aspnet/Time: 2007.3.24

AboutObjectType:
1. The object type can be used to reference any type of instances;
2. The object type can store any type of value;
3. Parameters of the object type can be defined;
4. You can use the object as the return type.
But there is a big problem in doing so.

1. errors may occur because the programmer does not remember the type used, resulting in type incompatibility;
2. Mutual binning of value type and reference type reduces system performance.

In C #2.0, the generic type is proposed to avoid forced type conversion, reduce packing and unpacking, and improve performance and reduce errors.

The system. Collections. Generic namespace provides generic versions of many collection classes and interfaces.

Definition:
Public class genericlist <t>
{
Public void add (T input) // T is set as a type parameter
Public t add () // T is set to return value
}
<T> T is a type parameter, which acts as a placeholder,Replaced by real types during compilation.

Use generic:
Genericlist <int> list1 = new genericlist <int> ();
Genericlist <string> list2 = new genericlist <string> ();
Genericlist <Class Name> list3 = new genericlist <Class Name> ();
Genericlist <class name <int> list4 = new genericlist <class name <int> ();
Using list1 as an example, the compiler generates the following methods:
Public void add (INT input)
Public int add ()

Generic classes with multiple type parameters:
Public class name <t, u>

Generic constraints:
Make sure that the parameters used by generic classes are the types that provide specific methods.
Public class genericlist <t> where T: iemployee
If the iemployee Interface contains the method, the compiler will verify that the type used to replace T must implement the iemployee interface.

Generic method: This method can be used to define generic classes.

// Define the generic Method
Static void swap <t> (ref t LHS, ref t RHs)

{T temp; temp = LHS; LHS = RHS; RHS = temp ;}
// Use the generic Method

public static void TestSwap(){    int a=1,b=3;
Swap<int>(ref a,ref b);


string s1="Hello",s2="world";
Swap<string>(ref s1,ref s2);}
There are generic classes, generic interfaces, generic methods, generic delegation

 

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.