what is the max limit of data into list<string> in c#?

Viewed 72905

How many values I can add to List?

For example:

List<string> Item = runtime data

The data is not fixed in size. It may be 10 000 or more than 1 000 000. I have Googled but have not found an exact answer.

5 Answers

As per the implementation of List

private void EnsureCapacity(int min) {
    if (_items.Length < min) {
        int newCapacity = _items.Length == 0? _defaultCapacity : _items.Length * 2;
        // Allow the list to grow to maximum possible capacity (~2G elements) before encountering overflow.
        // Note that this check works even when _items.Length overflowed thanks to the (uint) cast
        if ((uint)newCapacity > Array.MaxArrayLength) newCapacity = Array.MaxArrayLength;
            if (newCapacity < min) newCapacity = min;
                Capacity = newCapacity;
        }
    }

Now upon navigating to this Array.MaxArrayLength:

internal const int MaxArrayLength = 2146435071;

You can get around this limit easily with a List<char[]>.

Related