"C # Advanced Programming" Reading notes (iii)

Source: Internet
Author: User

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&lt;byte&gt; Sorter =Newsorthelper&lt;byte&gt; (); byte[] A = {4,5,1,3,2,8,5,0,2}; Sorter.            Bubblesort (a); Sorthelper&lt;int&gt; Sorter1 =Newsorthelper&lt;int&gt; (); 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)

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.