C# List Initialization

How to initialize a List<T> in C#

In this tutorial, you will learn how to initialize a list in C#. We will show you examples of creating an empty list and a list with initial values.

See also:

Initialize a list in C#

Creating an empty list

To create a new empty list, you can write as follows:

List<DataType> variable name = new List<DataType>();

Example:

List<int> numbers = new List<int>();

Before you can use the List<T> class, you need to declare the following namespace:

using System.Collections.Generic;

Initializing a list

To initialize a new list with values, you can write as follows:

List<DataType> variable name = new List<DataType>() { value1, value2, value3, value4, };

Example:

List<int> numbers = new List<int>() {1, 2, 3, 4, 5, 6};

Examples of C# List<T> initialization

The following sample program is an example of List<T> initialization in C#:

  1. using System;
  2. using System.Collections.Generic;
  3. namespace CSharp_List_Initialization
  4. {
  5. public class Program
  6. {
  7. public static void Main(string[] args)
  8. {
  9. //Create an empty list
  10. List<int> list = new List<int>();
  11. Console.WriteLine("The number of elements: {0}", list.Count);
  12. //Add items to the list
  13. list.Add(1);
  14. list.Add(3);
  15. list.Add(2);
  16. list.Add(4);
  17. list.Add(5);
  18. Console.WriteLine("The number of elements: {0}", list.Count);
  19. //Print elements of the list
  20. Console.WriteLine("{0}", string.Join(",", list));
  21. }
  22. }
  23. }
  24.  

Output:

The number of elements: 0
The number of elements: 5
1,3,2,4,5

The following sample program shows how to initialize a list:

  1. using System;
  2. using System.Collections.Generic;
  3. namespace CSharp_List_Initialization
  4. {
  5. public class Program
  6. {
  7. public static void Main(string[] args)
  8. {
  9. //Create an empty list
  10. List<int> list = new List<int>() { 1, 2, 3, 4, 5 };
  11. Console.WriteLine("The number of elements: {0}", list.Count);
  12. //Print elements of the list
  13. Console.WriteLine("{0}", string.Join(",", list));
  14. //Add items to the list
  15. list.Add(6);
  16. list.Add(7);
  17. list.Add(8);
  18. list.Add(9);
  19. list.Add(10);
  20. Console.WriteLine("The number of elements: {0}", list.Count);
  21. //Print elements of the list
  22. Console.WriteLine("{0}", string.Join(",", list));
  23. //Remove
  24. list.Remove(6);
  25. list.Remove(7);
  26. Console.WriteLine("The number of elements: {0}", list.Count);
  27. //Print elements of the list
  28. Console.WriteLine("{0}", string.Join(",", list));
  29. }
  30. }
  31. }

Output

The number of elements: 5
1,2,3,4,5
The number of elements: 10
1,2,3,4,5,6,7,8,9,10
The number of elements: 8
1,2,3,4,5,8,9,10

Summary

In this tutorial, you learned how to initialize a list in C#. There are two ways to create a list: an empty list or a list with initial values.


See also:
C# List Contains with Examples
C# typeof with Examples
C# TryParse (int, double, float) with Examples
C# Data Types
C# Variables