Reputation: 169
I have an ArrayList
and I want to give index as big integer value (Customer ID)
ex:
limitAmountTotal[CUSTOMERID] = amount;
where CUSTOMERID = 1000000
But I am getting an error
arraylist is out of range
Is there any way I can handle this in C#?
in PHP we can do this like this
$limitAmountTotal[$CUSTOMERID] = $amount;
Thanks
Upvotes: 0
Views: 975
Reputation: 474
Use List<T>
or simple Array
(array is optional but not recommended for this!).
If your amount
is some monetary value then you can try using:
decimal[] arr = new decimal[arraySize];
or:
List<decimal> list = new List<decimal>();
If not change decimal
to whatever data type you like: int
, long
and etc.
Edit:
Some performance and memory tests:
All collections are type of `int`
Maximum collection size: 1000000
Random generated index used for value lookup: 354497
Array populating time elapsed: 00:00:00.0063499
Array lookup time elapsed: 00:00:00.0000004
Size: 4000028 bytes
==============
List populating time elapsed: 00:00:00.0184629
List lookup time elapsed: 00:00:00.0000004
Size: 4194509 bytes
==============
Dictionary populating time elapsed: 00:00:00.0593676
Dictionary lookup time elapsed: 00:00:00.0000293
Size: 17001335 bytes
Code: https://gist.github.com/rekosko/999e58dc95ba3e4fc678
Upvotes: -2
Reputation: 33573
That's because ArrayList
behaves like an array. You want Dictionary
instead:
Dictionary<int, int> limitAmountTotal = new Dictionary<int, int>();
limitAmountTotal[10000] = 30;
Upvotes: 4