-1

I am attempting to add a value to a list at a specific index, which according to everything I've read, should be do-able with List's Insert() method.

Except I'm getting the exception "Index must be within the bounds of the List. Parameter name: index`

Here is my complete sample code.

class Program
{
    static void Main(string[] args)
    {
        MyList myList = new MyList();
    }
}
class MyList : List<int>
{
    public MyList()
    {
        this.Capacity = 2;
        this.Insert(1, 0);
    }
}

Is the only option to change to a nullable list type and call .Add(null) for each index and not use Insert()?

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
UpTide
  • 307
  • 3
  • 13

2 Answers2

2

You cannot insert an item beyond the last item, this is why you got the error message of out of bounds.

Setting the capacity of a list can be interesting if you know in advance that the list would not grow above a certain size, the Capacity.

Setting the capacity does not create any elements in the list.

PepitoSh
  • 1,774
  • 14
  • 13
1
  • Capacity is not equal to size of the list, its the internal size before it internally resizes , its for performance
  • Lists don't start at 1, they start at 0

List.Capacity Property

Gets or sets the total number of elements the internal data structure can hold without resizing.

You are trying to add something to a bucket that doesn't exist

Update

I'm trying to insert an item after the last index, that's the point of using 1.

From your code you don't have anything at 0 to add anything at 1

Add something to 0 first before you add something to 1

Also don't inherit form list, its bad form for lots of reasons.

var myAwesomeList = new List<int>();
// add the number 10
myAwesomeList.Add(10);
// insert the number 7 before 10
myAwesomeList.Insert(0,7);
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • I'm trying to insert an item after the last index, that's the point of using 1. Is there a way to set the size? – UpTide Jun 11 '18 at 04:20
  • @UpTide "After the last index" -> the last index in an _empty_ list is effectively `-1`, surely? – ProgrammingLlama Jun 11 '18 at 04:22
  • This question is solved elsewhere on stackoverflow. I'm going to mark this as duplicate. – UpTide Jun 11 '18 at 04:24
  • @UpTide as you wish, it would pay to spend 5 minutes searching before you ask a question next time – TheGeneral Jun 11 '18 at 04:27
  • @TheGeneral as usual my problem was not knowing what the problem was named. Namely your first sentence of "Capacity is not equal to size", which sparked the search for "how to set size of list c#". `List` does not have a property called `Size`, it's not as intuitive as you would think (especially with a property called `Capacity`). – UpTide Jun 11 '18 at 04:47
  • @UpTide i understand your frustration and i remember making it once myself, however the Microsoft documentation is pretty handy https://msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396 – TheGeneral Jun 11 '18 at 04:48
  • @TheGeneral this is just one of those things you have to "know" since setting size isn't mentioned on the List page or the page for the specific exception I had. – UpTide Jun 11 '18 at 04:55