TDa*_*ver 5 c# generics delegates covariance
public void Foo<T>(Func<T> bar)
where T: IMyInterface
{
Func<IMyInterface> func = bar;
}
Run Code Online (Sandbox Code Playgroud)
自从我理解了协方差以来已经有一段时间了,但这不应该编译吗?
任何bar可以返回的东西也是IMyInterface.什么似乎是问题?
正确的代码是:
public void Foo<T>(Func<T> bar)
where T: class, IMyInterface
{
Func<IMyInterface> func = bar;
}
Run Code Online (Sandbox Code Playgroud)