可能是个愚蠢的问题,但我想像这样初始化我的对象数组:
FooBar[] fooBars = new FooBars[]
{
{"Foo", "Bar"},
{"Foo", "Bar"}
};
FooBar 在哪里:
public class FooBar
{
public string foo;
public string bar;
}
我试过从 CollectionBase 继承并添加 Add(string) 方法或 string[] 运算符,但这些都不起作用:/
最佳答案
这是您要找的吗? 我不明白你在问什么。
你可以在这个 fiddle 中尝试一下如果你愿意的话。
public class FooBar
{
public FooBar(string foo, string bar)
{
this.foo = foo;
this.bar = bar;
}
public string foo;
public string bar;
}
public static void Main(String[] args)
{
FooBar[] fooBars = new FooBar[] {
new FooBar("Foo", "Bar"),
new FooBar("Foo", "Bar")
};
}
关于c# - 类数组初始化类C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46828721/