3
Which of the two ways to verify if a record of such TYPE has ever been registered is more performative? Are there differences, errors? What are the implications? The block using
frees only connection resources or all objects created in its scope? The memory used by using
has priority in GC? There is another better way to do such a check?
Block 1
int valorDoTipo = 5;
Tipo tipo;
using (Contexto contexto = new Contexto())
{
tipo = contexto.Tipo.FirstOrDefault(t => t.ValorDoTipo == valorDoTipo);
}
if (tipo != null)
return true;
else
return false;
Block 2
using (Contexto contexto = new Contexto())
{
int valorDoTipo = 5;
Tipo tipo;
tipo = contexto.Tipo.FirstOrDefault(t => t.ValorDoTipo == valorDoTipo);
if (tipo != null)
return true;
else
return false;
}
The end (the 4 lines of the
if
) of your code can be written asreturn tipo != null;
– Maniero
yeah, the direct boolean expression, so simple... never thought of it, would be lack of experience?
– Caique C.
Probably why you’re all here, learning from the experience of others. Even the less experienced help the more experienced, after all, each with their own experience.
– Maniero
@Caiquec. Entity framework is very cool and has so much that today I live learning, the most important is this and let’s say it is not lack of experience, it is a learning forever ... congratulations!
– user6026