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

梦想与她 提交于 2019-11-29 09:13:01

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

    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!")
        };

Shorthand for the win:

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

this will also work without a need to create a constructure

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

You want:

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

You were missing the brackets toward the end.

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!")
    };
MyClass[] testobjlist = new MyClass[noOfObjects];
for(int i = 0; i < testobjlist.Length; i++) { testobjlist[i] = new MyClass(); }
Hari Lakkakula

You can use below code for the array:

additionalusers[] __adiitonaluser =
{
    new additionalusers()
};
__adiitonaluser[0].Email = Userpersonal.Email;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!