I have a List<T>
and want to start adding from the bottom but I'm getting a runtime IndexOutOfBoundsException
.
I have initialized a list with a capacity:
List<ClassA> ClassesOfA = new List<ClassA>(10);
...
...
ClassesOfA[5] = classAObj;
...
Is there anyway to do this?
I need to do this because I'm analyzing another list from the bottom and adding the result to this list. So I need to be able to add from the bottom.
Is there any way to do this rather than initializing the List<ClassA>
with ClassA
objects before adding my objects?
To add items to the list, you should be calling ClassesOfA.Add()
. Any items added this way will be added to the end of the list (not sure if the beginning of the list or the end of the list is what you consider the bottom).
You can try adding the elements like this: ClassesOfA.Add(classAObj)
, then when all the list elements are added you can call ClassesOfA.Reverse()
to reverse the order of the objects inside the list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With