Creating a model with array members

时间:2016-04-04 16:28:45

标签: c# asp.net .net model

I want to make a model to add things to a database. An example here uses this class:

public class Movie
{
    public int ID { get; set; }
    public string Title { get; set; }
    public DateTime ReleaseDate { get; set; }
    public string Genre { get; set; }
    public decimal Price { get; set; }
}

But what would I do if I wanted some members to be arrays? Like if I wanted an array of genres instead of one. I know this is wrong but Im too new to C# to see why or to know how to do it correctly:

public class Movie
{
    public int ID { get; set; }
    public string Title { get; set; }
    public DateTime ReleaseDate { get; set; }
    public string[] Genres { get; set; }
    public decimal Price { get; set; }
}

1 个答案:

答案 0 :(得分:0)

You could simply declare the property as some type of collection such an IEnumerable<string> or List<string> as seen below :

public class Movie
{
    // Other properties omitted for brevity
    public IEnumerable<string> Genres { get; set; }
}

And then when you actually populate it, you could use a concrete implementation like a List<string> or string[].