How do I set the initial size of a list of a certain object type in .Net using C#, with the ability to insert fully allocated objects at a specified index?
-
1Do you mean `List
`? Do you definitely need to insert, or only overwrite? – Jon Skeet Apr 05 '13 at 20:57 -
Using `List
` can't you just use `Insert(index, item)`? – Mathew Thompson Apr 05 '13 at 20:59 -
1@mattytommo Sure you can. It'll throw an index out of range exception, bug you can compile it. – Servy Apr 05 '13 at 21:04
-
2@Servy Not if you're inserting at a position that isn't larger than the list's size. – Mathew Thompson Apr 05 '13 at 21:09
-
What you want is an array, which also implements the IList
interface. – RBarryYoung Apr 05 '13 at 21:12 -
possible duplicate of [How to initialize a List
to a given size (as opposed to capacity)?](http://stackoverflow.com/questions/466946/how-to-initialize-a-listt-to-a-given-size-as-opposed-to-capacity) – joce Apr 06 '13 at 15:46
5 Answers
There is List<T>
constructor which takes an int
as a parameter for initiall list capacity, but it does not actually creates that number of elements within a list, so this will throw ArgumentOutOfRangeException
:
var items = new List<int>(10);
items[4] = 3;
You can create your own method for creating that kind of List
, with initial size:
private static List<T> CreateList<T>(int capacity)
{
return Enumerable.Repeat(default(T), capacity).ToList();
}
It'll make it work:
var items = CreateList<int>(10);
items[4] = 3;
However - why don't you just use Array
instead of List
when you know required capacity?
No-LINQ version
private static List<T> CreateList<T>(int capacity)
{
List<T> coll = new List<T>(capacity);
for(int i = 0; i < capacity; i++)
coll.Add(default(T));
return coll;
}

- 124,003
- 15
- 196
- 263
-
@MarcinJuraszek Thanks for the answer, but this isn't working for me so far. Here's what I have. `subFields = CreateList
(numOfComponents);` `subFields.Insert(subField.segIndex, field);` `Exception Thrown: System.NullReferenceException: Object reference not set to an instance of an object.` – Glimpse Apr 05 '13 at 22:09
This can be easily done with an array:
string[] sa = new string[99];
sa[71] = "g";
Which also happens to implement the IList interface.

- 3,249
- 3
- 24
- 42

- 55,398
- 14
- 96
- 137
-
4
-
1@Servy As already pointed out many times, the phrase "insert at a specific location" is manifestly ambiguous, and implies "overwrite" or "inserting into a slot" as much or more than it implies the "insert and push-out" of linked-lists. When you talk about inserting at specific locations, you usually intend for objects at other locations to stay put rather than to change location. – RBarryYoung Apr 08 '13 at 14:27
-
If you didn't intend any of the other objects to change location then the appropriate term would be "replace" or "overwrite". Not "insert". That is not an appropriate use of the term if that's the intended meaning. – Servy Apr 08 '13 at 14:33
-
@Servy Sorry, but that's a parochially narrow view of how the language works. The term "Insert", even in the strictest technical CS jargon has at least three valid meanings, two of which could be intended here. Specifically, the phrase "Insert a value into an array", has for 50 years meant to assign a value to a particular array offset, without normally implying the intention of shifting the values at that offset (and all higher offsets) to the next higher array offset. Insert only clearly implies that shift-up when applied to linked-lists. – RBarryYoung Apr 08 '13 at 20:05
-
I don't think I've ever seen `Insert` used with respect to inserting values into an array. You generally *set* the value of an array, you don't insert a value into an array. – Servy Apr 08 '13 at 20:08
Down voters that claim Insert fails.
Did you read the documentation in the link?
ArgumentOutOfRangeException if index is less than 0 -or- index is greater than Count.
So capacity and count are not the same - does not make the answer wrong.
The answer from MarcinJuraszek will throw an ArgumentOutOfRangeException if index is less than 0 -or- index is greater than Count.
I use exactly this in a production application to load in alphabetic order and then insert any user additions at index 0 and it has never thrown an exception.

- 44,497
- 23
- 105
- 176
You can use the constructor overloading List<T>(int capacity)
:
var l = new List<string>(42);
creates a list with a capacity of 42.

- 5,700
- 4
- 31
- 44
-
1
-
OK, I read the question as "when filling the list, I want to be able to insert items" instead of "I want to be able to insert items right from the beginning" (eg: add item 0, item 1, insert item between 0 and 1) which works with `List
.Insert` – pascalhein Apr 05 '13 at 21:08
To my knowledge setting an initial size to a list goes against the whole idea behind a List compared to an array.
But here is how you do it:
List<ItemType> list = new List<ItemType>(size);
size is int data type.

- 396
- 3
- 3