3

It always feels wrong to me to write

if (MyObject)
    // do something

Or reversly

if (!MyObject)
    // do something

However you could argue that it is less verbose than

if (MyObject != null)
    // do something

if (MyObject == null)
    // do something

Are there any non-subjective reasons to use one over the other?

6 Answers 6

36

In C#, you can't do

if (MyObject) 

to check for nulls. It's a compile time error (if the class doesn't have an implicit boolean conversion operator).

if (!MyObject)

is also invalid if the class doesn't overload operator ! to return a boolean value (or a value that can be implicitly casted to a boolean).

So you have to stick with obj == null and obj != null.

To summarize, the non-subjective reason is being able to compile your code!

UPDATE (story):

Once upon a time, in ancient C, there was no bool type. Zero was considered false and every non-zero value was considered true. You could write

while(1) { }

to create an infinite loop.
You could also do things like

int n  = 10;
while (n--) { }

to have a loop that executes n times. The problem with this strategy was:

int x = 10;
if (x = 0) { // bug: meant to be x == 0
}

You missed a single character and you created a bug (most modern C compilers will issue a warning on this statement, but it's valid C, nevertheless).

This is why you see code like

if (5 == variable) 

if (NULL == pObj)

in many places as it's not prone to the above mistake.

C# designers decided to require a boolean expression as the condition for if, while, etc., and not allow casting of types (unless they explicitly declare an overloaded operator, which is discouraged) to boolean to reduce the chance of errors. So things like:

object x = null;
if (x) { }

int y = 10;
if (y) { }
while (y--) { }

that are valid in C, do not compile in C# at all.
This is not a matter of style or agreement by any means.

Sign up to request clarification or add additional context in comments.

Comments

5

Are there any non-subjective reasons to use one over the other?

The fact that (1) and (2) don't compile is a non-subjective reason enough to not use them.

So we are left with (3) and (4):

if (MyObject != null)
    // do something

if (MyObject == null)
    // do something

Here it depends on whether you want to do something if MyObject is null (4), or if it is not null (3). Clearly this is not a style choice. It would be ridiculous to adopt always the same "style", because then if you wanted the other condition you'd have to do:

    if (MyObject == null) // style policy
    {
        // nothing here
    }
    else
    {
        // do something
    }

Which isn't exactly what I'd call readable code.

Comments

2

In C#, an if statement requires a boolean expression.

So, in your example above using if(myObject) to test for null is actually wrong. You'd need to do the more verbose if(myObject==null).

Comments

2

In my mind, a bit of verbosity in this case is a good thing. That would be the case if you were allowed to do both, which isn't in fact possible, as Mehrdad points out. The "implicit" null check (i.e. if (MyObject)) does not even compile in C#.

if (MyObject == null)
    // do something

This is however quite similar to the situation of checking with an integer is greater than 0. The two equivalent options here (that both compile and run fine in C but not C#, unless I'm mistaken) are:

if (myInt)
    // do something

and

if (myInt > 0)
    // do something

I always go for the second option here, purely for clarity (it can't be mistaken for a bool!), though you will quite often see the former "implicit" check in code.

14 Comments

Don't they do it in reverse like this - if (null == MyObject) //do something ?
The implicit check if (MyObject) is better to check for boolean values only.
@Kirtan: That's a left-over from C syntax, where you could accidentally assign null to MyObject instead of equating it (by only using a single =). There's no risk of ding this in C#.
No, it's not equivalent. You can't say if (myInt) in C#. It's not an issue of style and agreement. It's a semantic rule in C#.
I like to do it this way instead of if(myObject)because it is more immediately clear that you are checking for null and not checking MyObject as a bool for true or false. Kirtan, you can do it both ways. I would put the null first if we were doing a few of these checks against different objects. Such as if (null==MyObject1)else if(null==myObject2) ... I would out my Object first if the 'paragraph of code' was 'about' myobject, and put null first if it was 'about' null, if that makes sense.
|
1

And if you just want to set a default value to an object if it is null, you can use the c# 3.0 ?? operator.

the code:

MyType _myObject
if (SomeObject==null)
  _myObject = defaultObjectValue
else
  _myObject = SomeObject

can be writen as

_myObject = SomeObject ?? defaultObjectValue;

which basically means that if SomeObject exists, use it, and if it does not, use the default value.

1 Comment

?? is also known as "Coalescing Operator". en.wikipedia.org/wiki/%3F%3F_Operator
1

It depends upon personal preferences. Ideally, in C#, to check for null, the following syntax is used -

if (MyObject != null)
    //Do Something.

3 Comments

Says who, and why? The above is a purely personal preference, it's neither standard in C# nor is their a technical reason to corroborate this (in C#!).
This may be ideal in C, but it's mostly frowned upon in C#; use this in your own code, but don't force others to reverse their logic for no reason. To me, that piece of code reads: "if null is not equal to something". But I already know the value of null, it's null! Why would I want to find out if null has changed? I want to know "if something is not equal to null" - so I do: if (MyObject != null).
Writing the boolean expression like this makes it harder to read (it doesn't logically flow like a sentence). As others have said, this style of writing null checks is not helpful at all in C#.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.