High memory consumption with Enumerable.Range?

前端 未结 4 629
轻奢々
轻奢々 2020-11-28 16:03

Originally i wanted to know whether ToList allocates more memory than using the constructor of List which takes an IEnumerable

4条回答
  •  [愿得一人]
    2020-11-28 16:43

    I guess that:

    • Enumerable.Range(1, 10000000) only creates an IEnumerable and doesn't create items yet.

    • Enumerable.Range(1, 10000000).ToArray() creates an array, using memory for the numbers

    • Enumerable.Range(1, 10000000).ToList() creates the numbers and additional data to manage the list (links between parts. The list can change its size and needs to allocate memory in blocks).

提交回复
热议问题