Method I Wish Was There: Dictionary.Insert

Yes, this is a horrible name. If you have a better suggestion, I’m very welcome to it.

Here is the point: I want a method on Dictionary that does what Add does, but returns the dictionary instead of void. That way I can do this.

var d = (new Dictionary<string,string>()).Insert(“hi”,”mom”).Insert(“hello”,”dad”).Insert(“peace”,”sista”).Insert(“word”,”brotha”)

And yes, I know that C# 3’s initializer syntax lets me do this instead:

new Dictionary { { “hi”,”mom” }, {“hello”,”dad”}, {“peace”,”sista”}…}

BUT… that only works with a constructor. Tomorrow’s edition will show why I want a method instead of an initializer.

* I didn’t know that initializer is not a dictionary word. I love when fields (programming) extend their own words.