-1
struct a
{
int x;
int y;
byte[] z;
}

var b = new a[] {{0, 0, {0, 0, 0}}, {1,1, {1,1,1}}};

I want to initialize an array of a struct each of which contains an array of bytes. I also tried:

var b = new a[] {{0, 0, new byte[] {0, 0, 0}}, {1,1, new byte[] {1,1,1}}};
Cœur
  • 37,241
  • 25
  • 195
  • 267
Bing Bang
  • 524
  • 7
  • 16

2 Answers2

2

A constructor will make it more arranged and readable:

struct a
{
    int x;
    int y;
    byte[] z;

    public a(int xv, int yv, byte[] zv)
    {
        x = xv;
        y = yv;
        z = zv;
    }
}

public void Initialize()
{
    var b = new a[] {new a(0,0,new byte[] { 0,0,0}),
    new a(1,1,new byte[] { 1,1,2})};
}

Another way According to your Comment
1. if you declare the access modifier of the struct fields as public you will be able to initialize them using object initializer and not with constructor (constructor is a method).
2. you can use static class and immediately call that object
3. make b global and public (var is only local keyword) in order to call it from outside (i would use a more descriptive name then b).

full example:

public class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("y value of index 1 is: {0}", General.b[1].y);
        Console.ReadLine();
    }
}

public static class General
{
    public static a[] b = new a[] { new a() { x = 0, y = 0, z = new byte[] { 0, 0, 0 }},
                                    new a() { x = 1, y = 1, z = new byte[] { 1, 1, 1 }}
        };
    public struct a
    {
        public int x;
        public int y;
        public byte[] z;
    }
}
Jonathan Applebaum
  • 5,738
  • 4
  • 33
  • 52
  • I'm porting code I wrote in C. In C, this struct array is created by the compiler without having to run any code at run time. I know this is probably the best answer, but having to create it at run time just feels a bit awkward. – Bing Bang Mar 03 '18 at 07:26
  • @BingBang now your question is a little bit more clear, please see my edits. – Jonathan Applebaum Mar 03 '18 at 09:04
  • Thanks @jonathana.I'll try it out Monday. – Bing Bang Mar 04 '18 at 05:53
  • The actual code contains 64 members in the struct array and each struct member contains a byte array with 256 members. That's why I was hesitant about creating it at run time. – Bing Bang Mar 04 '18 at 06:06
  • I get errors that say the elements are inaccessible due to protection level – Bing Bang Mar 05 '18 at 19:50
  • @BingBang C# is strongly typed more than C, VB, etc...make sure everything that uses that class is public (see few fixes in my code) – Jonathan Applebaum Mar 05 '18 at 20:09
0

Use a regular constructor with some values, and write the array contents later:

public struct A
{
    const int Size = 256;
    // mutable structs are evil. 
    public int x, y;
    // At least make the arrays (not the contents) readonly
    readonly public byte[] a;
    readonly public byte[] b;

    public A(int x, int y)
    {
        this.x = x;
        this.y = y;
        this.a = new byte[Size];
        this.b = new byte[Size];
    }
}

class Program
{
    static void Main(string[] args)
    {
        var x = new A(48, 64);
        var y = new A(32, 24);

        x.a[4] = 1;
        y.b[127] = 31;
    }
}
John Alexiou
  • 28,472
  • 11
  • 77
  • 133