Is there a limit of elements that could be stored in a List?
Asked Answered
O

1

7

Is there a limit of elements that could be stored in a List ? or you can just keeping adding elements untill you are out of memory ?

Option answered 13/11, 2009 at 17:43 Comment(0)
C
20

The current implementation of List<T> uses Int32 everywhere - to construct its backing array, for its Count property, as an indexer and for all its internal operations - so there's a current theoretical maximum of Int32.MaxValue items (2^31-1 or 2147483647).

But the .NET framework also has a maximum object size limit of 2GB, so you'll only get anywhere near the items limit with lists of single-byte items such as List<byte> or List<bool>.

In practice you'll probably run out of contiguous memory before you hit either of those limits.

Clubfoot answered 13/11, 2009 at 17:45 Comment(2)
Theoretically that means you can only randomly access items up to Int32.MaxValue, not how many elements can there be, theoretically, of course.Krone
@Martinho: Well, the current implementation uses Int32 everywhere, so it's not just random access that's restricted to 2^31 items. (Of course, the use of Int32 internally is just an implementation detail, but properties like the indexer and Count are part of the public contract.)Clubfoot

© 2022 - 2024 — McMap. All rights reserved.