Which value would you want it to return if it didn't exist in the map already? It could return default(TValue)
- but then it would be somewhat easy to accidentally use a value assuming it was correct. Note that if you want that behaviour, you can use:
get {
string value;
if (!_mydic.TryGetValue(key, out value))
value = default(string);
return value;
}
I wouldn't personally recommend it in general, but if you're happy not to be able to distinguish (via this indexer) between a key with a null value and a missing key, it will do the trick.
Note that your setter can me a lot simpler - just
_mydic[key] = value;
will overwrite if necessary.