【问题标题】:Display a list items and the index of how many items exist in the list [closed]显示列表项和列表中存在多少项的索引[关闭]
【发布时间】:2022-11-07 08:59:42
【问题描述】:

在这里我有列表和字典

 public static List<Category> categories = new List<Category>();
 public static Dictionary<string, ProductInfo> ProductInformation = new Dictionary<string, ProductInfo>();

问题:我想显示制作的类别及其数量 使用此代码的产品:

static void ShowCategory()
    {
        foreach (var c in Category.categories)
            Console.WriteLine(ProductDictionary[c.Name].Count);

        Console.ReadKey(true);
        ConsoleKey key = ConsoleKey.Escape;
    }

我收到一条错误消息:

CS1503:无法从“方法组”转换为“布尔”

这意味着什么?

【问题讨论】:

    标签: c# list dictionary foreach console


    【解决方案1】:

    我收到一条错误消息:

    CS1503:无法从“方法组”转换为“布尔”

    这意味着什么?

    我的意思正是它所说的。在您的情况下,Count 是一种方法,而不是您期望的属性。将您的代码更改为:

    Console.WriteLine(ProductDictionary[c.Name].Count());
    

    【讨论】:

    • 嗯不确定这一点,productdictionary[c.Name] 会返回ProductInfo...所以Count 真的可以是任何东西...
    • @CanBilgin 我同意,但鉴于我们拥有的信息,这是我能想出的唯一合乎逻辑的解释。
    • 那么 Console.WriteLine(ProductDictionary[c.Name].Name.Count());解决了这个问题,但现在我得到了一个例外:字典中不存在给定键“类别名称”
    • 您可以在此处复制并粘贴错误吗?
    • 抛出异常:System.Private.CoreLib.dll 中的“System.Collections.Generic.KeyNotFoundException” System.Private.CoreLib.dll 中发生“System.Collections.Generic.KeyNotFoundException”类型的未处理异常给定的键“类别名称”字典里没有。注意:类别名称=用户给类别的名称!
    猜你喜欢
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    • 1970-01-01
    • 2019-02-10
    • 2012-11-15
    • 1970-01-01
    • 2015-09-16
    相关资源
    最近更新 更多