通过数组中的构造函数参数初始化自定义类/对象的 C# 语法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17322250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:10:35  来源:igfitidea点击:

C# syntax to initialize custom class/objects through constructor params in array?

c#arraysconstructorinitializationrazor-2

提问by BerggreenDK

I have a class with minimum 4 variables and I have made a constructor for the class so that I can initialize it with

我有一个至少有 4 个变量的类,并且我为该类创建了一个构造函数,以便我可以用

MyClass testobj = new MyClass(1234,56789,"test text", "something else", "foo");

Works fine.

工作正常。

Then I have an array of these, that I need to parse in a loop, so I would like to get some static data into this array.

然后我有一个数组,我需要在循环中解析它们,所以我想将一些静态数据放入这个数组中。

My approach was:

我的方法是:

MyClass[] testobjlist = new MyClass 
{
     new MyClass(1001,1234,"Text 1", "abcdefghijklm", "ding"),
     new MyClass(1002,2345,"Text xx", "bla bla", "dong"),
     new MyClass(1003,8653,"Text yy", "blah blah even more", "bamm!")
}

but somehow this gives me a weird error about me needing an extra } ???

但不知何故这给了我一个奇怪的错误,关于我需要一个额外的 } ???

I dunno if I should mention this, but I use it for webpages using Razor-engine 2. But I think this is an ordinary C# question?

我不知道我是否应该提及这一点,但我将它用于使用 Razor-engine 2 的网页。但我认为这是一个普通的 C# 问题?

My workaround is currently to initialize the array with a size, then adding the elements one by one through index, but I would rather prefere the above solution as I might have to move the items up and down in order when testing and I have a lot more than 3 in the real data.

我的解决方法目前是用一个大小初始化数组,然后通过索引一个一个地添加元素,但我更喜欢上面的解决方案,因为我可能需要在测试时按顺序上下移动项目,而且我有很多实际数据中超过3个。

Wondering what I am missing in the above code...?

想知道我在上面的代码中缺少什么......?

采纳答案by Eric Beaulieu

Try adding square brackets after new MyClass and a semi-colon at the end

尝试在 new MyClass 之后添加方括号并在末尾添加分号

    MyClass[] testobjlist = new MyClass[] 
        {
         new MyClass(1001,1234,"Text 1", "abcdefghijklm", "ding"),
         new MyClass(1002,2345,"Text xx", "bla bla", "dong"),
         new MyClass(1003,8653,"Text yy", "blah blah even more", "bamm!")
        };

回答by Kirk Woll

You want:

你要:

MyClass[] testobjlist = new MyClass[] { ... }

You were missing the brackets toward the end.

你在最后错过了括号。

回答by Ben Sewards

Shorthand for the win:

胜利的简写:

var myClassList = new[]
{
    new MyClass(1001,1234,"Text 1", "abcdefghijklm", "ding"),
    new MyClass(1002,2345,"Text xx", "bla bla", "dong")
};

回答by Nitin Rajurkar

MyClass[] testobjlist = 
    {
     new MyClass(1001,1234,"Text 1", "abcdefghijklm", "ding"),
     new MyClass(1002,2345,"Text xx", "bla bla", "dong"),
     new MyClass(1003,8653,"Text yy", "blah blah even more", "bamm!")
    };

回答by ahaliav fox

this will also work without a need to create a constructure

这也无需创建结构即可工作

new MyClass [] { new MyClass { Field1 = "aa", Field2 = 1 } } 

回答by Hasala Senevirathne

MyClass[] testobjlist = new MyClass[noOfObjects];
for(int i = 0; i < testobjlist.Length; i++) { testobjlist[i] = new MyClass(); }

回答by Hari Lakkakula

You can use below code for the array:

您可以对数组使用以下代码:

additionalusers[] __adiitonaluser =
{
    new additionalusers()
};
__adiitonaluser[0].Email = Userpersonal.Email;