Plazza Sele Plazza Sele - 2 months ago 8
C# Question

create an object and add a List<string>

I have a class Category that has:

public int id { get; set; }
public string catName { get; set; }
public List<string> subCat { get; set; }

I want to create a list like this:

List<Category> list = new List<Category>();
Category cat = new Category(){ 1 ,"Text", new List<string>(){"one", "two", "three"}};

I get red error mark with this error message:

Cannot initialize type 'Category' with a collection initializer because it does not implement 'System.Collection.IEnumerable'

Any help would be much appriciated.


By the way you're initializing it, it thinks you're trying to implement a list. Do the following instead.

var category = new Category
    id = 1,
    catName = "Text",
    subCat = new List<string>(){"one", "two", "three"}}