This post will discuss how to initialize a List in a single line in C#.

We can initialize a list inline in C# using the new List<Type> { Value1, Value2, .., ValueN }; syntax. This is demonstrated below:

Download  Run Code

 
The List<T> provides a constructor that initializes a new instance of the List<T> class containing elements copied from the specified collection. The following example demonstrates this by creating an array of strings and passing it to the List<T> constructor.

Download  Run Code

 
We can do this in a single step by combining the list constructor with the array initializer syntax.

Download  Run Code

That’s all about initializing a List in a single line in C#.