C#怎么使用泛型 C#泛型类与泛型方法定义

来源:这里教程网 时间:2026-02-21 17:33:04 作者:

泛型是 C# 中提升代码复用性、类型安全性和性能的重要机制。它让你写一次逻辑,适配多种类型,避免装箱/拆箱,也省去强制类型转换的麻烦。

泛型类:用 T 占位,运行时确定真实类型

泛型类在定义时用类型参数(如 T)代替具体类型,实例化时再指定实际类型。

基本语法:

class Stack
{
  private List _items = new();

  public void Push(T item) => _items.Add(item);

  public T Pop()
  {
    if (_items.Count == 0) throw new InvalidOperationException();
    var last = _items[^1];
    _items.RemoveAt(_items.Count - 1);
    return last;
  }
}

使用方式:

var intStack = new Stack<int>(); intStack.Push(42);</int>
var strStack = new Stack<string>(); strStack.Push("hello");</string>

注意:同一个泛型类的不同类型实参(如

Stack<int></int>
Stack<string></string>
)在运行时是完全不同的类型,互不兼容。

泛型方法:方法自己带类型参数,更灵活

泛型方法把类型参数写在方法名后,可独立于所在类是否泛型。编译器常能自动推断类型,调用时可省略尖括号。

示例:

static T GetFirst(IList list)
{
  if (list == null || list.Count == 0) throw new ArgumentException();
  return list[0];
}

// 调用方式(两种都行):
int first = GetFirst(new[] { 1, 2, 3 }); // 自动推断 T 为 int
string s = GetFirst(new[] { "a", "b" }); // 显式指定

常用约束(where):给泛型加规则

有时你需要对 T 做限制,比如要求它有无参构造函数、实现某个接口或继承某个类。这时用 where 子句:

class Repository<t> where T : class, IEntity, new()</t>
→ T 必须是引用类型、实现
IEntity
、且有 public 无参构造函数
void Process<t>(T value) where T : IComparable<t></t></t>
→ T 必须支持自身比较
where T : struct
→ 限定为值类型(如 int、DateTime)

约束让泛型更安全,也让你能在方法体内放心调用

T
的成员。

泛型委托与常见泛型类型

C# 内置大量泛型类型和委托,直接拿来就用:

集合:
List<t></t>
Dictionary<k></k>
Queue<t></t>
HashSet<t></t>
委托:
Action<t></t>
Func<tresult></tresult>
Func<t tresult></t>
Predicate<t></t>
接口:
IEnumerable<t></t>
IComparable<t></t>
IEqualityComparer<t></t>

它们都是泛型设计的最佳实践样本,读源码或文档时多留意其泛型结构,有助于你写出更地道的泛型代码。

基本上就这些。泛型不复杂,但容易忽略约束和推断细节。写的时候想清楚“这个类型需要做什么”,再选合适的约束,代码会更健壮也更易读。

相关推荐