-1

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?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Glimpse
  • 462
  • 2
  • 8
  • 25

5 Answers5

16

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;
}
MarcinJuraszek
  • 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
1

This can be easily done with an array:

string[] sa = new string[99];
sa[71] = "g";

Which also happens to implement the IList interface.

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
RBarryYoung
  • 55,398
  • 14
  • 96
  • 137
  • 4
    You cannot insert an item into an array. You can only replace a value. – Servy Apr 08 '13 at 13:50
  • 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
-1

List Constructor (Int32)

List.Insert Method

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.

paparazzo
  • 44,497
  • 23
  • 105
  • 176
-2

You can use the constructor overloading List<T>(int capacity):

var l = new List<string>(42);

creates a list with a capacity of 42.

pascalhein
  • 5,700
  • 4
  • 31
  • 44
  • 1
    I've downvoted. See my answer and you'll know why. – MarcinJuraszek Apr 05 '13 at 21:05
  • 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
-2

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.

LuiNova
  • 396
  • 3
  • 3