문제

Is there a difference between getting an array into a List via array.ToList() method and to do do it via new List(array) in the code below, in terms of its performance and its costs under the hood?

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();
도움이 되었습니까?

해결책

No; the Enumerable.ToList<T> extension method is just a wrapper that calls the List(IEnumerable<T>) constructor.

The only additional costs you'll incur from ToList are an extra method call and nullity check, which are typically negligible (and might even be eliminated by the JIT compiler).

다른 팁

Internally, here's what the call to array.ToList() (Enumerable.ToList()) looks like:

if (source == null)
    throw Error.ArgumentNull("source");

return new List<TSource>(source);

So your second example runs the same code as your first example.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top