Seems like a classic Covariance example in Generics: you can use an interface with a more general class, rather than a derivative.
So they
"allow implicit drops from types that are not derived from them"
since here you have an implicit translation from an interface with a base type to an interface with a derived type, therefore, from types (interfaces with basic types) that do not derive from them (interfaces with derived types).
In my example, you can see a covariant interface that calculates a region of a more derivative form, since it has a less derivative form, so you have a listing where, for example, the dimension is lost ...
public class Basic { public double dim1; } public class Derived : Basic { public double dim2; } public interface IFactory<in T> { double Area(T shape); } class BasicFactory : IFactory<Basic> { public double Area(Basic shape) { return shape.dim1 * shape.dim1; } } class DerivedFactory : IFactory<Derived> { public double Area(Derived shape) { return shape.dim1 * shape.dim2; } } class Program { double Area(IFactory<Derived> factory, Derived shape) { return factory.Area(shape); } static void Main(string[] args) { IFactory<Basic> notDerived = new BasicFactory(); // from not derived type Derived shape = new Derived() { dim1 = 10, dim2 = 20 }; double area = new Program().Area(notDerived,shape); // cast! dimension loss Console.WriteLine(area); // 100 = 10*10 IFactory<Derived> derived = new DerivedFactory(); //from derived type area = new Program().Area(derived, shape); // no cast, now Console.WriteLine(area); // 200 = 10*20 Console.ReadKey(); } }
user6996876
source share