1. 接口
- 解释:接口定义了一组方法、属性等的签名,但不包含实现。类或结构通过实现接口来提供这些功能,用于实现多重继承。
- 重点:定义行为契约,实现多态性。
- 难点:处理接口成员同名冲突,确保实现的正确性。
// 定义接口
interface IPrintable
{
void Print();
}
// 实现接口的类
class Document : IPrintable
{
public void Print()
{
Console.WriteLine("打印文档…");
}
}
2. 多接口
- 解释:一个类可以实现多个接口,从而具备多种不同的行为能力。
- 重点:组合功能,增加灵活性与扩展性。
- 难点:解决接口成员冲突,管理代码复杂度。
interface IDataProvider
{
string GetData();
}
interface IDataModifier
{
void ModifyData(string newData);
}
class Database : IDataProvider, IDataModifier
{
private string data;
public string GetData()
{
return data;
}
public void ModifyData(string newData)
{
data = newData;
}
}
3. 泛型方法
- 解释:在方法定义中使用类型参数,允许方法在不指定具体数据类型的情况下编写通用逻辑,调用时确定具体类型。
- 重点:提高代码复用性与类型安全性。
- 难点:理解类型推断,合理应用泛型约束。
class Utility
{
// 泛型方法,交换两个变量的值
public static void Swap<T>(ref T a, ref T b)
{
T temp = a;
a = b;
b = temp;
}
}
class Program
{
static void Main()
{
int num1 = 5, num2 = 10;
Utility.Swap(ref num1, ref num2);
Console.WriteLine($"num1: {num1}, num2: {num2}");
string str1 = "Hello", str2 = "World";
Utility.Swap(ref str1, ref str2);
Console.WriteLine($"str1: {str1}, str2: {str2}");
}
}
4. 泛型效率
- 解释:泛型对于值类型可避免装箱和拆箱操作提升性能,但泛型类型的实例化和方法调用可能存在一些额外开销。
- 重点:值类型性能提升,关注实例化开销。
- 难点:性能调优,理解 CLR 对泛型处理。
// 非泛型集合存储值类型会有装箱拆箱
ArrayList nonGenericList = new ArrayList();
nonGenericList.Add(1); // 装箱
int value1 = (int)nonGenericList[0]; // 拆箱
// 泛型集合直接存储值类型,无装箱拆箱
List<int> genericList = new List<int>();
genericList.Add(1);
int value2 = genericList[0];
5. 泛型类和泛型接口
- 泛型类:
- 解释:在类定义时使用类型参数,使类能处理不同类型的数据。
- 重点:代码复用与类型安全,可自定义数据结构。
- 难点:实现复杂数据结构,设计泛型约束。
class Box<T>
{
private T item;
public void Store(T item)
{
this.item = item;
}
public T Retrieve()
{
return item;
}
}
class Program
{
static void Main()
{
Box<int> intBox = new Box<int>();
intBox.Store(42);
int result = intBox.Retrieve();
Console.WriteLine(result);
Box<string> stringBox = new Box<string>();
stringBox.Store("Hello");
string strResult = stringBox.Retrieve();
Console.WriteLine(strResult);
}
}
- 泛型接口:
- 解释:在接口定义中使用类型参数,实现接口的类型需为类型参数指定具体类型。
- 重点:灵活定义契约,促进代码一致性。
- 难点:理解多样的接口实现,与其他泛型协同。
interface IComparer<T>
{
int Compare(T x, T y);
}
class IntegerComparer : IComparer<int>
{
public int Compare(int x, int y)
{
return x.CompareTo(y);
}
}
class StringLengthComparer : IComparer<string>
{
public int Compare(string x, string y)
{
return x.Length.CompareTo(y.Length);
}
}
6. 泛型约束
- 解释:限制泛型类型参数必须满足的条件,确保泛型代码中对类型参数操作的合法性。
- 重点:保证类型安全与功能,增强代码可读性。
- 难点:选择合适约束条件,处理多重约束。
class GenericFactory<T> where T : new()
{
public T CreateInstance()
{
return new T();
}
}
class MyClass
{
// 无参数构造函数,满足约束
public MyClass() { }
}
class Program
{
static void Main()
{
GenericFactory<MyClass> factory = new GenericFactory<MyClass>();
MyClass instance = factory.CreateInstance();
}
}
异同点
- 接口与泛型接口:
- 相同点:都定义行为契约,实现它们的类型都需提供对应成员实现。
- 不同点:接口针对特定类型定义契约,泛型接口通过类型参数使契约适用于多种类型,更灵活。
- 泛型类与泛型接口:
- 相同点:都使用类型参数实现参数化类型,提高复用性与类型安全性。
- 不同点:泛型类是具体实现,包含数据成员和方法实现;泛型接口仅定义行为签名,由实现接口的类型提供具体实现。
- 泛型方法与泛型类:
- 相同点:都利用类型参数实现代码复用与类型安全,都可应用泛型约束。
- 不同点:泛型方法在方法级别实现通用逻辑,可存在于非泛型类或泛型类中;泛型类是整个类针对不同类型参数进行参数化,提供统一的数据结构或功能实现。



![P11246 [GESP202409 六级] 小杨和整数拆分-171主机测评](https://www.171host.com/wp-content/uploads/2026/11/20261108100631-6af049a75855c-220x150.png)


