c# - 类数组初始化类C#

标签 c# arrays initialization

可能是个愚蠢的问题,但我想像这样初始化我的对象数组:

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/

相关文章:

c# - 组合应该专门用于继承还是在某些情况下不应该使用?

javascript - 为什么 Google map RichMarker 库中的 zIndex 不起作用?

java 设置字母在一定范围内的循环使用

java - 给定 "sub-grid"和 "cell-in-the-sub-grid"索引,如何获取数独二维数组索引?

属性函数的javascript初始值

c# - 使用 Microsoft SQL Server 从动态表中搜索数据

c# - 保护 asp.net webform cookie 不起作用

c# - 如何从数据库中选择最频繁的项目?

c++ - 使用 new 表达式创建内置类型

ruby - 当我可以继承而不使用它时,为什么我们在 ruby​​ 中使用 super?