views:

188

answers:

6

Hello,

I was wondering if there is any structure in C# that can contain more than Int.MaxValue's restriction of 2,147,483,647 items, in case of really large sets of information. Would this have to be done with multi level arrays? Or could you create an array that has a maximum length of Long.MaxValue? If so, how?

+1  A: 

Very little computers will have enough memory to hold such structure in-memory.

Darin Dimitrov
That's no longer true.
SLaks
@SLaks, then maybe I should add: very little computers will handle such structure in-memory.
Darin Dimitrov
A: 

ArrayList can contain more than Int.MaxValue.

Wildhorn
Nope, it cannot, because the constructor takes only an integer for capacity, and thus cannot be more than Int32.MaxValue
Richard J. Ross III
@Richard: Your logic isn't *completely* sound, as the capacity increases automatically. You could provide `int.MaxValue` and then add more items. I'm pretty sure ArrayList would still fail, but not just because you couldn't specify the *final* capacity to the constructor as the *initial* capacity.
Jon Skeet
+2  A: 

You could only use an index based structure if the indexer was a long (int64) rather than an int32.

Even a List wouldn't work as the indexer is an int32.

ChrisF
Yep, that's what I thought...oh well, looks like I need to cut back on the information.
Richard J. Ross III
+2  A: 

What is your requirement? It's hard to believe that an array with more elements than this is going to be the correct solution for any real-world problem, irrespective of whether it's legal or not. You may be thinking of a database here, whether persisted or in-memory. That's the standard means for organizing very large datasets.

Steve Townsend
+3  A: 

The CLR currently has a limit of 2GB for any single object - so you'd have to build it out of multiple arrays, even for an element type of byte. That should be feasible though. Most of the normal collection interfaces would fail as they use int for the index, count etc.

I believe the CLR itself isn't restricted to an overall process limit which would prevent this - although you'd almost certainly want to be running on a 64-bit CLR and OS, of course.

Jon Skeet
Which is all a bit odd since there's an overload of GetValue on System.Array that takes a long as index suggesting that you could have way larger arrays (http://msdn.microsoft.com/en-us/library/2zexc3z9.aspx)
Rune FS
@Rune: Yes - I agree it's very odd. A bit of premature design, potentially.
Jon Skeet
@Rune: The current implementation of `GetValue(Int64)` throws an `ArgumentOutOfRangeException` if you pass an argument that's outside the range of an `Int32`. Similarly, there's also a `LongLength` property which returns an `Int64`, but actually just casts and returns the standard `Int32` `Length` property.
LukeH
@LukeH - yes and that's the odd part when they don't add anything (except for misleading the unfortunate) why add them at all. Adding them when they would actually be supported would not be a breaking change. (Removing them no of course would)
Rune FS
+5  A: 

It's been done, a sample BigArray<T> implementation is here.

Hans Passant
Cool! Thanks for giving me the link to this!
Richard J. Ross III