I have the following method I would like to know how it is interpreted
if (!existe_producto(ListaProd, item1.codigo))
{
item1.Descripcion = item1.codigo + " - " + item1.Descripcion;
ListaProdx.Add(item1);
}
private bool existe_producto(List<Ex_Producto> full_codigo, long codigo)
{
try
{
bool resp = false;
foreach (var c in full_codigo)
{
if (c.codigox == codigo)
{
resp = true;
break;
}
}
return resp;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
That is, when I evaluate if(bool), that is, if my variable is equal to true, but when I say if(!bool) of a method that returns true or false, as it is read
Basically it's going to negate what it returns
existe_producto()
, if it returns atrue
then convert it tofalse
that will be what theif
! Operator (C# Reference)
The operator "!" is a negation operator , and what it does is convert one of the booleans (
true
orfalse
) to its respective opposite. In plain language, you could loosely see it as "If x is no such thing, then... ".For example, in your case:
We assume that the "product_exists()" method returns true
(true)
if it does exist. Adding the negation operator ("!") is like saying, " If the product doesn't exist, then... ".If we were to granulate the code, an equivalent would be something like:
For more information, you can consult the link that Leandro left: ! Operator (C# Reference)