协方差,委托和泛型类型约束

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.什么似乎是问题?

TDa*_*ver 7

这是C#4中的协方差错误吗?

正确的代码是:

public void Foo<T>(Func<T> bar)
 where T: class, IMyInterface
{
   Func<IMyInterface> func = bar;
}
Run Code Online (Sandbox Code Playgroud)