C# generic collections offer a lot of flexibility and power for organizing your program's data. However, if you need to set one up manually, you may have written code that looks something like this:
List<string> list1 = new List<string>(); list1.Add("list1 item1"); list2.Add("list1 item2"); list2.Add("list1 item3"); List<string> list2 = new List<string>(); list2.Add("list2 item1"); list2.Add("list2 item2"); list2.Add("list2 item3"); List<string> list3 = new List<string>(); list2.Add("list3 item1"); list2.Add("list3 item2"); list2.Add("list3 item3"); Dictionary<string, List<string>> dictionary = new Dictionary<int, List<string>>(); dictionary.Add("list1", list1); dictionary.Add("list2", list2); dictionary.Add("list3", list3);
Wouldn't it be nice if you could declare a dictionary in one statement? Or a list? Or an array? Or even an entire class, complete with all data members, which may themselves be other classes, lists, dictionaries, or arrays? Well, turns out that you can. Find out how after the break!