Hello everyone today I was working to make API.I got confused when I saw different behavior of C# compiler. let me explain:
I get exception:
If(userLoginModel.UserId>0)
{
//do something
}
else{
//do something
}
Exception: {"Object reference not set to an instance of an object."}
Yes it is null; But if write :
if (userLoginReturnModel != null && userLoginReturnModel.UserId >0)
{
//do something
}
else
{
//do something
}
This code did not throw any Exception.
Note: My question is why compiler is not throwing any exception on 2nd code because there is && condition in if so when compiler will check that userLoginReturnModel.userId > 0 then this will also need to throw exception because object is null. And as we know that both condition will be check in if. Any answer with good explanation.