A brief description of the role of C # generics

Source: Internet
Author: User

Generics: That is, a parameterized type is implemented to manipulate multiple data types on the same piece of code. Generic programming is a programming paradigm that abstracts types using "parameterized types" for more flexible reuse.

Overview of the role of C # generics

C # generics give code a stronger type-safe, better reuse, higher efficiency, and clearer constraints.

In a method, the value of a variable can be used as a parameter, but in fact the type of the variable can also be used as a parameter. Generics allow us to specify what this type parameter is when called. In. NET, the two obvious benefits that generics can bring to us are type safety and reduced boxing and unpacking.

In a method, the value of a variable can be used as a parameter, but in fact the type of the variable can also be used as a parameter. Generics allow us to specify what this type parameter is when called. In. NET, the two obvious benefits that generics can bring to us are: type safety and reduced boxing, unpacking.

Suppose we now have a people collection: Create a Person class

  1. Public class person
  2. {
  3. Private string name;
  4. Private int age;
  5. Public Person (string Name, int age) //constructor
  6. {
  7. this. Age = age;
  8. this. Name = name;
  9. }
  10. Public string Name
  11. {
  12. Set {This . Name = value;}
  13. Get { return name;}
  14. }
  15. Public int age
  16. {
  17. Set {This . Age = value;}
  18. Get { return age;}
  19. }
  20. }

We run the following at the entry point of the program to add an additional type of object to the collection, but when inserting the data there is no error and the compilation can pass, but "who is the King of Kung Fu?" "It's a problem when the field is converted to a person, which means that ArrayList is unsafe type.

  1. Static void Main (string[] args)
  2. {
  3. ArrayList peoples = new ArrayList ();
  4. Peoples.  ADD (new person ("dragon", 18));
  5. Peoples.  ADD (new person ("Bruce Lee", 17));
  6. Peoples. ADD ("Who is the King of Kung Fu?")  ");
  7. foreach (person, in Peoples)
  8. {
  9. Console.WriteLine (person. Name + "This year" + person. Age + "old."  ");
  10. }
  11. }

So here we create a generic for a person, and when you want to insert another type into it, the compiler will give you an error.

  1. Static void Main (string[] args)
  2. {
  3. list< person> peoples = new list< person> ();
  4. Peoples.  ADD (new person ("dragon", 18));
  5. Peoples.  ADD (new person ("Bruce Lee", 17));
  6. Peoples. ADD ("Who is the King of Kung Fu?")  ");
  7. foreach (person, in Peoples)
  8. {
  9. Console.WriteLine (person. Name + "This year" + person. Age + "old."  ");
  10. }
  11. }

The role of C # generics: Sorting

C # generics as a collection, sorting is indispensable. The sorting is based on the comparison with, to sort, first to compare. An object can have multiple comparison rules, but there can be only one default rule, and the default rule is placed in the class that defines the object. The default rule is defined in the CompareTo method, which belongs to the icomparable< t> generic interface.

  1. Public class person:icomparable< person>
  2. {
  3. 。。。。。。
  4. Public int CompareTo (person P) //Here adds a sort by age comparator
  5. {
  6. return This . Age-p.age;
  7. }
  8. }
  9. Static void Main (string[] args)
  10. {
  11. list< person> peoples = new list< person> ();
  12. Peoples.  ADD (new person ("NBS", 18));
  13. Peoples.  ADD (new person ("Bruce Lee", 17));
  14. Peoples.  ADD (new person ("House Little Dragon", 23));
  15. Peoples.  ADD (new person ("Zhang Xiaolong", 42));
  16. Peoples. Sort (); ///the sort () method without parameters to sort the collection
  17. foreach (person, in Peoples)
  18. {
  19. Console.WriteLine (person. Name + "This year" + person. Age + "old."  ");
  20. }
  21. }

Well, here's a simple explanation of the role of C # generics.

A brief description of the role of C # generics

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.