赞
踩
在 C# 中,类模板被称为泛型类,它是一种可以在编译时支持多种类型的通用类。与 C++ 类模板类似,泛型类可以提供代码重用性和灵活性。
使用场景:
优点:
缺点:
举例:
- // 定义一个简单的泛型类
- public class Pair<T> {
- private T first;
- private T second;
-
- public Pair(T a, T b) {
- first = a;
- second = b;
- }
-
- public T GetFirst() {
- return first;
- }
-
- public T GetSecond() {
- return second;
- }
- }
-
- class Program {
- static void Main(string[] args) {
- // 实例化一个Pair泛型类,支持int类型
- Pair<int> intPair = new Pair<int>(1, 2);
- Console.WriteLine(intPair.GetFirst() + ", " + intPair.GetSecond());
-
- // 实例化一个Pair泛型类,支持double类型
- Pair<double> doublePair = new Pair<double>(3.14, 2.7);
- Console.WriteLine(doublePair.GetFirst() + ", " + doublePair.GetSecond());
- }
- }

在使用 C# 泛型类时,容易出现的问题包括以下几个方面,以及对应的解决办法:
1.类型约束错误:在定义泛型类时,可能会遇到类型约束错误的问题,例如要求泛型参数实现某个接口或继承自某个基类。如果在使用泛型类时传入的类型不符合这些约束,将导致编译错误。解决办法是确保在使用泛型类时传入的类型符合泛型类定义的类型约束。
- public class MyGenericClass<T> where T : IComparable
- {
- // ...
- }
2.引用类型和值类型问题:在泛型类中,需要注意引用类型和值类型之间的差异。引用类型的默认值是 null,而值类型的默认值是对应类型的默认值。如果在泛型类中使用了 null 初始值并且传入了值类型,可能会导致空引用异常。解决办法是在泛型类中对传入的值类型进行判空处理或使用可为空类型(Nullable)。
- public class MyGenericClass<T>
- {
- private T myValue;
-
- public MyGenericClass()
- {
- if (typeof(T).IsValueType)
- {
- myValue = default(T); // 对值类型使用默认值
- }
- else
- {
- myValue = default(T); // 对引用类型使用 null
- }
- }
- }

3.性能问题:使用泛型类的一个常见问题是在运行时会产生多个实例化的类,可能会导致不必要的性能开销。为了解决这个问题,可以考虑使用单例模式或者静态类来避免频繁的实例化。
- public class Singleton<T> where T : new()
- {
- private static T instance;
-
- public static T Instance
- {
- get
- {
- if (instance == null)
- {
- instance = new T();
- }
- return instance;
- }
- }
- }
-
- public class MyClass
- {
- // ...
- }
-
- class Program
- {
- static void Main(string[] args)
- {
- MyClass instance1 = Singleton<MyClass>.Instance;
- MyClass instance2 = Singleton<MyClass>.Instance;
-
- Console.WriteLine(instance1 == instance2); // 输出:True,说明得到的是同一个实例
- }
- }

在上述的例子中,使用泛型类 Singleton<T> 实现了一个单例模式,保证了只有一个实例存在,提高了性能。
在这个例子中,我们定义了一个Pair泛型类,它可以支持不同类型的数据,并使用int和double类型分别实例化了这个泛型类。这样,我们可以通过泛型类来创建通用的数据类型容器。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。