How to start a variable correctly?

Asked

Viewed 84 times

6

There is a difference between these two ways to start a variable?

List<classeterapeutica> itens = new List<classeterapeutica>();
modelOff.classeterapeuticas.ToList();

or

List<classeterapeutica> itens = modelOff.classeterapeuticas.ToList();

1 answer

5


It is not a matter of being correct, but initializing a variable with a value to exchange that value in the next line makes no sense, so the second code is preferable. Only assign a value to a variable if this value will be used somewhere. If it is discarded unused it is something bad.

  • then what would be the logic of using new class();? because I’ve seen many codes like this.

  • 3

    In a case similar to this one you don’t have. A lot of code out there is misspelled. You always have to see the context. There is no magic formula for programming correctly.

Browser other questions tagged

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