How to initialize array element in struct?

I am having trouble initializing an array of structs, in which each struct also contains an array of data. I am trying this, but it throws the following compiler error:

A field or property ‘test’ cannot be initialized with a collection object initializer because type ‘test’ does not implement ‘System.Collections.IEnumerable’ interface

struct test
{
    int Value1;
    int Value2;
    int [] Modifiers;
}

test [] Test = new test [ 1 ]
{
    new test { 1, 1, new int [5] { 1, 2, 3, 4, 5 } }
};

Could someone please point me in the right direction how to initialize this?

On line 10 you want to be using parentheses instead of braces for the outer part:

new test( 1, 1, new int[]{1, 2, 3, 4, 5})

Then you simply make a constructor (in test) that accepts those particular arguments and sets them to the member variables of the new struct instance.

1 Like

Yes, that worked. Thanks a bunch, Kurt. :slight_smile: