Forum guidelines
ahmed sami
Limitless Array .. ?
Posted By ahmed sami on 16 Dec 2012
is there a limitless array in c# lang. ?
and if so ..
how to identify it ?

  • 0
  • 0
Re: Limitless Array .. ?
Posted By Vulpes on 17 Dec 2012   Accepted Answer
Well, in theory, an array in .NET can hold up to (approximately) 2 billion elements. However, in practice you'll probably have run out of memory well before then.

Once created an array always maintains its original size. So, if you create an array with a thousand elements, it will always have a thousand elements until it is disposed of.

You can resize an array (using Array.Resize) but all you're really doing is creating another array (of the required size) and copying the elements of the old one to it.

If you don't know in advance how many elements an array will need, then it's usually preferable to create an ArrayList or (better) a List<T>. These start with a (usually low) initial capacity which is doubled automatically as new elements are added. However, as these classes use an array for their internal storage, the doubling process can only go on ( memory permitting) as long as the limit of 2 billion elements is not exceeded.

So, the strict answer to your question is 'no' - there is no such thing as a limitless array in C#.

C# Corner Hyderabad Developer Conference


Spire.Doc - Free .NET Word API
Use Spire.Doc to create, read, write, print, and convert word documents to OpenXML, RTF, TXT, XPS, EPUB, EMF, HTML, Image and PDF.