很简单,为什么这段代码无法编译?
public interface IWorld { }
public class Foo<T> where T : IWorld { }
public void Hello<T>(T t) where T : IWorld
{
Foo<IWorld> bar1 = new Foo<T>(); //fails implicit cast
Foo<IWorld> bar2 = (Foo<IWorld>)new Foo<T>(); //fails explicit cast
}
Run Code Online (Sandbox Code Playgroud)
由于每个T实现IWorld,每个实例都Foo<T>应该匹配Foo<IWorld>.为什么不?有没有办法解决?我真的不想借助于泛型来实现这一目标.