I have created a list using:
List<int> foo = new List<int>();
I need to be able to insert values into any index after I create this. If I have
foo[325] = 55;
an ArgumentOutOfRangeException is thrown with the following details:
Index must be within the bounds of the List.
Parameter name: index
What must I do in order to correct this? I could fill the list with dummy values and then access the indices after the list is filled, but that seems like a messy solution.
Maybe a List<> isn’t the correct datatype to use for storage.
If you used a
Dictionary<int, int>()you can just maintain the indexes you wish:And to retrieve:
If you plan on actually using every single index, you’d be better off doing:
To pre-fill the list.