Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize char array in struct

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

like image 756
ssbb Avatar asked Dec 17 '12 13:12

ssbb


3 Answers

You can use a constructor

struct cell
{
    public cell(int charArraySize) : this()
    {
         domain = new char[arraySize];
    }

    public char[] domain;
    public int[] Peers;
    public int NumberOfPeers;
    public char assignValue;
}

Note: Even with structures, you should not have public fields but encapsulate them in properties.

And being able to change the values of a structure is an even worse problem - mutable structs are evil.

like image 149
Oded Avatar answered Nov 06 '22 15:11

Oded


One major limitation of value types in .net is that they cannot contain arrays, even fixed sized ones, unless one leaves the realm of managed code (C# provides a means of including fixed-sized arrays within a value type using the unsafe and fixed keywords, but code that uses that ability will be unusable in certain security contexts). They can contain references to arrays, but if a structure that holds an array reference is copied, the copy will receive a reference to the same array as the original.

While would suggest that in most cases it's better for a structure type to be completely open about what it contains (expose its entire state via public fields) than for it to pretend to be anything other than a collection of variables bound together with duct tape, a structure whose state is supposed to encapsulate the state of a mutable class object (such as a System.Array) must use a different pattern.

If a struct is supposed to behave as a value-type array of 64 floats, and one wishes to actually have the floats stored within a System.Array rather than in 64 separate fields, the struct should probably contain a private field Arr of type float[]. To read element n, check whether Arr is null. If so, return zero. Otherwise, return the value of Arr[n]. To write element n, set a temp variable Arr2 to a new float[64] and copy Arr if it's not null. Then set Arr2[n] to the desired value, and replace Arr with Arr2. Note that at once a float[] has been stored to Arr, it will again never be written to. If an attempt is made to write to element 5 of the struct, the struct will receive a new array with the proper value there; element 5 of the old array will be untouched. Consequently, the struct will behave as a value-type array should (except that writing to it will be much slower than a write to a normal array would be).

If a type will need a large array, it may be better to use a float[][] or a float[][][]; for example, one might hold 4096 elements using 16 arrays of 16 arrays of 16 arrays of 16 elements. In that scenario, writing to an element would require generating/copying a float[16], a float[16][], and a float[16][][]; the three small arrays of size 16 may be better than two arrays of size 64, and would almost certainly be better than one array of size 4096.

Value-type semantics can certainly be useful, but as noted structures can't provide terribly efficiently if they contain arrays. An alternative design, if the number of elements is small, would be to simply have a field for each element, and have an indexed property accessor use a switch statement to read or write a struct field. A little icky, but for structs smaller than a dozen elements or so it would almost certainly be more efficient than using arrays as described above.

like image 3
supercat Avatar answered Nov 06 '22 15:11

supercat


You can (and possibly should) use a class instead of a struct (though it makes a difference, but you don't tell how you're going to use your type).

Or you can write a constructor. But note that a struct instance constructor must take in at least one parameter.

If you stick to the struct, be sure to make it an immutable struct. One way of doing this is to make all fields readonly, and then assign to them in your instance constructor.

like image 2
Jeppe Stig Nielsen Avatar answered Nov 06 '22 14:11

Jeppe Stig Nielsen