What does Return myVar! = Null really mean?

Resharper is a great tool, but sometimes I am confused by what the proposed code actually means. I have this code:

private bool DoesUserExists()
{
    var user = De.Users.FirstOrDefault(u => u.Username == CurrentUser.Username);
    return user != null;
}

I originally had:

if(user == null)
    return false;
else
    return true;

But Resharper suggested the upper code. However, it seems to me that I'm talking about the return of the user if he is not null. But the method only accepts a bool return, not a class.

So what does the user return! = Null, really returned when it is zero, and when not?

+4
source share
2 answers

So what really returns return user != nullwhen it is null and when it is not

. user null, false, user , true.

, :

bool isNotNull = user != null;
return isNotNull;

:

bool isNull = user == null;
return !isNull;

isNotNull , user .

, if-else.

+7

, , @Yuval , , , .

:

(return user) (!= null)

" " , . return . .

(return) (everything else)

,

(return) (user != null)

return .

, 3 + 5 * 2 3 + 10 8 * 2, * ( ), +. return .

, .

+3

Source: https://habr.com/ru/post/1616536/


All Articles