c# - C#-在这种情况下无法使用list.Count()

标签 c# list count compiler-errors

我在编译过程中遇到了这个错误:

System.Collections.Generic.List<string>.Count is inaccessible due to its protection level

关于尝试在我创建的代表项目 list 的列表上使用Count()方法。该列表本身是公开的,所以我无法理解为什么会发生这种情况...(这里是新手)
public class Inventory
{
    private List <string> _inventory;
    public List<string> inventory {get; set;}

    public Inventory ()
    {
        _inventory = new List<string>();
        inventory = _inventory;
    }

    public void AddItem(string str)
    {
        if (inventory.Count() < 3)  //    error pops up here...
            inventory.Add(str); 
        else
            Console.WriteLine ("Your inventory is full!");
    }
}

最佳答案

使用List时,应使用Count属性,而不是Count()方法。 Count()是扩展方法,您需要导入System.Linq才能使用它。

关于c# - C#-在这种情况下无法使用list.Count(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30214400/

相关文章:

c# - 语法高亮部分适用于 C# 的 VS 代码

list - 如何生成只有给定长度的两个 1 和其他 0 的列表?

java - 如何在 HashMap 中存储和访问 2 种不同类型的 List

r - 在 R 中的循环中构建列表 - 获取正确的项目名称

powershell - 计算 Powershell 中的字符数

c# - VR乐谱文字展示

c# - WP7邮件客户端是否使用ActiveSync

c# - T4 模板中无法识别 EnvDTE 类型

php - 来自多个表的 MySQL 计数列。按主要 ID 对结果进行分组

MySQL join, count and group by 多个数据库