0
现在,我使用此命令初始化对象列表,并且它工作正常。如何使用构造函数初始化包含数组的对象列表
public class RelatedBlog
{
public string trekid { get; set; }
public string imagepath { get; set; }
public RelatedBlog(string trekid, string imagepath)
{
this.trekid = trekid;
this.imagepath = imagepath;
}
}
trek.relatedblog = new List<RelatedBlog>
{
new RelatedBlog("trekid", "../Images/image.jpg"),
};
然而,最近我已经决定,而不是单一的字符串作为第一个属性,我想有几个字符串数组 - 与尺寸高达4(但它也可以是固定的,我可以进入在初始化期间为空)。这是我使用的代码,但它不工作,预计一些“(”当我调用构造函数。
public class RelatedBlog
{
public string[] trekid { get; set; }
public string imagepath { get; set; }
public RelatedBlog(string[] trekid, string imagepath)
{
this.trekid = trekid;
this.imagepath = imagepath;
}
}
trek.relatedblog = new List<RelatedBlog>
{
new RelatedBlog({"string1", "string2"}, "../Images/image.jpg"),
};
有人可以告诉我,我犯了一个错误,以及如何正确初始化该列表。非常感谢
什么'新名单()'? –