Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
Generic class operators derived from interface seems broken #1659
Let's say I have a CaseInsensitiveConcurrentDictionary. The intention of it is to be interchangeable with regular Dictionary implementations -- where, we always use the interface IDictionary<T, T> to interact with either. Both Dictionary and ConcurrentDictionary implement IDictionary interface members.
The class looks like this:
Let's say then that we are trying to implicitly convert the ConcurrentDictionary to and from a dictionary. To test, I wrote these methods:
The first (FromDictionaryToCaseInsensitiveDictionaryTest) and third methods (FromCaseInsensitiveDictionaryToDictionaryTest) work fine and correctly use the operators.
The second one fails with this exception: System.InvalidCastException: 'Unable to cast object of type 'System.Collections.Generic.Dictionary
With this evidence, it seems to me that something is missing when the runtime is looking for the appropriate operator. FromIDictionaryToCaseInsensitiveDictionaryTest should work either from the Dictionary operator or automatically work because it implements the IDictionary interface. (I cannot create an operator for IDictionary conversion because that is illegal).
Please let me know if this is expected behavior and if there is some workaround for it. This issue seems to be present in all versions of C#.