tags:

views:

290

answers:

6

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.

+11  A: 
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
Justin Niessner
The underlying issue is that they're trying to insert into the middle. Using Add and Insert(0,x) are both ways to insert into the front.
Steven Sudit
The whole purpose is to insert items at desired location. Order is important.
LibraRocks
Add inserts at the end, not at the beginning!
Oliver Hanappi
Yes, Add appends to the end. Insert wouldn't.
Justin Niessner
Right, and inserting to the middle won't work unless there's a middle.
Steven Sudit
oh... that will be not be a good solution.. if the size is dynamic. but yes it will work.
LibraRocks
A more compact way would be: *var list = new List<int>(Enumerable.Repeat(0, 10));* or keep the size specification then add *list.AddRange(Enumerable.Repeat(0, 10));* - makes it more pleasant to look at IMHO :)
Ahmad Mageed
+4  A: 

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.

Steven Sudit
I know why its failing but whats the workaround?I want to be able to use like arrays.
LibraRocks
Add 10 zero elements to it?
CannibalSmith
So call Add 10 times, passing in a 0 each time. This will give you a list with 10 elements, each holding 0. Then you can do what you want.
Steven Sudit
Beat me by 5 seconds, but yes, exactly. :-)
Steven Sudit
A: 

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

Oliver Hanappi
The capacity value you pass to the constructor is indeed just the initial capacity. It will grow.
Joren
Thanks for explaining my diagnosis clearly, but that's not really a solution.
Steven Sudit
A: 

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:

  • index is less than 0, or
  • index is greater than Count.

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...
Reed Copsey
+2  A: 

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.

JDunkerley
This works just as well: *var list = new List<int>(Enumerable.Repeat(0, 10));*
Ahmad Mageed
This really is the clearest answer given here. +1
Joren
+1  A: 

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();
LibraRocks
You can use null, no?
Steven Sudit
Steven, It doesnt work.var test = new List< IInterfaceX >(10);test.AddRange(Enumerable.Repeat(null, 10));It needs explicit argument.
LibraRocks
The solution you suggest allocated an array that is immediately discarded, and uses a relatively expensive and complex LINQ method to initialize a new list. I am not comfortable with this and would instead recommend something simpler, such as a for loop that calls Add(null) in its body.
Steven Sudit
I suspect you could simply cast the null to ISomeInterface and it would then work, but I haven't tried it and probably wouldn't recommend it.
Steven Sudit