Use of struct in C#

Asked

Viewed 258 times

1

I have to record customer information. And I used struct to do this. Is it worth doing this? It is or is not a good practice?

criei as seguintes variaveis public string dentro do struct

fiz um array do struct que criei com 110 posicões

adiciones as seguintes informações para cada variavel da posição 0

2 answers

4


Not generally, this seems to be a clear case for a class, not only because of its size, but also because there is no identity in it. I already gave a answer that says when to choose one thing or another.

If you really need performance and memory allocation control you can use yes, but you have to know what you’re doing, it has several not so obvious implications, even for experienced programmers.

In C# 7.2 it has become easier to do this as long as you use a readonly ref struct, but it still doesn’t seem suitable for your case, since it should allow direct writing on the object instead of creating a new one. If by chance the semantics is immutable even then it can be useful, but not absolutely necessary. The simplest for a beginner will still be using the class.

0

The use of class would be more appropriate/efficient. And how Boa Pratica use properties instead of fields. Note that in c# properties public begin with capital letters.

public class Cliente 
{
   public string Email { set; get; }
   public string Telefone { set; get; }
   ...
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.