interface ICloneable<out T>
{
T Clone();
}
class Base : ICloneable<Base>
{
public Base Clone() { return new Base(); }
}
class Derived : Base, ICloneable<Derived>
{
new public Derived Clone() { return new Derived(); }
}
Учитывая эти объявления типов, какая часть спецификации C # объясняет, почему последняя строка следующего фрагмента кода печатает «True»? Могут ли разработчики полагаться на такое поведение?
Derived d = new Derived();
Base b = d;
ICloneable<Base> cb = d;
Console.WriteLine(b.Clone() is Derived); // "False": Base.Clone() is called
Console.WriteLine(cb.Clone() is Derived); // "True": Derived.Clone() is called
Обратите внимание, что если параметр типа T
в ICloneable
был не объявлен вне
, то оба строки будут печатать «Ложь».