What is the best syntax for checking objects with a null value in C #

I always feel bad writing

if (MyObject) // do something 

Or vice versa

 if (!MyObject) // do something 

However, you can claim that it is less detailed than

 if (MyObject != null) // do something if (MyObject == null) // do something 

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

+4
source share
6 answers

In C # you cannot do

 if (MyObject) 

to check for null s. This is a compile-time error (if the class does not have an implicit logical conversion operator).

 if (!MyObject) 

also invalid if the class does not overload operator ! to return a boolean value (or a value that can be implicitly entered into a boolean value).

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

To summarize, a non-subjective reason is the ability to compile your code!

UPDATE (story):

There used to be no bool type in ancient C. Zero was considered false , and each nonzero value was considered true . You can write

 while(1) { } 

to create an endless loop.
You can also do things like

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

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

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

You missed one character and you created an error (most modern C compilers will give a warning about this statement, but it is still C).

That's why you see code like

 if (5 == variable) if (NULL == pObj) 

in many places since it is not subject to the above error.

C # developers decided to require a logical expression as a condition for if , while , etc., and not to allow casting of types (unless they explicitly declare an overloaded operator that is discouraged), to eliminate the possibility of errors. So things like:

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

which are valid in C do not compile in C # at all .
It is not a matter of style or agreement in any way.

+36
source

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

The fact that (1) and (2) does not compile is not a subjective reason sufficient 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). Obviously, this is not a style choice. It would be ridiculous to always accept the same β€œstyle”, because then, if you would like another condition that you would have to make:

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

This is not exactly what I would call readable code.

+5
source

In my opinion, a little verbosity in this case is a good thing. That would be so if you were allowed to do both, which is actually impossible, as Mehrdad points out. An "implicit" zero check (i.e. if (MyObject) ) doesn't even compile in C #.

 if (MyObject == null) // do something 

This, however, is very similar to a verification situation with an integer greater than 0. Two equivalent options here (which both compile and execute normally in C, but not C #, if I'm not mistaken):

 if (myInt) // do something 

and

 if (myInt > 0) // do something 

I always use the second option here, purely for clarity (it cannot be mistaken for bool!), Although you will often see the old "implicit" code check.

+2
source

In C #, the if statement requires a Boolean expression.

So, in your example above, using if(myObject) to check for null is actually wrong. You will need to do a more detailed if(myObject==null) .

+2
source

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

 if (MyObject != null) //Do Something. 
+1
source

And if you just want to set the default value for an object, if it is null, can you use C # 3.0 ?? Operator.

code:

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

can be written as

 _myObject = SomeObject ?? defaultObjectValue; 

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

+1
source

All Articles