How to initialize char array in struct

Welcome to Programming Tutorial official website. Today - we are going to cover how to solve / find the solution of this error How to initialize char array in struct on this date .

how to initialize a char array in structure in C# i am doing this

struct cell
{
    public char[] domain =new char[16];
    public int[] Peers;
    public int NumberOfPeers;
    public char assignValue;
}

but its giving error that we cannot initialize an array in the body of structure! can any one tell the correct way

Answer

You can use a constructor for this. Look at MSDN.

struct cell
{
    public cell(int size)
    {
         domain = new char[size];
         this.Peers = Peers;
         this.NumberOfPeers = NumberOfPeers;
         this.assignValue = assignValue;
    }    
}