Instantiate list with values c#
C# Code 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class Program { static void Main(string[] args) { var list = new List<string> { "test1", "test2", "test3" }; list.ForEach(n => Console.WriteLine(n)); Console.ReadLine(); } } |
C# initialize list of objects
C# Code 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
class Cat { public String Name; public int Age; } class Program { static void Main(string[] args) { List<Cat> cats = new List<Cat> { new Cat{ Name = "Brownie", Age=3 }, new Cat{ Name = "Punch", Age=1 } }; cats.ForEach(cat => Console.WriteLine(cat.Name)); Console.ReadLine(); } } |
Output:
1 2 3 4 |
Brownie Punch |