In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "the application of C# generic interface example". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "C# generic interface instance application".
C # generic interface code
/ / Type parameter T in angle brackets. Public class GenericList < T >: System.Collections.Generic.IEnumerable < T > {protected Node head; protected Node current = null; / / Nested class is also generic on T protected class Node {public Node next; private T data; / / T as private member datatype public Node (T t) / / T used in non-generic constructor {next = null; data = t;} public Node Next {get {return next;} set {next = value } public T Data / / T as return type of property {get {return data;} set {data = value;} public GenericList () / / constructor {head = null;} public void AddHead (T t) / / T as method parameter type {Node n = new Node (t); n.Next = head; head = n;} / Implementation of the iterator public System.Collections.Generic.IEnumerator < T > GetEnumerator () {Node current = head While (current! = null) {yield return current.Data; current = current.Next;}} / / IEnumerable < T > inherits from IEnumerable, therefore this class / / must implement both the generic and non-generic versions of / / GetEnumerator. In most cases, the non-generic method can / / simply call the generic method. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator () {return GetEnumerator ();}} public class SortedList < T >: GenericList < T > where T: System.IComparable < T > {/ / A simple, unoptimized sort algorithm that / / orders list elements from lowest to highest: public void BubbleSort () {if (null = = head | | null = = head.Next) {return;} bool swapped; do {Node previous = null; Node current = head; swapped = false While (current.next! = null) {/ / Because we need to call this method, the SortedList / / class is constrained on IEnumerable < T > if (current.Data.CompareTo (current.next.Data) > 0) {Node tmp = current.next; current.next = current.next.next; tmp.next = current; if (previous = = null) {head = tmp;} else {previous.next = tmp;} previous = tmp; swapped = true } else {previous = current; current = current.next;} while (swapped);} / / A simple class that implements / / IComparable < T > using itself as the / / type argument. This is a common / / design pattern in objects that / / are stored in generic lists. Public class Person: System.IComparable < Person > {string name; int age; public Person (string s, int I) {name = s; age = I;} / / This will cause list elements / / to be sorted on age values. Public int CompareTo (Person p) {return age-p. Age;} public override string ToString () {return name + ":" + age;} / / Must implement Equals. Public bool Equals (Person p) {return (this.age = = p.age);}} class Program {static void Main () {/ / Declare and instantiate a new generic SortedList class. / / Person is the type argument. SortedList < Person > list = new SortedList < Person > (); / / Create name and age values to initialize Person objects. 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}; / / Populate the list. For (int x = 0; x < 10; list.AddHead +) {list.AddHead (new Person (names [x], ages [x]));} / / Print out unsorted list. Foreach (Person p in list) {System.Console.WriteLine (p.ToString ());} System.Console.WriteLine ("Done with unsorted list"); / / Sort the list. List.BubbleSort (); / / Print out sorted list. Foreach (Person p in list) {System.Console.WriteLine (p.ToString ());} System.Console.WriteLine ("Done with sorted list");}
Multiple interfaces can be specified as constraints on a single type, as follows:
C # generic interface code
Class Stack < T > where T: System.IComparable < T >, IEnumerable < T > {}
An interface can define multiple type parameters, as follows:
C # generic interface code
Interface IDictionary < K, V > {}
Inheritance rules between classes also apply to interfaces:
C # generic interface code
Interface IMonth < T > {} interface IJanuary: IMonth < int > {} / / No error interface IFebruary < T >: IMonth < int > {} / / No error interface IMarch < T >: IMonth < T > {} / / No error / / interface IApril < T >: IMonth < T, U > {} / / Error
If a generic interface is inverted, that is, only its type parameters are used as the return value, the generic interface can inherit from a non-generic interface. In the .NET Framework class library, IEnumerable < T > inherits from IEnumerable because IEnumerable < T > only uses T in the return value of GetEnumerator and the current property getter.
Concrete classes can implement closed construction interfaces, as follows:
C # generic interface code
Interface IBaseInterface < T > {} class SampleClass: IBaseInterface < string > {}
A generic class can implement a generic interface or a closed construction interface as long as the class parameter list provides all the necessary parameters for the interface, as follows:
C # generic interface code
Interface IBaseInterface1 < T > {} interface IBaseInterface2 < T, U > {} class SampleClass1 < T >: IBaseInterface1 < T > {} / / No error class SampleClass2 < T >: IBaseInterface2 < T, string > {} / / No error
The rules for controlling method overloading are the same for methods in generic classes, generic structures, or generic interfaces.
Thank you for your reading. the above is the content of "C# generic Interface instance Application". After the study of this article, I believe you have a deeper understanding of the application of C# generic interface instance, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.