Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
public void Foo<T>(Func<T> bar) where T: IMyInterface { Func<IMyInterface> func = bar; }
自从我理解协方差以来已经有一段时间了,但这不应该编译吗?
任何bar可以返回的东西也是一个IMyInterface。似乎是什么问题?
bar
IMyInterface
这是 C# 4 中的协方差错误吗?
正确的代码是:
public void Foo<T>(Func<T> bar) where T: class, IMyInterface { Func<IMyInterface> func = bar; }