您好,登錄后才能下訂單哦!
這篇文章主要講解了“C#泛型接口實(shí)例應(yīng)用”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“C#泛型接口實(shí)例應(yīng)用”吧!
C# 泛型接口代碼
//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; x++) { 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"); } }
可將多重接口指定為單個(gè)類型上的約束,如下所示:
C# 泛型接口代碼
class Stack﹤T﹥ where T : System.IComparable﹤T﹥, IEnumerable﹤T﹥ { }
一個(gè)接口可定義多個(gè)類型參數(shù),如下所示:
C# 泛型接口代碼
interface IDictionary﹤K, V﹥ { }
類之間的繼承規(guī)則同樣適用于接口:
C# 泛型接口代碼
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
如果泛型接口為逆變的,即僅使用其類型參數(shù)作為返回值,則此泛型接口可以從非泛型接口繼承。在 .NET Framework 類庫(kù)中,IEnumerable﹤T﹥ 從 IEnumerable 繼承,因?yàn)?IEnumerable﹤T﹥ 僅在 GetEnumerator 的返回值和當(dāng)前屬性 getter 中使用 T。
具體類可以實(shí)現(xiàn)已關(guān)閉的構(gòu)造接口,如下所示:
C# 泛型接口代碼
interface IBaseInterface﹤T﹥ { } class SampleClass : IBaseInterface﹤string﹥ { }
只要類參數(shù)列表提供了接口必需的所有參數(shù),泛型類便可以實(shí)現(xiàn)泛型接口或已關(guān)閉的構(gòu)造接口,如下所示:
C# 泛型接口代碼
interface IBaseInterface1﹤T﹥ { } interface IBaseInterface2﹤T, U﹥ { } class SampleClass1﹤T﹥ : IBaseInterface1﹤T﹥ { }//No error class SampleClass2﹤T﹥ : IBaseInterface2﹤T, string﹥ { }//No error
對(duì)于泛型類、泛型結(jié)構(gòu)或泛型接口中的方法,控制方法重載的規(guī)則相同。
感謝各位的閱讀,以上就是“C#泛型接口實(shí)例應(yīng)用”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)C#泛型接口實(shí)例應(yīng)用這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。