Tutorial by Examples

Initialize a collection type with values: var stringList = new List<string> { "foo", "bar", }; Collection initializers are syntactic sugar for Add() calls. Above code is equivalent to: var temp = new List<string>(); temp.Add("foo"); temp.A...
Starting with C# 6, collections with indexers can be initialized by specifying the index to assign in square brackets, followed by an equals sign, followed by the value to assign. Dictionary Initialization An example of this syntax using a Dictionary: var dict = new Dictionary<string, int> ...
To make a class support collection initializers, it must implement IEnumerable interface and have at least one Add method. Since C# 6, any collection implementing IEnumerable can be extended with custom Add methods using extension methods. class Program { static void Main() { va...
You can mix normal parameters and parameter arrays: public class LotteryTicket : IEnumerable{ public int[] LuckyNumbers; public string UserName; public void Add(string userName, params int[] luckyNumbers){ UserName = userName; Lottery = luckyNumbers; } } ...
public class Tag { public IList<string> Synonyms { get; set; } } Synonyms is a collection-type property. When the Tag object is created using object initializer syntax, Synonyms can also be initialized with collection initializer syntax: Tag t = new Tag { Synonyms = new List&...

Page 1 of 1