Analyze the creation and use of C #2.0 generic classes (2)

Source: Internet
Author: User
3. Implement a generic class

A reasonable way to implement a new function is to build on the original thing. We already know about strong type sets and know that a good technique for building generic classes is to use a specific class and delete data types. That is to say, Let's define a strong type set customerlist, and let's take a look at what it will convert into a generic class.

List 2 defines a class customerlist. The following section converts customerlist to list <t>.

List 2 Definition class mermerlist:

 

Using system;
Using system. collections;
Using system. text;
Namespace generics {
Public class customerlist: collectionbase {
Public customerlist (){}
Public customer this [int Index] {
Get {return (customer) list [Index];}
Set {list [Index] = value ;}
}
Public int add (customer value)
{Return list. Add (value );}
}
}

Iv. Define class Headers

If we define a generic class, we need to convert the Class header into a generic class. All we need to do is to name parameters and change the class name to a certain generic type. List <t> only has one parameter T, and because we work in a backward compatible way, we know that the class name is list. List 3 shows the new Class header of the class in List 2.

List 3 a generic class header shows the parameterized parameter T.

Using system;
Using system. collections;
Using system. text;
Namespace generics {
Public class list <t>: collectionbase {}

5. Implement generic fields

If we need to convert any field to a generic field, we simply need to change their type to T (or any parameter described by this field ). A generic list does not need any fields, but we suppose there is a private integer field named foo-and we will make it generic. We will redefine it as follows:

Private t Foo;

When the parameter T is filled in the class, list T will also be filled in because of foo.

6. Define generic methods

Next, we will define other features for the required parameterized type. This includes attributes, methods, and events. In our instance, we replace each place where the customer appears with the t parameter. The generic list class is displayed in list 4.

List 4 a lightweight parameterized generic list class based on system. Collections. collectionbase.

Using system;
Using system. collections;
Using system. text;
Namespace generics {
Public class list <t>: collectionbase {
Public list (){}
Public t this [int Index] {
Get {return (t) list [Index];}
Set {list [Index] = value ;}
}
Public int add (T value ){
Return list. Add (value );
}
}
}

To test the custom list, comment out the use of the system. Collections. Generic namespace and use list <t> in list 4CodeIt will work in the same way.

Complete modification. the net list <t> is unnecessary and contains far more features than our example. However, list 4 shows how easy this mechanism is to define custom generic classes.

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.