tags:

views:

63

answers:

3

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?

A: 

I believe it depends on your virtual memory size...

Bryce Fischer
It doesn't. No object in .NET can be larger than 2 GB.
Brian Rasmussen
A: 

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
+2  A: 

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).

Jeremy Collake
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