Introduction: Let's take a look at the concept of generics-"using parameterized types to implement multiple data types on the same code. The "parameterized type" is used to abstract the type and achieve flexible reuse ". Many beginners are hard to understand when they first come into contact with generics. Here, "generics" are considered as an adjective to facilitate understanding, because many things can be generic. for example, "generic classes ", "generic methods", "generic interfaces", and "generic delegation... in many cases, the use of generics can greatly reduce code duplication to make the program more fresh, and avoid unnecessary 'binning. <Introduction of generics | why do we need generics?> In the process of program design, we often encounter such a situation: in order to implement a function, we first write the method, however, we later found that the same function requires us to write it again, but the parameter type of this method is different from that of the previous one. At this time, we should not abstract and respond to changes too early according to the idea of agile software development, when a change occurs for the first time, the fastest way is used to solve it, but when the change appears for the second time, a better architecture design is conducted to avoid excessive design, because it is possible that the second change will never happen. Taking into account the same functions, here we usually directly copy the code of the original method, and then modify the parameter type to quickly solve the problem; this is indeed true, but sometimes not only the second change, but also the third change... or, if you continue to use the CV method to modify the signature of the method multiple times, a large number of repeated codes will appear, so we will think, if there is a method that can pass any data type, that is, the implementation of this method is used as a template to abstract the signature of the method, so we introduced the generic type. Next let's take a look at the specific example: 1.1 Use the CV big method --------------- to input multiple int types and perform Bubble sorting to reduce them to large output in sequence. The Code is as follows: copy the public class SortHelper {public void BubbleSort (int [] arr) {int length = arr. length; for (int I = 0; I <length-1; I ++) {for (int j = 0; j <length-1-i; j ++) {if (arr [j]> arr [j + 1]) {int temp = arr [j]; arr [j] = arr [j + 1]; arr [j + 1] = temp ;}}} copy code test: copy the code static void Main (string [] args) {SortHelper sorter = n Ew SortHelper (); int [] a = {, 2}; sorter. bubbleSort (a); // output omitted} copy the code to output:, 8 --------------- Input Multiple bytes, perform Bubble sorting to reduce them to large output in sequence. The Code is as follows: at this time, I just need to copy the original method and change the signature to copy the code public class SortHelper {public void BubbleSort (byte [] arr) {int length = arr. length; for (int I = 0; I <length-1; I ++) {for (int j = 0; j <length-1-i; j ++) {if (arr [j]> arr [j + 1]) {byte temp = arr [j]; arr [j] = Arr [j + 1]; arr [j + 1] = temp ;}}} can copy the code, however, if you want to process multiple other data classes for N times, a large number of duplicate copies will seriously affect the Code's conciseness. In addition, when the function is to be extended, each method must be modified, it is very inconvenient to maintain. 1.2 using generic (generic class): We naturally think that if we can use a "Placeholder" for the parameter type in the method to indicate the type of each input, in this way, you can use this method as a template (a bit like using placeholders in Html in Web programming ). Here we use "T" to set this special parameter type, so the code becomes like this: copy the code public class SortHelper {public void BubbleSort (T [] arr) {int length = arr. length; for (int I = 0; I <length-1; I ++) {for (int j = 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 ;}}} copy the code. here T represents the "type" and int, string... T is the type itself. The exciting thing is that there is something special like "T. NET is called a type parameter. next let's take a look at the Standard Code. Here we define BubbleSort as a generic class. One way to define a generic class is to add "<T>" after the class to copy the code // define the generic type SortHelper here "where T: IComparable "is a restriction for type parameter T -- the parameter type must implement the IComparable interface; otherwise, public class SortHelper cannot be compiled. <T> where T: IComparable {public void BubbleSort (T [] arr) {int length = arr. length; for (int I = 0; I <length-1; I ++) {for (int j = 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 ;}}} copy code test: copy the code static void Main (string [] args) {SortHelper <byte> sorter = new SortHelper <byte> (); byte [] a = {, 2}; sorter. bubbleSort (a); SortHelper <int> sorter1 = new SortHelper <int> (); int [] B = {4, 5, 1, 3, 2, 8, 5, 0, 2}; sorter1.BubbleSort (B); // output omitted} copy the code output, 8 --------------- enter multiple custom instances and perform Bubble sorting to reduce them to large output. The Code is as follows: next we will simulate the cat categories sold by pet shops by price: copy the code public class cat: IComparable {public string name; public int price; public int CompareTo (object obj) {cat catT = (cat) obj; return this. price. compareTo (catT. price);} public cat (string name, int price) {this. price = price; this. name = name ;}} copy code test: copy the code static void Main (string [] args) {SortHelper <cat> sorter2 = new SortHelper <cat> (); cat cat1 = new cat ("cat 1", 1000); cat cat2 = new cat ("cat 2", 1400); cat cat3 = new cat ("cat 3 ", 400); cat [] c = {cat1, cat2, cat3}; sorter2.BubbleSort (c); // output for (int I = 0; I <c. length; I ++) {Console. writeLine ("Name:" + c [I]. name + "Price:" + c [I]. price );}}