Differences between generic and non-generic types ., Differences between generics

Source: Internet
Author: User

Differences between generic and non-generic types ., Differences between generics

Generic set

Lisit <>
Advantages
1. High Performance
Non-generic collection classes are used for value types. When converting a value type to a reference type or converting a reference type to a value type, you need to bind and unbox the value type. The packing and unpacking operations are easy to implement, but the performance loss is large. If the generic type is used, the packing and unpacking operations can be avoided.
This is a collection.
ArrayList list = new ArrayList ();
List. Add (20); // boxed. The list stores object-type Elements and must convert the value type to the reference type.
Int I = (int) list [0]; // unpack. The type of list [0] is object. To assign a value, you must convert the reference type to the value type.
This is a generic set.
List <T> list = new List <int> ();
List. Add (20); // because int is specified for instantiation, you do not need to pack
Int I = list [0]; // Similarly, you do not need to unpack during access.
2. type security.
Like the Arraylist class, if you use an object, you can add any type to this set. If you use non-generic programming, the following code may cause exceptions in some cases.
A non-generic set.
ArrayList list = new ArrayList ();
List. Add (20 );
List. Add ("string ");
List. Add (new MyClass ());
Foreach (int I in list)
{
Console. WriteLine (I); // there is an exception here, because not all elements in the set can be converted to int
}
If you use generic programming, you can avoid this exception and let the compiler check for errors.
List <int> list = new List <int> ();
List. Add (20 );
Lsit. Add ("string"); // an error is reported during compilation. Only the integer type can be added to the set.
List. Add (new MyClass (); // same as above
3. Reuse of binary code.
Generics can be defined once and instantiated using many different types. Generics can be defined in one language and used in another. NET language.
4. Code extension.
Because the definition of a generic class is placed in a program set, the value type is included in the memory of the instantiated generic class. Each value type has different requirements on memory, so a new class should be instantiated for each value type.
Ii. Features
1. default value.
When initializing type T, be sure not to assign null to the generic type. Because the generic type can also be instantiated as the value type, and null can only be used to reference the type. To solve this problem, you can use the default keyword. Use the default keyword to assign null to the reference type and 0 to the value type.
Public T GetDoucumet ()
{
T doc = default (T );
Lock (this)
{
Doc = documentQueue. Dequeue ();
}
Return doc;
}
2. Constraints.
If a generic class needs to call a method on a generic type, you must add constraints. Note: You can also combine multiple constraints using generic types. Where T: IFoo, new () Constraint and MyClass <T> Declaration are specified. The type T must execute the IFoo interface and have a default constructor.
3. Inheritance.
A generic type can execute a generic interface or be derived from a class. Generic classes can be derived from generic base classes:
Public class Base <T>
{}
Public class Derived <T>: Base <string>
{}
The generic type of the interface must be repeated, or the type of the base class must be specified.
Therefore, a derived class can be a generic class or a non-generic class. For example, you can define an abstract generic base class, which is implemented with a specific type in the derived class.
4. Static members.
Special attention is required for static members of generic classes. Static members of a generic class can only be shared in one instance of the class.
Public class StaticDemo <T>
{
Public static int x;
}
The StaticDemo <T> class is used for a string type and an int type, so there are two sets of static fields:
StaticDemo <string>. x = 4;
StaticDemo <int>. x = 5;
Console. WrileLine (StaticDemo <string>. x); // output 4
Summary (advantages and features of generics ):

High performance, type security, binary code reuse, and code extension.

Default Value, inheritance, constraint, and static member.

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.