Hi, I am trying to do
var test = new List< int>(10);
test.Insert(1, 0);
or
test[1] =0;
I am getting exception at this.
How can I insert to list?
Thanks space is there to get this editor show things properly.
Hi, I am trying to do
var test = new List< int>(10);
test.Insert(1, 0);
or
test[1] =0;
I am getting exception at this.
How can I insert to list?
Thanks space is there to get this editor show things properly.
var test = new List<int>(10);
test.Add(1);
or
var test = new List<int>(10);
test.Insert(0, 1) // inserts the value in the first position
UPDATE
I missed your original intent. To do what you want, you just have to initialize each position in the list with an initial value. The not so suggested way would be something like:
//var test = new List<int>(10){0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
var test = new List<int>(10);
test.AddRange(Enumerable.Repeat(0, 10)); // Thanks to Ahmad
test[3] = 10; // works now
You're allocating an empty list with the capacity to hold 10 items before re-allocating. Then you're inserting into the middle of it. This is why it's failing.
edit
You seem to think that it's like an array, where it actually allocated ten slots to start with. It's not. If you want ten slots, you need to add them yourself. Once you do, it's possible to reference positions in the middle.
The integer parameter of the list constructor defines the capacity of the list, which means how many elements the list can hold maximally. After you have instanciated the list, it is empty and is capable of holding 10 values (although I'm not sure if 10 isn't just the initial capacity, which will grow on demand). Anyway, when you are trying to insert at position 1 (the second element), you get an exception because the list does not contain any elements.
I think what you like to use is an array:
int[] integers = new int[10]; // Creates an array which holds 10 values,
// initially all values are zero.
integers[1] = 123; // Modifiy the value at index 1.
Best Regards
Oliver Hanappi
You cannot insert into a position until after you've added enough elements to make the list at least that large. This is clearly documented in the List's Insert documentation:
ArgumentOutOfRangeException can be thrown when:
If you're just trying to set specific elements, you can add in all 10 elements, then use the indexer:
test[1] = 0;
Otherwise, you can add an element, then insert:
test.Add(0); // Now the list has an element
test.Insert(1,0); // Insert into element 1...
Assuming you want to create a list of 10 ints and set the value of 1. You could
var test = Enumerable.Repeat<int>(0, 10).ToList();
test[1] = 0;
Which would create a List of 10 ints (all equal to 0) and then set the value of index 1 to 0. Obviously as the list is initialised with values of 0, the second line is unnecessary.
Hi All,
Thanks for all the answers.
My list is
var x = new List<ISomeInterface>(10);
I cannot do repeat on interface and I don't know what object it is. Since it's pretty generic.
The solution I used:
var x = new ISomeInterface[10];
x.ToList();