我試着這樣做:爲什麼IList <T>不包含Length屬性?
int listAverage = NumberList.Sum()/NumberList.Length;
其中NumberList
是IList
。它會拋出一個錯誤:
'System.Collections.Generic.IList' does not contain a definition for 'Length' and no extension method 'Length' accepting a first argument of type 'System.Collections.Generic.IList' could be found (are you missing a using directive or an assembly reference?)
這讓我想,why does List<T>
not have a Length
property?我知道你可以使用Count()
來代替,但Length
的時間複雜度爲O(1)。
因爲它有一個Count屬性... –
如果IList實際上是一個列表,則Count是O(1)。 –
@AndrewWhitaker:根據問題'NumberList'是一個'IList'不是'List',所以雖然它可能實際上是一個List,實際上是O(1)(每個文檔),但它不一定是案件。 –