I have a Dictionary>. Is there any function which gets the list at a particular key, and or creates a list if it does not exist. For example, this does not work:
Dictionary<string, List<string>> d = new Dictionary<string, List<string>>();
d["Bob"].Add("Fred");
Right now I'd have to do this:
Dictionary<string, List<string>> d = new Dictionary<string, List<string>>();
List<string> l = d["Bob"];
if(l == null) { l = d["Bob"] = new List<string>(); }
l.Add("Fred");
I've considered adding an extension method Get(T key), which would allow me to do this:
Dictionary<string, List<string>> d = new Dictionary<string, List<string>>();
d.Get("Bob").Add("Fred");
But I wanted to check to see if something like this doesn't already exist.
Looks like this is a dupe, that had a good answer: