I created a list as a class level field like this:
private readonly IList<int> _intCol = new List<int>();
I then index into it and it blows up with the exception below:
_intCol[0] = 0;
Exception:
Index was out of range. Must be non-negative and less than the size of
the collection.Parameter name: index
At this point,
_intColis a list of the size 0, it does not have an element on the first position.You can use
_intCol.Add(0);.See also:
List<T>.Add(T)List<T>.Insert(int, T)If you do want to insert elements that way, you can use a
Dictionary<int,int>, but note that your elements are not ordered – you just map numbers to numbers. For example:integersnow has two items, in no particular order: