[英]Difference between int[,] array = new int[4, 2]; and int[] test = { 4, 2 };
[英]Is there a difference between new List<int>(array) and array.ToList()?
就其性能和內部成本而言,通過array.ToList()方法將數組放入List與通過以下代碼中的new List(array)進行數組之間有區別嗎?
IEnumerable<int> array = new int[]{1,2,3,4,5,6,7,8};
List<int> list_Foo = new List<int>(array);
List<int> list_Bar = array.ToList();
沒有; Enumerable.ToList<T>
擴展方法只是一個包裝,它調用List(IEnumerable<T>)
構造函數。
您將從ToList
產生的唯一額外費用是額外的方法調用和無效檢查,這些費用通常可以忽略不計(甚至可以由JIT編譯器消除)。
在內部,這是對array.ToList()
( Enumerable.ToList()
)的調用的樣子:
if (source == null)
throw Error.ArgumentNull("source");
return new List<TSource>(source);
因此,第二個示例運行與第一個示例相同的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.