@Mason Wheeler said:
Wait, so TryGetValue doesn't automatically set the out param to Default if it's not found?
It will, but I consider that an implementation detail.
@pbean said:
Pretty sure I'm serious. Yes, I would indeed suggest returning null. I assume you mean "what if you want to store null values?"? Well, you shouldn't. Why would you? In Java, Map.get() returns null if there is no entry for the given key, and I haven't had any problems with that all the while.
And what does Java return if TValue is int, 0? I'd rather know the difference between a value set to 0 and a value not set at all. Besides -1 is a much better default value anyway.
if (dict.TryGetValue(key, out value)) /* do something with value */
else /* do something without value */
is superior to
value = dict[key]
if (value != null) /* do something with value */
else /* do something without value */