Which means an "!" exclamation before a string inside an if in C#

Asked

Viewed 259 times

6

Hello, I have a question regarding some "snippets" of a program I am using to "study", the question is, what is the meaning of "!" inside an if before a string, I saw some topics on the site explaining, but I don’t understand how it works in the program I have...

if (Connect._state == HostStatus.ONLINEALERT && !string.IsNullOrEmpty(Connect._message))

The code above is one of the "excerpts" that I do not understand, I thank from now on to those who help me...

3 answers

6


In general the operator "!" is the logical "not", ie a negation, I believe it is the same in C# as quoted here. That stretch is probably denying string.IsNullOrEmpty(Connect._message).

4

then, string.Isnullorempty indicates if a string is null or empty, that is, if this variable Connect._message which is being passed as parameter null or empty, will return a Boolean true, otherwise will return false, but to satisfy the condition of his If, the value returned has to be false, on account of the exclamation mark.

example:

var exemplo = null;    
if(string.IsNullOrEmpty(exemplo)){
//a condição deve ser true para prosseguir
//se a variavel exemplo for null ou empty, irá satisfazer a condição do If
//pois a condição pede que o valor seja null ou empty
}

var exemplo2 = 'teste';    
if(!string.IsNullOrEmpty(exemplo)){
//a condição deve ser false(por conta do !) para prosseguir
//ele verifica a variável, como nesse caso ela tem valor, o IsNullOrEmpty irá
//retornar false, pois não é null nem empty a variavel
}

1

In several programming languages you will find this character as a negation symbol. It denies a certain value.

In his example: !string.IsNullOrEmpty(Connect._message)), the method IsNullOrEmpty is checking your text (Connect._message) is null or empty. If this check returns true (true), the simile ! will deny this information making the final result false (false).

The same occurs in the reverse process if the result of IsNullOrEmpty is false (false), the symbol ! will deny this information making the final result true (true).

Browser other questions tagged

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