Get the App
SLTechnology News&Howtos  ›  Development  › 

What's the use of C # generics?

Shulou Source: shulou.com Published: 2022-06-02 08:37:46 10月01日 Update

This article will explain in detail what is the use of C# generics. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Generics: that is, through parameterized types to manipulate multiple data types on the same code. Generic programming is a programming paradigm that abstracts types using "parameterized types" to achieve more flexible reuse.

Summary of the role of C # generics

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

In a method, the value of a variable can be used as a parameter, but the type of the variable itself can also be used as a parameter. Generics allow us to specify what the type parameter is when we call it. In. Net, generics can bring us two obvious benefits-type safety and less boxing and unboxing.

In a method, the value of a variable can be used as a parameter, but the type of the variable itself can also be used as a parameter. Generics allow us to specify what the type parameter is when we call it. In. Net, generics can bring us two obvious benefits: type safety and less boxing and unboxing.

Suppose we now have a collection of people: create the Person class

Public class Person {private string name; private int age; public Person (string Name, int Age) / / Constructor {this.age = Age; this.name = Name;} public string Name {set {this.name = value;} get {return name;} public int Age {set {this.age = value;} get {return age;}}

/ / We run the following at the entry point of the program to add another type of object to the collection, but there is no error when inserting the data, and the compilation can also pass, but put "who is the king of kung fu?" There is a problem with the conversion of such fields to people, which indicates that ArrayList is not type safe.

Static void Main (string [] args) {ArrayList peoples = new ArrayList (); peoples.Add (new Person (Jackie Chan, 18)); peoples.Add (new Person (Bruce Lee, 17)); peoples.Add ("who is the king of kung fu?") ; foreach (Person person in peoples) {Console.WriteLine (person.Name + "this year" + person.Age + "year old.") ;}}

/ / so here we create a human generic, and when we insert other types into it, the compiler will report an error.

Static void Main (string [] args) {List

< Person>

Peoples = new List

< Person>

(); peoples.Add (new Person (Jackie Chan, 18)); peoples.Add (new Person (Bruce Lee, 17)); peoples.Add (who is the king of kung fu?) ; foreach (Person person in peoples) {Console.WriteLine (person.Name + "this year" + person.Age + "year old.") ;}}

The role of C # generics: sorting

As a collection, sorting of C # generics is indispensable. Sorting is based on comparison. To sort, you must first compare. An object can have more than one comparison rule, but only one default rule, which is placed in the class that defines the object. The default rule is defined in the CompareTo method, which belongs to IComparable

< T>

Generic interface.

Public class Person: IComparable

< Person>

{. Public int CompareTo (Person p) / / add an age-sorted comparator {return this.Age-p.Age;}} static void Main (string [] args) {List

< Person>

Peoples = new List

< Person>

(); peoples.Add (new Person ("Chen Xiaolong", 18); peoples.Add (new Person ("Bruce Lee", 17)); peoples.Add (new Person ("Fang Xiaolong", 23); peoples.Add (new Person ("Zhang Xiaolong", 42)); peoples.Sort () / / the Sort () method with no parameters sorts the collection foreach (Person person in peoples) {Console.WriteLine (person.Name + "this year" + person.Age + "year.") This is the end of this article on "what is the use of C# generics?". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Type parameter ranking method time security variable rule person kung fu object article Bruce Lee obvious two code function benefit dragon Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Information Microsoft OPPO Reno Shulou Tech Info