In Python one can do:
d = {1 : 'Hello', 2 : 'World'}
In C# it’s more verbose:
Dictionary<int, string> d = new Dictionary<int, string>();
d.Add(1, 'Hello');
d.Add(2, 'World');
How can I make this less verbose?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use the collection initializer syntax (and implicit typing with
var):This code will actually be compiled down to the code you have above. Note that you (unfortunately) can’t elide the constructor or the generic type arguments.
Not quite Pythonic, but getting there!