I couldn't find a relevant thread on this topic, so I decided to ask a new question. I'm at my first steps in the Dictionary
class in C#, and I have several basic questions. First off, what the difference between this:
Dictionary<int, string> dict = new Dictionary<int, string>();
dict[1] = "hello";
and this (if any):
Dictionary<int, string> dict = new Dictionary<int, string>();
dict.Add(1, "hello");
My second question is: is there any way to add multiple entries (if I can call a pair of key and value that way) on the same line. Something like int a, b, c;
, instead of int a; int b; int c;
. Or is it only possible to add them on multiple lines:
dict.Add(1, "hello");
dict.Add(2, "hey");
My last question, concerning the output of a dictionary, is if there is a "better" way to print a dictionary, instead of doing it that way:
foreach (KeyValuePair<int, string> pair in dict)
{
Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
}
I mean "better" in terms of faster to type and faster to execute. (I know that it also works with var
, instead of KeyValuePair<>
.