How to insert values into C# Dictionary on instantiation?
Solution 1:
There's whole page about how to do that here:
http://msdn.microsoft.com/en-us/library/bb531208.aspx
Example:
In the following code example, a
Dictionary<TKey, TValue>
is initialized with instances of typeStudentName
:
var students = new Dictionary<int, StudentName>()
{
{ 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}}
};
Solution 2:
Dictionary<int, string> dictionary = new Dictionary<int, string> {
{ 0, "string" },
{ 1, "string2" },
{ 2, "string3" } };