How to make a struct in C #?

2

How to make a structure in C # where you create a type?

For example, in C ++ we use:

struct teste
{
  int a;
  char c[30];
};
    
asked by anonymous 03.01.2016 / 02:41

2 answers

5

First of all it would be nice to read in this question to understand how struct works in C #, it is not well the same thing as in C or C ++. Although it does not differ much.

So in this case you can do something similar:

unsafe struct teste {
    public int a;
    public fixed char c[30];
};

You can only reproduce the same effect with unsafe . This structure will be 34 bytes in size (in 32-bit code), as expected. But it really should not do the same. First that code unsafe should only be used when there is real need and no substitute, and this form is not idiomatic. Second, something so big should be a class. This can best be understood in the link above. The most idiomatic form would look like this:

struct Teste {
    public int a;
    public byte[] c;
}

See working on dotNetFiddle .

Note that members are explicitly public. This is a C # requirement. The array syntax is a bit different and does not allow you to set a size limit that can be used. To get more expensive from C # I've changed the name to use uppercase which is normal in language and I have removed the ; end which is unnecessary. You could still have created a constructor that would make it easier to start the structure and even create a size barrier:

struct Teste {
    public int a;
    public char[] c;
    public Teste(int size = 30) {
        if (size > 30) {
            throw new ArgumentException($"parâmetro deve ter tamanho máximo de 30");
        }
        a = 0;
        c = new char[size];
    }
    public Teste(int a, char[] c) {
        if (c.Length > 30) {
            throw new ArgumentException($"parâmetro para {nameof(c)} deve ter tamanho máximo de 30");
        }
        this.a = a;
        this.c = c;
    }
    public Teste(int a, string c) : this(a, c.ToCharArray()) {}
}

See working on dotNetFiddle .

Note that this shape is a bit different. The struct has 8 bytes in size (in 32-bit code), plus an array from up to 30 characters (60 bytes plus the object's allocation cost and other properties of the array , as its size, for example, after all is a separate object and the default% of C # has 2 bytes to work well with UTF, and in fact each character can occupy more than one position, albeit rare) that will always stay in the heap .

Remember that char is not used by reference, so it will always always be allocated in stack or within another object in heap . There is a different semantics there. If you need semantics by reference, use a class (you can also use a struct parameter in some cases). In C # this has a clear distinction based on the link of the question at the beginning of the response.

Obviously there is an error in the last attempt, as expected.

This form is no longer compatible with a% C / C ++%, although it does have some compatibility with some effort on the side of these languages.

As the last optimization I would change the array from ref to struct , a better type for this in C #, would not care much about size and would transform into a structure usable , which is correct for a char in C #, using properties instead of public fields and with better names:

struct Teste {
    public int Numero {get;}
    public string Texto {get;}
    public Teste(int numero, string texto) {
        Numero = numero;
        Texto = texto;
    }
}

See working on dotNetFiddle .

Now it looks like C # in the best way.

    
03.01.2016 / 06:31
2

Same thing as your example, only without ; at the end. Here's the example:

public struct Livro
{
   public decimal Preco;
   public string Titulo;
   public string Autor;
}

For more information, check out this link .

    
03.01.2016 / 03:09