How to initialize a char array in a structure

how to initialize a char array in a structure in c # i do this

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

but it gives an error that we cannot initialize the array in the body of the structure! can anyone tell the right way

+5
source share
7 answers

You can use constructor . Take a look at MSDN.

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

You can use 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 the ability to change structure values โ€‹โ€‹is an even more complex problem - volatile structures are evil .

+9
source

You can (and perhaps should) use a class instead of a structure (although it does matter, but you don't say how you intend to use your type).

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

If you adhere to a structure, be sure to create its immutable structure. One way to do this is to make all the fields readonly and then assign them in your instance constructor.

+2
source

One of the main limitations of value types in .net is that they cannot contain arrays, even fixed ones, unless they leave the managed code area (C # provides a means of including fixed size arrays in a value type using unsafe and fixed , but the code that uses this ability will not be usable in certain security contexts). They may contain references to arrays, but if the structure containing the reference to the array is copied, the copy will receive a link to the same array as the original.

For now, I would have suggested that in most cases it is better that the type of structure is completely open about what it contains (expose all its state through public fields) than to pretend to be something other than a collection of variables connected together An adhesive tape whose structure whose state is to encapsulate the state of a mutable class object (for example, System.Array ) must use a different template.

If a structure should behave like an array of values โ€‹โ€‹from 64 floats, and everyone wants to have floats stored in System.Array, and not in 64 separate fields, the structure should probably contain a private Arr field of type float[] . To read element n , check if Arr is null. If so, return zero. Otherwise, return Arr[n] . To write the element n , set the temporary variable Arr2 to a new float[64] and copy Arr if it is not zero. Then set Arr2[n] to the desired value and replace Arr with Arr2 . Note that immediately float[] is stored in Arr , it will never be written. If an attempt is made to write element 5 of the structure, the structure will receive a new array with the corresponding value; element 5 of the old array will remain untouched. Therefore, the structure will behave like an array of value-type (except that writing to it will be much slower than writing to a regular array).

If a type needs a large array, it is better to use float[][] or float[][][] ; for example, one could contain 4096 elements using 16 arrays of 16 arrays of 16 arrays of 16 elements. In this case, writing to the element will require creating / copying a float[16] , a float[16][] and a float[16][][] ; three small arrays of size 16 can be better than two arrays of size 64 and will almost certainly be better than one array of size 4096.

The semantics of a type value may be useful, but since marked structures cannot provide horribly efficient 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 accessory using the switch statement to read or write the struct field. A little icky, but for structures less than a dozen elements or so, it will almost certainly be more efficient than using arrays as described above.

+2
source

try using something like this:

 [StructLayout(LayoutKind.Sequential)] public class TCardDB { public TCardDB(string strCardNo) { CardNo = strCardNo; FName = LName = string.Empty; OpenMode = FingerCount = 0; Finger1 = new string[3]; Finger2 = new string[3]; } .......... .......... } 
+1
source

See the first comment on this question
C # array inside structure
Fixed size buffers - msdn.microsoft.com/en-us/library/zycewsya.aspx
It did the trick for me

+1
source

Especially for char [];

Itโ€™s a little strange that we have so many difficulties with an array of characters when we can easily add a string ... Anyway, this is just char [] ... With all the indexing functions you need.

Anyway, this worked for me.

0
source

All Articles