1

Possible Duplicate:
What is the Maximum Size that an Array can hold?

i want to define new array for example:

string[] str=new string[maxsize];

now the maximum number that i can set for maxsize is what?

Community
  • 1
  • 1
Farna
  • 1,157
  • 6
  • 26
  • 45

2 Answers2

3

UPDATED: According to comments below, .NET limits arrays to 2GB of members (or bytes?). I'd guess this is due to it using a signed 32-bit integer for the index, but that's just a wild guess.

Regardless, the maximum is nowhere near what you should ever need in day to day use. The larger question is why you would want to allocate an array with that many members. If you need a particularly huge array then you should allocate one on-demand the specific size you need (or use a dynamically resizing container).

... BUT if you have some special need, you could always link multiple arrays together. The last member of each array could point to the next array (or point to nothing to end the single linked list).

dyasta
  • 2,056
  • 17
  • 23
  • the 2GB is bytes, which for ref-types means space for references (not the objects themselves), but for structs means space for the values. – Marc Gravell Sep 26 '10 at 20:10
  • The limit is 2GB (bytes) and not necessarily 2G elements (indexes). This is due to the CLR having a maximum object size of 2GB. So if the array is of byte data type then you can have ~2G elements, if the array is of ushort data type then you can only have ~1G elements (1G * 2 bytes = 2GB). Linking multiple arrays together can be problematic depending on what the software is doing as various functions may be across multiple arrays. For this to work well a special BigArray class would be required which managed the external ulong index into the multiple smaller arrays. – deegee Aug 06 '13 at 22:51
0

I am not sure whether I properly understood your requirement, but here are my comments.

  1. Why do you need to set the maxsize for the new array ? instead you could simply make use of list which will dynamically get increase as required.

    for ex : List<string> str = new List<string>();

    http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

  2. Also, if you set some maxsize to array then probably your program could waste precious memory.

Karthik Mahalingam
  • 1,071
  • 8
  • 10