NightOwl888/J2N

ReadOnlyDictionary IDictionary.this[object] has inconsistent behavior with other collections that implement IDictionary

NightOwl888 opened this issue · 0 comments

All dictionaries in System.Collections.Generic return null from IDictionary.this[object] when the key doesn't exist, however ReadOnlyDictionary throws a KeyNotFoundException. This is due to the fact that the IDictionary<TKey, TValue> instance that ReadOnlyDictionary wraps doesn't necessarily need to implement IDictionary, so the call is cascaded to IDictionary<TKey, TValue>.this[TKey], which is expected to throw KeyNotFoundException.

We need to break from the behavior in .NET and return null when the key does not exist to make this behavior consistent across all IDictionary implementations.