我有一个类 Category 有:
public int id { get; set; }
public string catName { get; set; }
public List<string> subCat { get; set; }
我想创建一个这样的列表:
List<Category> list = new List<Category>();
Category cat = new Category(){ 1 ,"Text", new List<string>(){"one", "two", "three"}};
list.Add(cat);
我收到此错误消息的红色错误标记:
Cannot initialize type 'Category' with a collection initializer because it does not implement 'System.Collection.IEnumerable'
如有任何帮助,我们将不胜感激。
最佳答案
按照您初始化它的方式,它认为您正在尝试实现一个列表。请改为执行以下操作。
var category = new Category
{
id = 1,
catName = "Text",
subCat = new List<string>(){"one", "two", "three"}
};
关于c# - 创建一个对象并添加一个 List<string>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38873164/