I have following classes
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Shop
{
public string Name { get; set; }
}
public class Xyz
{
public string Abc { get; set; }
}
Why is that following is not allowed
IDictionary<Person, IDictionary<Shop, Xyz>> blahItem
= new Dictionary<Person, Dictionary<Shop, Xyz>>();
For the same reason that you can’t do:
Suppose that were allowed… we could have:
Basically you’d violate type safety –
IDictionaryisn’t covariant in either its key or value types.Search for “generic variance” and “covariance” to find more details, including the limited support (e.g. for
IEnumerable<T>which can be used safely in a covariant way) in C# 4.