The data type is the knowledge that every programmer must study carefully, this article will introduce the novice to understand the data type "sad" course.
There are data types, such as Integer, String, Boolean, time, float, double ... to get to know the entity type, and then the various collection classes that follow. Anyway, they're all derived from the object type:
Background:
ArrayList is an easy-to-use collection class that can be used to store any reference or value type without modification, and any reference or value type added to ArrayList is implicitly cast upward to Object. If the item is a value type, it must be boxed when it is added to the list, and the unboxing operation is performed when it is retrieved. Both casting and boxing and unboxing operations degrade performance, and the effect of boxing and unboxing is noticeable in cases where large collections must be cycled.
The Times Create Heroes:
Because of the limitations of the above data types, this great hero of generics was able to solve the problem that other data types could not provide type parameters.
Characteristics:
A generic Class (genericlist<t>) cannot be used like this because it is not actually a type, but rather a blueprint of a type. To use GENERICLIST<T>, the client code must declare and instantiate the constructed type by specifying the type parameter in angle brackets. The type parameter for this particular class can be any type recognized by the compiler.
Demo:
Using system;using system.collections.generic;using system.linq;using system.text;namespace ConsoleApplication1{ Class Program { private class ExampleClass {} static void Main (string[] args) { //declares an array of shaping genericlist<int > list1 = new genericlist<int> (); List1. ADD (333); Declares an array of strings genericlist<string> list2 = new genericlist<string> (); List2. ADD ("HelloWorld"); } } Declare a generic class public class genericlist<t> {public void Add (T input) { Console.WriteLine ("The value you passed in is:" + input);}}
There is a simple definition of a generic class, and when we need to instantiate different types of objects, we just have to determine the generics exactly.
The effect is as follows:
The result can be seen: the same class, when we have different needs, change the input parameters can achieve the effect we want. This is the more superficial understanding of generics, the constraints on generic type parameters, generic classes, generic delegates will be in the post of Bo article one by one, but also hope to get everyone's attention and advice.
A preliminary understanding of generic type