At first I thought the book was too thick, until I saw the chapter of generics, and found that this book was not enough, after all, it is impossible to tell a complicated concept without a certain amount of space.
The concept of generics troubled me for a long time, so I took this chapter over and over again, and went online to find some articles, sort of understand.
Let's take a look at the concept of generics: "Using parameterized types to manipulate multiple data types on the same piece of code." Use the parameterized type to abstract the type for flexible reuse. So, with generics, you can create classes and methods that are independent of the containing type. You do not have to write many methods or classes of the same functionality for different types, just create a method or class.
Talk less, look at the code:
1, without the consequences of generics
We create a method to sort an array of int, the algorithm is bubbling:
Public classSorthelper { Public voidBubblesort (int[] arr) { intLength =arr. Length; for(inti =0; I < length-1; i++) { for(intj =0; J < length-1-I.; J + +) { if(arr[j]>arr[j+1]) { inttemp=Arr[j]; ARR[J]= Arr[j +1]; Arr[j+1] =temp; } } } } }
Test:
Static void Main (string[] args) { new sorthelper (); int 4,5,1,3,2,8,5,0,2 }; Sorter. Bubblesort (a); // output omitted }
output is:0,1,2,2,3,4,5,5,8
If the array is of type Byte, simple, direct copy method, change the signature to be finished
Public classSorthelper { Public voidBubblesort (byte[] arr) { intLength =arr. Length; for(inti =0; I < length-1; i++) { for(intj =0; J < length-1-I.; J + +) { if(arr[j]>arr[j+1]) { bytetemp =Arr[j]; ARR[J]= Arr[j +1]; Arr[j+1] =temp; } } } } }
If the introduction of other data types of methods, then this class can not see, long do not say, maintenance is also very troublesome, if I want to change the bubble to fast, then every method to change, then how to do it, this time can introduce generics.
2, using generics
in short, use "T" instead of specific type
Public classSorthelper { Public voidBubblesort (t[] arr) {intLength =arr. Length; for(inti =0; I < length-1; i++) { for(intj =0; J < length-1-I.; J + +) { if(arr[j]>arr[j+1]) {T temp=Arr[j]; ARR[J]= Arr[j +1]; Arr[j+1] =temp; } } } } }
Here we need to define the Sorthelper class generic class:
//define a generic class sorthelper here "where t:icomparable" is a restriction to the type parameter T--the parameter type must implement the IComparable interface, otherwise it cannot be compiled Public classSorthelper<t>wheret:icomparable { Public voidBubblesort (t[] arr) {intLength =arr. Length; for(inti =0; I < length-1; i++) { for(intj =0; J < length-1-I.; J + +) { if(Arr[j]. CompareTo (arr[j+1]) >0) {T temp=Arr[j]; ARR[J]= Arr[j +1]; Arr[j+1] =temp; } } } } }
Test it:
Static voidMain (string[] args) {Sorthelper<byte> Sorter =Newsorthelper<byte> (); byte[] A = {4,5,1,3,2,8,5,0,2}; Sorter. Bubblesort (a); Sorthelper<int> Sorter1 =Newsorthelper<int> (); int[] B = {4,5,1,3,2,8,5,0,2 }; Sorter1. Bubblesort (b); //output omitted}
Output:
0,1,2,2,3,4,5,5,8
0,1,2,2,3,4,5,5,8
3, why not use object
1) generics do not need packing and unpacking, good performance
2) Generics are type-safe
3) generics can be reused in binary code
4, generic constraint: A constraint declares the characteristics of a type parameter that is required by a generic type.
In order to declare a constraint, you need to use the where keyword followed by a pair of "parameters: Requirements". where "parameters" must be one of the parameters defined in a generic type, and "requirement" is used to restrict the type from
A class or interface that is "derived" in, or a restriction must exist for a default constructor, or to restrict the use of a reference/value type constraint.
Examples of constraints can be seen here: http://www.cnblogs.com/smiler/p/3163312.html
The sample code is copied from here: http://www.cnblogs.com/keiling/p/3672346.html
"C # Advanced Programming" Reading notes (iii)