How To Initialize A List In .NET

How To Initialize A List Banner Image

Introduction

One of the first things to do when creating a list is to initialize them or pre-populating them with data. In .NET, There are two main ways to initialize the lists. One is at compile time, which sets the values before the app is started. The other way is at runtime, which adds values to the list after the app has started. These could be small datasets, a list that holds types of configuration or test data, etc.

Initialize A List At Compile Time

One of the go-to ways to initialize the data. When it happens before the program starts then the visual studio will check to see if the syntax is correct. There are good reasons to do it this way. It's easy to read and out of the way. Perhaps there is a drop-down list that needs to be populated. Usually doing it this way means there are few values to initialize to the list.

Initialize A List At Compile Time Example Code
List<string> stringList = new List<string>() { "first", "second", "third", "fourth", "fifth" };//Use double quotes for string
List<int> intList = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };//Only whole integer values
List<double> doubleList = new List<double>() { 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0 };//large number with decimal places can be used.
List<float> floatList = new List<float>() { 1.0f, 2.0f, 3.0f, 4.0f, 5.1f, 6.2f };//floats are similar to double but the float has f at the end
List<char> charList = new List<char>() { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'f' };//Characters are surrounded by single quotes 
List<Car> carList = new List<Car>() { new Car(2, "SuperHType"), new Car(3, "XType"), new Car(4, "TruckXType"), new Car(4, "SUVType") };//Add any Object to the list as long as it has a defined constructor

Console.WriteLine(string.Join(",", stringList));//Print list to screen
Console.WriteLine(string.Join(",", intList));//Print list to screen
Console.WriteLine(string.Join(",", doubleList));//Print list to screen
Console.WriteLine(string.Join(",", floatList));//Print list to screen
Console.WriteLine(string.Join(",", charList));//Print list to screen
Console.WriteLine(string.Join(",", carList));//Print list to screen

class Car
{
    public Car(int wheels, string type)
    {
        Wheels = wheels;
        Type = type;
    }
    public int Wheels { get; set; }
    public string Type { get; set; }
    public override string? ToString()
    {
        return $"Wheels:{Wheels}; ; Type:{Type}";
    }
}
Code Output
first,second,third,fourth,fifth
1,2,3,4,5,6,7,8,9,10
1,1.5,2,2.5,3,3.5,4
1,2,3,4,5.1,6.2
a,b,c,d,e,f,g,h,f
Wheels:2; Type:SuperHType,Wheels:3; Type:XType,Wheels:4; Type:TruckXType,Wheels:4; Type:SUVType

This example gives different types and how they are initialized. They are different syntaxes for each type. For strings, require double quotes. Ints are whole numbers. Double can be whole or numbers with decimals. Float has an f character at the end of the number and can be whole or decimals numbers. Characters are created by single quotes.

Initialize A List At Run Time

Another way to set a list to initial values is to use Add. We can use hard-coded values or variables. Perhaps we need to set the list of initials from the database or some files. Initializing this way, it is possible to add a great deal of data to the list.

Initialize A List At Run Time Example Code
List<string> stringList = new List<string>();//create a new list of strings
List<int> intList = new List<int>();//create a new list of ints
List<double> doubleList = new List<double>();//create a new list of doubles
List<float> floatList = new List<float>();//create a new list of floats
List<char> charList = new List<char>();//create a new list of characters
List<Car> carList = new List<Car>(); //Add any Object to the list as long as it has a defined constructor

stringList.Add("first");//Add string to a list
stringList.Add("second");//Add string to a list
stringList.Add("third");//Add string to a list
stringList.Add("fourth");//Add string to a list
stringList.Add("fifth");//Add string to a list

intList.Add(1);//Add int to a list
intList.Add(2);//Add int to a list
intList.Add(3);//Add int to a list
intList.Add(4);//Add int to a list
intList.Add(5);//Add int to a list
intList.Add(6);//Add int to a list
intList.Add(7);//Add int to a list
intList.Add(8);//Add int to a list
intList.Add(9);//Add int to a list

doubleList.Add(1.0);//Add double to a list
doubleList.Add(2.0);//Add double to a list
doubleList.Add(3.0);//Add double to a list
doubleList.Add(4.0);//Add double to a list
doubleList.Add(5.0);//Add double to a list
doubleList.Add(6.0);//Add double to a list
doubleList.Add(7.0);//Add double to a list
doubleList.Add(8.0);//Add double to a list
doubleList.Add(9.0);//Add double to a list

floatList.Add(1.1f);//Add float to a list
floatList.Add(2.2f);//Add float to a list
floatList.Add(3.3f);//Add float to a list
floatList.Add(4.4f);//Add float to a list
floatList.Add(5.5f);//Add float to a list
floatList.Add(6.6f);//Add float to a list
floatList.Add(7.7f);//Add float to a list
floatList.Add(8.8f);//Add float to a list
floatList.Add(9.9f);//Add float to a list

charList.Add('a');//Add character to a list
charList.Add('b');//Add character to a list
charList.Add('c');//Add character to a list
charList.Add('d');//Add character to a list
charList.Add('e');//Add character to a list
charList.Add('f');//Add character to a list
charList.Add('g');//Add character to a list
charList.Add('h');//Add character to a list
charList.Add('i');//Add character to a list

carList.Add(new Car(3, "SuperHType"));
carList.Add(new Car(4, "CompactModel"));
carList.Add(new Car(4, "SUVType"));
carList.Add(new Car(4, "TruckType"));

Console.WriteLine(string.Join(",", stringList));//Print list to screen
Console.WriteLine(string.Join(",", intList));//Print list to screen
Console.WriteLine(string.Join(",", doubleList));//Print list to screen
Console.WriteLine(string.Join(",", floatList));//Print list to screen
Console.WriteLine(string.Join(",", charList));//Print list to screen
Console.WriteLine(string.Join(",", carList));//Print list to screen

class Car
{
    public Car(int wheels, string type)
    {
        Wheels = wheels;
        Type = type;
    }
    public int Wheels { get; set; }
    public string Type { get; set; }
    public override string? ToString()
    {
        return $"Wheels:{Wheels}; Type:{Type}";
    }
}
Code Output
 first,second,third,fourth,fifth
 1,2,3,4,5,6,7,8,9
 1,2,3,4,5,6,7,8,9
 1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9
 a,b,c,d,e,f,g,h,i
 Wheels:3; Type:SuperHType,Wheels:4; Type:CompactModel,Wheels:4; Type:SUVType,Wheels:4; Type:TruckType

With the lists populated with initial data, they can be used for whatever purpose or checks are needed.

C# Programming for Unity Game Development Specialization

COURSERA

C# Programming for Unity Game Development Specialization

4.7 (2,230 reviews)

Beginner level

No previous experience is necessary

3-months at 10 hours a week (At your pace)

$59 / month or audit, and financial aid available

If you want to learn more about C# then check out this course on Coursera. It's a 3 month course to take you through the basics of C#.

You can get started with programming using C# and apply it to Unity games in this beginner-level program. Facilitated by the University of Colorado, the program offers a chance to grasp the basics of C# and utilize the knowledge to develop Unity games.

[Full Disclosure: As an affiliate, I receive compensation if you purchase through this link]

Start Your 7-day Free Trial

Conclusion

RankMethodTimeConciseReadability(1-5)
1List Inline InitializeCompile Time44
2List Initialize By AddRun Time34

Initializing the list using the in-line method is best used when there are only a few items because of the typing involved and also where the list won't change much very often. These are hardcoded values. But if you plan to initialize a list to a large amount of data from a file or database then going with a dynamic approach is preferable.

Get Latest Updates
Comments Section