73

Possible Duplicate:
Auto-Initializing C# Lists

I have a list of integers that has a certain capacity that I would like to automatically fill when declared.

List<int> x = new List<int>(10);

Is there an easier way to fill this list with 10 ints that have the default value for an int rather than looping through and adding the items?

3
  • 1
    Also stackoverflow.com/questions/466946/… and a few others.
    – Mark Byers
    Jul 29, 2010 at 15:11
  • 1
    Yep, my question was a dupe of that first one...
    – BLG
    Jul 29, 2010 at 15:38
  • It seems duplicated, but actually not, for me. Not all the programmer knows java's array initialization.
    – nyconing
    Apr 13, 2018 at 13:23

5 Answers 5

164

Well, you can ask LINQ to do the looping for you:

List<int> x = Enumerable.Repeat(value, count).ToList();

It's unclear whether by "default value" you mean 0 or a custom default value.

You can make this slightly more efficient (in execution time; it's worse in memory) by creating an array:

List<int> x = new List<int>(new int[count]);

That will do a block copy from the array into the list, which will probably be more efficient than the looping required by ToList.

5
  • 5
    I was hoping there was some sweet little method call to do this like 'new List<int>(10).Fill()' or something. Thanks for the quick answers.
    – BLG
    Jul 29, 2010 at 15:37
  • @JonSkeet: is that possible for custom class list? example: List<Items> listOfItems where public Items {int id; DateTime currentDateTime;} Need to have same values for currentDateTime. possible?
    – Praveen
    May 13, 2013 at 8:59
  • @user1671639: It's not really clear what you're asking for (nor why Items would be plural rather than Item). Why would it be useful to create a list with lots of "default" entries like this? I suggest you ask a new question with more detail.
    – Jon Skeet
    May 13, 2013 at 9:04
  • @JonSkeet: I will ask for a new question. Thanks Jon.
    – Praveen
    May 13, 2013 at 9:09
  • Hey @JonSkeet, how would you do the above for List<List<int>>? I have asked the question here
    – parsh
    Oct 29, 2016 at 0:07
17
int defaultValue = 0;
return Enumerable.Repeat(defaultValue, 10).ToList();
13

if you have a fixed length list and you want all the elements to have the default value, then maybe you should just use an array:

int[] x  = new int[10];

Alternatively this may be a good place for a custom extension method:

public static void Fill<T>(this ICollection<T> lst, int num)
{
    Fill(lst, default(T), num);
}

public static void Fill<T>(this ICollection<T> lst, T val, int num)
{
    lst.Clear();
    for(int i = 0; i < num; i++)
        lst.Add(val);
}

and then you can even add a special overload for the List class to fill up to the capacity:

public static void Fill<T>(this List<T> lst, T val)
{
    Fill(lst, val, lst.Capacity);
}
public static void Fill<T>(this List<T> lst)
{
    Fill(lst, default(T), lst.Capacity);
}

Then you can just say:

List<int> x  = new List(10).Fill();
6

Yes

int[] arr = new int[10];
List<int> list = new List<int>(arr);
2
var count = 10;
var list = new List<int>(new int[count]);

ADD

Here is generic method to get the list with default values:

    public static List<T> GetListFilledWithDefaulValues<T>(int count)
    {
        if (count < 0)
            throw new ArgumentException("Count of elements cannot be less than zero", "count");

        return new List<T>(new T[count]);
    }

Not the answer you're looking for? Browse other questions tagged or ask your own question.