我正在使用.net 3.5框架。下面是 是我的代碼。泛型是用於鍵入sefty?派生類型呢?
class Base {}
class Derived : Base {}
class Program {
static Main() {
IList<Base> base_col = new List<Base>();
base_col.Add(new Derived()); // Do you think this line code is good?
}
}
你認爲仿製藥將是類型檢查的好主意嗎?
我會走一步。
static Main() {
IList<Derived> base_col = new List<Derived>();
Process(base_col); // error.
}
static Process(IEnumarable<Base> baseCollection) { }
爲什麼代碼在這裏打破?
如果你認爲這是有趣的,等到你嘗試一個IList傳遞給接受一個IList –