In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.