How to work on dictionaries c#



We''ll dig more into what dictionaries are all about, but first, let''s look at a simple example, to show you what it''s all about:

Dictionary<string, int> users = new Dictionary<string, int>();  
users.Add("CshapCode 1", 42);  
users.Add("CshapCode 2", 38);  
users.Add("CshapCode 3", 12);  
users.Add("CshapCode 4", 12);

Console.WriteLine("Developer is " + users["CshapCode 2"] + " years old");

You can also write on this way

Dictionary<string, int> users = new Dictionary<string, int>()
{
    { "Csharp-1", 42 },
    { "Csharp-2", 38 },
    { "Csharp-3", 12 },
    { "Csharp-4", 12 }
};
foreach (KeyValuePair<string, int> user in users)
{
    Console.WriteLine(user.Key + " is " + user.Value + " years old");
}

0 Comment's

Comment Form

Submit Comment