C#泛型接口实例应用
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,这篇文章主要讲解了"C#泛型接口实例应用",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"C#泛型接口实例应用"吧!C# 泛型接口代码//Type par
千家信息网最后更新 2026年11月03日C#泛型接口实例应用
这篇文章主要讲解了"C#泛型接口实例应用",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"C#泛型接口实例应用"吧!
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"); } }可将多重接口指定为单个类型上的约束,如下所示:
C# 泛型接口代码
class Stack﹤T﹥ where T : System.IComparable﹤T﹥, IEnumerable﹤T﹥ { }一个接口可定义多个类型参数,如下所示:
C# 泛型接口代码
interface IDictionary﹤K, V﹥ { }类之间的继承规则同样适用于接口:
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如果泛型接口为逆变的,即仅使用其类型参数作为返回值,则此泛型接口可以从非泛型接口继承。在 .NET Framework 类库中,IEnumerable﹤T﹥ 从 IEnumerable 继承,因为 IEnumerable﹤T﹥ 仅在 GetEnumerator 的返回值和当前属性 getter 中使用 T。
具体类可以实现已关闭的构造接口,如下所示:
C# 泛型接口代码
interface IBaseInterface﹤T﹥ { } class SampleClass : IBaseInterface﹤string﹥ { }只要类参数列表提供了接口必需的所有参数,泛型类便可以实现泛型接口或已关闭的构造接口,如下所示:
C# 泛型接口代码
interface IBaseInterface1﹤T﹥ { } interface IBaseInterface2﹤T, U﹥ { } class SampleClass1﹤T﹥ : IBaseInterface1﹤T﹥ { }//No error class SampleClass2﹤T﹥ : IBaseInterface2﹤T, string﹥ { }//No error对于泛型类、泛型结构或泛型接口中的方法,控制方法重载的规则相同。
感谢各位的阅读,以上就是"C#泛型接口实例应用"的内容了,经过本文的学习后,相信大家对C#泛型接口实例应用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
接口
C#
代码
实例
应用
参数
类型
学习
内容
方法
规则
相同
之间
单个
多个
就是
属性
思路
情况
文章
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
湖南永感互联网科技有限公司
互联网科技会
美国互联网科技巨头监管
互联网下的科技发展
金诺互联网科技
与互联网科技公司
上海挚品互联网科技
互联网科技ppt模板
人人互联网科技是培训
互联网金融科技委员会
互联网科技公司的市场部门
中国的互联网科技企业的崛起
互联网科技中介
杭州壁岐互联网科技有限公司
长沙茂晟互联网科技有限公司
互联网科技公司营业执照怎么办理
伊宁互联网科技做什么的
互联网硬科技公司
抖集互联网科技技术咨询
江苏互联网智能科技产品出厂价
未来的互联网科技
万达互联网科技
居家互联网科技有限公司
湖南洣水互联网科技有限公司扣款
华服互联网科技官网
速维有品互联网信息科技(苏州)有限公司
链环科技传统企业怎么转型互联网
九思连强互联网电子科技有限公司
唐山双旺合互联网科技产业园
科技美学互联网 艺术