作者:
GoalBased (Artificail Intelligence)
2016-05-30 01:39:20※ 引述《kisha024 (4545454554)》之銘言:
: 各位好
: https://msdn.microsoft.com/zh-tw/library/5y536ey6%28v=vs.110%29.aspx
: 我看 MSDN 有關 IList<T> 介面的說明是 表示可以個別由索引存取之物件的集合
: 加上底下這行程式碼編譯沒有錯
: IList<string> list = new string[] { "A", "B", "C" };
: 所以我本來以為可能是 string:IList<T> 或 Array:IList<T> 但卻都不是
: 最接近的大概是 Array:IList
: 我的疑問是 上面那行程式碼不會錯 就是因為 Array:IList 嗎?
: 如果是 那為什麼可以不用Array:IList<T> 而只要Array:IList就不會出錯了呢?
: 是因為IList是interface的關係嗎?
: 謝謝
查了一下MSDN,一些資料給你參考一下,順便講一下我查詢的過程(灰色部分),
開了vs試著對 string[] 按f12 結果只能進到string class,
所以,google:C# 陣列,得到以下資料:
https://msdn.microsoft.com/zh-tw/library/9b9dty7d.aspx
陣列類型是從抽象基底類型 Array 衍生的參考類型。 由於此類型會實作 IEnumerable
和 IEnumerable<T>,您可以在 C# 中的所有陣列上使用 foreach 反覆運算。
所以進到Array的MSDN,發現
https://msdn.microsoft.com/zh-tw/library/system.array.aspx
從.NET Framework 2.0 開始 Array 類別會實作
System.Collections.Generic.IList<T>,
System.Collections.Generic.ICollection<T>,和
System.Collections.Generic.IEnumerable<T> 泛型介面。
陣列中提供了實作在執行階段,也因此,泛型介面不會顯示在宣告語法 Array 類別。
此外,還有介面的成員只能由轉型為泛型介面型別 (明確介面實作) 的陣列,
可存取的任何參考主題。
這邊就知道了,陣列繼承Array,Array又會實作IList<T>,所以得到答案,
但是進到Array class卻沒看到他說的實作阿,故把該頁MSDN換回英文
https://msdn.microsoft.com/en-au/library/system.array.aspx
The implementations are provided to arrays at run time, and as a result, the
generic interfaces do not appear in the declaration syntax for the Array class
藉由看黃色那一段的原文可以知道,因為是在run time才實作,所以在Array裡面
找不到這一段,結束。
黃色那段直接英翻中我看不懂,所以才去看英文的