Writing code with implicit cast generates CS0266.
Dictionary<Int32, string[]> d1 = new Dictionary<int, string[]>();
IDictionary<int, IEnumerable<string>> ide1 = d1; // CS0266
The hint that an explicit cast exists suggests an explicit cast would fix the issue.
IDictionary<int, IEnumerable<string>> ide1 = (IDictionary<int, IEnumerable<string>>)d1; // No error, but throws an exception
Is there a way to make this cast ? Would it work for string[][]
and IEnumerable<IEnumerable<string>>
?
namespace Quickie
{
class QuickieArray
{
private static void TestCastDictionaryWithArrayValue()
{
Console.WriteLine();
Console.WriteLine("===TestCastDictionaryWithArrayValue===");
Dictionary<Int32, string[]> d1 = new Dictionary<int, string[]>();
d1[1] = new string[]{"foo"};
IDictionary<int, string[]> id1 = d1;
Console.WriteLine("id1 is null: {0}", id1 == null);
IDictionary<int, IEnumerable<string>> ide1 = id1 as IDictionary<int, IEnumerable<string>>;
IDictionary<int, IEnumerator<string>> iden1 = id1 as IDictionary<int, IEnumerator<string>>;
Console.WriteLine("ide1 is null: {0}", ide1 == null);
Console.WriteLine("iden1 is null: {0}", iden1 == null);
Console.WriteLine();
}
internal static void Test()
{
Console.WriteLine();
Console.WriteLine("=QuickieArray=");
TestCastDictionaryWithArrayValue();
}
}
}
Output:
===TestCastDictionaryWithArrayValue===
id1 is null: False
ide1 is null: True
iden1 is null: True