Interviews3 answers
How to initialize Dictionary<int, string> using collection initialize?
3 Answers
Dictionary students = new Dictionary()
{
{ 111, new StudentName {FirstName="Sachin", LastName="Karnik", ID=211}},
{ 112, new StudentName {FirstName="Dina", LastName="Salimzianova", ID=317}},
{ 113, new StudentName {FirstName="Andy", LastName="Ruth", ID=198}}
};
The innermost braces enclose the object initializer for the StudentName, and the outermost braces enclose the initializer for the key/value pair that will be added to the students Dictionary. Finally, the whole collection initializer for the dictionary is enclosed in braces.
Manju lata YadavPosted Jul 9, 2014, 7:37 AM EDT
it will store values in key/value pair.And the syntax is
Dictionary obj=new Dictionary();
obj.Add(1,"jan");
obj.Add(2,"feb");
keerthi rakasiPosted Feb 28, 2013, 6:36 AM EST
Dictionary t = new Dictionary() { {1, “a”}, {2, “vb”}, {3, “c”}};
Atulya PandaPosted Nov 26, 2012, 9:32 AM EST
