Create 'struct' with string fields of a length or array char with a length

1

I want to create a type 'struct' composed of some fields that must be an array of char with a certain length or a string with a certain length. Currently I have declared it this way:

public struct Header
    {
        public char[] entityCode;
        public char[] date;
        public char[] numberProcess; 
        public char[] numberEnd;  
        public char[] filler;           
    }

Where each must have the following lengths: entityCode = length 6, date = length 6, numberProcess = length 4, numberEnd = length 3, filler = length 50

    
asked by Popularfan 21.08.2018 в 12:20
source

1 answer

3

What you want is technically called Buffer of fixed size , and to be able to create a buffer of this type you should use the instruction fixed , for example in your case the first field would be defined as:

public fixed char entityCode[6];

But there is a problem. Copy of the documentation:

  

In the secure code, a C # struct that contains an array does not contain the array elements. Instead, the struct contains a reference to the elements. You can insert a fixed-size array into a struct when it is used in an unsecured block of code.

With what the definition of your structure would look like this:

public unsafe struct Header
{
    public fixed char entityCode[6];
    public fixed char date[6];
    public fixed char numberProcess[4];
    public fixed char numberEnd[3];
    public fixed char filler[50];
}

You must bear in mind that being a code unsafe , you are responsible for managing possible errors, such as exceeding the limit of the array.

    
answered by 21.08.2018 / 12:39
source