我有这个代码:
public async static Task<T?> RequestValue1<T>(Command requestCommand)
where T : struct
{
// Whatever
}
public async static Task<T> RequestValue2<T>(Command requestCommand)
where T : class
{
// Whatever
}
我想为我的两种方法使用相同的名称。这甚至可能吗?
我的问题:
- 我必须编写两种不同的方法,因为返回类型(如果请求失败,我希望它为 null,如果请求成功,我希望它为值),即
Nullable<T>
ifT
是值类型,T
if的实例T
是引用类型。 async
不允许引用/输出,因此如果没有 type 的方法参数T
,则不会推断,并且我的两个方法不能具有相同的名称(签名冲突,因为如果不推断T
,通用约束不适用于签名冲突解决)T
目前这段代码有效,但我不喜欢“RequestValue1”和“RequestValue2”之间的这个奇怪的函数调用。