C# 使用数组成员创建模型
我想制作一个模型,向数据库中添加内容。一个示例使用该类:C# 使用数组成员创建模型,c#,asp.net,.net,model,C#,Asp.net,.net,Model,我想制作一个模型,向数据库中添加内容。一个示例使用该类: 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; } } 但如果我
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; }
}
但如果我想让一些成员成为数组,我会怎么做?就像我想要一系列的体裁而不是一种。
我知道这是错误的,但我对C#太陌生了,不知道为什么,也不知道如何正确操作:
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; }
}
您只需将属性声明为某种类型的集合,如
IEnumerable
或List
,如下所示:
public class Movie
{
// Other properties omitted for brevity
public IEnumerable<string> Genres { get; set; }
}
公共类电影
{
//为简洁起见,省略了其他属性
公共IEnumerable类型{get;set;}
}
然后,当您实际填充它时,您可以使用一个具体的实现,如
列表
或字符串[]
我建议使用列表而不是数组