Tag: 逆转

当反转导致歧义时,不会有警告或错误(或运行时失败)

首先,记住一个.NET String是IConvertible和ICloneable 。 现在,考虑以下相当简单的代码: //contravariance "in" interface ICanEat<in T> where T : class { void Eat(T food); } class HungryWolf : ICanEat<ICloneable>, ICanEat<IConvertible> { public void Eat(IConvertible convertibleFood) { Console.WriteLine("This wolf ate your CONVERTIBLE object!"); } public void Eat(ICloneable cloneableFood) { Console.WriteLine("This wolf ate your CLONEABLE object!"); } } 然后尝试以下(在一些方法中): ICanEat<string> wolf = new HungryWolf(); […]