Mar*_*ius 1 c# generics polymorphism
为什么像这样的方法需要显式转换返回类型?
public T Test<T> ()
where T : Response
{
var ret = new Response ();
return ret; // Explicit cast needed here
}
Run Code Online (Sandbox Code Playgroud)
该约束明确指出 T 是 Response 或其子代,因此返回类型多态性应该允许无需强制转换,或者我是这么认为的。
添加as T允许编译通过。
| 归档时间: |
|
| 查看次数: |
45 次 |
| 最近记录: |