I would think it’s fairly straightforward to cast an IDictionary<TKey, IList<TValue>> object to an IDictionary<TKey, IEnumerable<TValue>>, but
var val = (IDictionary<TKey, IEnumerable<TValue>>)Value;
throws a System.InvalidCastException, and
var val = Value as IDictionary<TKey, IEnumerable<TValue>>;
makes val null. What is the proper way to cast this?
Absolutely not. It wouldn’t be type-safe. Here’s an example of why not:
As you can see, that’s going to cause problems – we’d be trying to get a
LinkedList<int>out when we expect all the values to implementIList<int>. The point of generics is to be type-safe – so which line would you expect to fail? The first, third and fourth lines look pretty clearly valid to me – so the second one is the only one which can fail to compile, and it does…Now in some cases, it can be done safely. For example, you can convert (in C# 4) from
IEnumerable<string>toIEnumerable<object>becauseIEnumerable<T>only usesTin “output” positions.See MSDN for more details.
EDIT: Just to clarify – it’s easy to create a new dictionary with a copy of the existing key/value pairs, e.g. using link:
You just need to be aware that you now have two separate dictionaries.