This article analyzes the usage of C# generics in examples. Share it for your reference. The specific analysis is as follows:
Here is a demonstration of how to create a custom generic list class with a single type parameter, and how to implement IEnumerable<T> to enable foreach iteration on the contents of the list. This example also demonstrates how client code creates an instance of the class by specifying a type parameter, and how constraints of that type parameter implement additional operations on a type parameter.
using System; using ; using ; using ; namespace Generics_CSharp { // Type parameter T in angle brackets. public class MyList<T> : IEnumerable<T> { protected Node head; protected Node current = null; // Nested types are also generics on T protected class Node { public Node next; // T as a private member data type. private T data; // T used in non-generic constructors. public Node(T t) { next = null; data = t; } public Node Next { get { return next; } set { next = value; } } // T is the return type of the property. public T Data { get { return data; } set { data = value; } } } public MyList() { head = null; } // T as method parameter type. public void AddHead(T t) { Node n = new Node(t); = head; head = n; } // Implement GetEnumerator to return IEnumerator<T>, thus enabling list // foreach iteration. Please note that in C# 2.0, // does not need to implement Current and MoveNext. // The compiler will create a class that implements IEnumerator<T>. public IEnumerator<T> GetEnumerator() { Node current = head; while (current != null) { yield return ; current = ; } } // This method must be implemented because // IEnumerable<T> Inherit IEnumerable IEnumerator () { return GetEnumerator(); } } public class SortedList<T> : MyList<T> where T : IComparable<T> { // A simple sorting algorithm that is not optimized, // The algorithm sorts list elements from low to high: public void BubbleSort() { if (null == head || null == ) return; bool swapped; do { Node previous = null; Node current = head; swapped = false; while ( != null) { // Since this method needs to be called, SortedList // Classes are constrained on IEnumerable<T> if (() > 0) { Node tmp = ; = ; = current; if (previous == null) { head = tmp; } else { = tmp; } previous = tmp; swapped = true; } else { previous = current; current = ; } }// end while } while (swapped); } } // A simple class that implements IComparable<T> as a type parameter, // It's in the object // Commonly used design patterns, these objects //Stored in the generic list. public class Person : IComparable<Person> { string name; int age; public Person(string s, int i) { name = s; age = i; } // This will make the list element // Sort by age value. public int CompareTo(Person p) { return age - ; } public override string ToString() { return name + ":" + age; } // Equals must be implemented. public bool Equals(Person p) { return ( == ); } } class Generics { static void Main(string[] args) { // Declare and instantiate a new paradigm SortedList class. // Person is a type parameter. SortedList<Person> list = new SortedList<Person>(); // Create name and age values to initialize the Person object. string[] names = new string[] { "Franscoise", "Bill", "Li", "Sandra", "Gunnar", "Alok", "Hiroyuki", "Maria", "Alessandro", "Raul" }; int[] ages = new int[] { 45, 19, 28, 23, 18, 9, 108, 72, 30, 35 }; // Fill in the list. for (int x = 0; x < ; x++) { (new Person(names[x], ages[x])); } ("Unsorted List:"); // Print out the unsorted list. foreach (Person p in list) { (()); } // Sort the list. (); (("{0}Sorted List:", )); // Print out the sorted list. foreach (Person p in list) { (()); } ("Done"); } } }
I hope this article will be helpful to everyone's C# programming.