c# - 不可能在字典中返回 IEnumerable 的子类型

标签 c# dictionary ienumerable ilist

为什么这是 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/

相关文章:

dictionary - 从 Pandas df 创建字典列表

C# 简单列表显示的问题

c# - 在迭代 IEnumerable 时从集合中删除项目

asp.net-mvc - MVC错误: model item of type 'System.Collections.Generic.IEnumerable` 1 required

c# - 在 C# 中获取印度标准时间 (IST)?

c# - 将 String 转换为 DateTime 时的奇怪行为

c# - 加密二维码的开源实现

c# - 如何使面板内的鼠标拖动从窗口移动?

dictionary - 映射到 Deedle 框架

python - 如何获取二维字典 python 中的所有键