我在编译过程中遇到了这个错误:
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/