泛型通过类型参数化解决类型安全与性能问题,避免装箱和强制转换;支持泛型类、方法、约束及常见集合应用,提升代码复用性与执行效率。

泛型在C#中是一种强大的语言特性,它允许我们编写可重用、类型安全且高性能的代码。通过使用泛型,我们可以定义类、方法、接口等而不指定具体的数据类型,而是在实际使用时再确定类型。这不仅减少了重复代码,还避免了运行时的类型转换开销。
1. 泛型解决的问题:类型安全与性能
在没有泛型的时代,如果我们想写一个通用的容器来存储数据,通常会使用 object 类型。例如:
public class ObjectList
{
private object[] items = new object[10];
private int count = 0;
public void Add(object item)
{
items[count++] = item;
}
public object Get(int index)
{
return items[index];
}
}
这种做法有两个主要问题:
- 类型不安全:可以从集合中取出错误类型的对象,编译器无法检查。
- 性能损耗:值类型(如int)需要装箱(boxing),引用类型取出来后还需要强制转换。
使用泛型后,这些问题迎刃而解:
public class GenericList{ private T[] items = new T[10]; private int count = 0; public void Add(T item) { items[count++] = item; } public T Get(int index) { return items[index]; } }
现在我们可以这样使用:
var intList = new GenericList(); intList.Add(100); int value = intList.Get(0); // 直接是int,无需转换 var stringList = new GenericList (); stringList.Add("hello"); string text = stringList.Get(0); // 类型安全
代码复用的同时,保持了类型安全和高性能。
2. 泛型方法:让方法更灵活
泛型不仅可以用于类,也可以用于方法。比如我们想写一个交换两个变量的方法:
public static void Swap(ref T a, ref T b) { T temp = a; a = b; b = temp; }
调用方式非常直观:
int x = 10, y = 20; Swap(ref x, ref y); // 自动推断T为int string s1 = "A", s2 = "B"; Swap(ref s1, ref s2); // 自动推断T为string
同一个方法支持任意类型,编译器确保类型一致,无需重载多个版本。
3. 泛型约束:控制类型范围
有时候我们希望泛型只接受满足特定条件的类型。这时可以使用约束(constraints)。例如,我们想写一个比较两个对象并返回较大值的方法:
public static T Max(T a, T b) where T : IComparable { return a.CompareTo(b) > 0 ? a : b; }
这里的 where T : IComparable
使用示例:
int maxInt = Max(5, 10); // 正确:int 实现了 IComparablestring maxStr = Max("a", "z"); // 正确:string 也实现了 // MyClass obj1, obj2; // Max(obj1, obj2); // 编译错误:MyClass 未实现 IComparable
常见约束包括:
- 基类约束:where T : MyBaseClass
- 接口约束:where T : ISomeInterface
- 构造函数约束:where T : new()
- 引用/值类型约束:where T : class / where T : struct
4. 常见泛型类型的实际应用
.NET框架大量使用泛型。最常见的就是 List
var numbers = new List{ 1, 2, 3 }; var people = new Dictionary (); people["john"] = new Person("John"); var stack = new Stack (); stack.Push("first"); string top = stack.Pop(); // 类型安全,无需转换
这些集合类都基于泛型实现,提供了高性能和类型安全的操作体验。
基本上就这些。泛型让C#在保持类型安全的同时实现了高度的代码复用,是现代C#开发不可或缺的一部分。理解并熟练使用泛型,能显著提升代码质量和开发效率。










