【发布时间】:2015-05-13 12:10:49
【问题描述】:
我在编译过程中遇到了这个错误:
System.Collections.Generic.List<string>.Count is inaccessible due to its protection level
关于尝试在我创建的代表我项目中的清单的列表上使用方法 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!");
}
}
【问题讨论】:
-
你能引用确切的错误信息吗?
-
错误是什么,是在编译还是运行时出现的?
-
这可能有点离题,但你能解释一下你为什么不使用
public List<string> inventory { get; set; }吗? -
@Voidpaw,你觉得改变会给代码带来什么好处?
-
从提供的代码来看,这在技术上是不可能的。所以,或者消息不准确,或者描述问题的代码不完整。
标签: c# list count compiler-errors