为什么这是 C# 中的问题?
public Dictionary<string, IEnumerable<object>> GetProducts() {
return new Dictionary<string, IList<object>>();
}
最佳答案
这是不可能的,因为它会创建一个字典,有 IList<object>
作为一个值。
所以如果你会调用:
IEnumerable<object> values = someOtherObjectCollectionPerhapseSomethingFromLinq;
Dictionary<string, IEnumerable<object>> products = GetProducts();
products.Add("MyKey", values);
您返回的字典将接受 IEnumerable<object>
但是下面的字典不会接受它,因为它的值必须是 IList<object>
类型,这是,它不是。
关于c# - 不可能在字典中返回 IEnumerable 的子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16358923/