how to initialize list in c# code example

Example 1: instantiate list with values c#

var list = new List<string> {
  "test1",
  "test2",
  "test3"
};

Example 2: c# initialize list of objects

List<Cat> moreCats = new List<Cat>
{
    new Cat{ Name = "Furrytail", Age=5 },
    new Cat{ Name = "Peaches", Age=4 },
    null
};

Example 3: initialize list in c#

List<string> optionList = new List<string>
            { "AdditionalCardPersonAdressType", /* rest of elements */ };

Example 4: c# list with 0 initialize

var tenDoubles = new List<double>(new double[10]);