Create List with values at compile time

List<int> myValues = new List<int>(new int[] { 1, 2, 3 } );

This will create an intermediate array however so there may be a more efficient way of doing the same thing.

EDIT:

John Feminella suggested creating a factory method to accept a list of parameters and return a List which you could implement as follows:

List<T> CreateList<T>(params T[] values)
{
    return new List<T>(values);
}

which you can use as follows:

List<int> myValues = CreateList(1, 2, 3);

Leave a Comment