首页 > 解决方案 > C# 中的通用部分方法?

问题描述

部分类和部分方法状态:

部分方法可以是通用的。约束放在定义的部分方法声明上,并且可以选择在实现的部分上重复。实现声明中的参数和类型参数名称不必与定义声明中的相同。

"Constraints are put on the defining partial method declaration, and may optionally be repeated on the implementing one."视觉上和的代码示例是"Parameter and type parameter names do not have to be the same in the implementing declaration as in the defining one."什么?

我不知道上面 2 个语句在代码中的视觉效果如何。

标签: c#partialpartial-methods

解决方案


宣言

包括通用约束:

public void Method<TName>(int name) where T : class;

执行

没有重复的通用约束和不同的参数名称(但相同的签名):

public void Method<TOther>(int other)
{
    // ....
}

推荐阅读