473,385 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Comparison to null

Hello there,

Why is it that I sometimes see this construct

//case 1
if ( null == myVariable)
{
//...etc
}
//case 2
if ( myVariable == null)
{
//...etc
}

Basically, what is the difference between the two?

Nov 16 '05 #1
14 2333
It makes no difference, they are the same. The syntax is common for people
that have been programing in C/C++ for many years.

In C/C++ it is legal to write (single '=');

if(myVariable = null)
{
}

If you switch them around, it causes a compilation error because you cannot
assign a value to null.

if(null = myVariable)
{
}

Chris

<ju******@hotmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
Hello there,

Why is it that I sometimes see this construct

//case 1
if ( null == myVariable)
{
//...etc
}
//case 2
if ( myVariable == null)
{
//...etc
}

Basically, what is the difference between the two?

Nov 16 '05 #2
To my knowledge, there is no difference.

I was taught to use the if(null == myVariable) method because a mistake
like using a = instead of == will be caught at compile time instead of
runtime.

--
Message posted via http://www.dotnetmonster.com
Nov 16 '05 #3
Scott Klueppel via DotNetMonster.com <fo***@DotNetMonster.com> wrote:
To my knowledge, there is no difference.

I was taught to use the if(null == myVariable) method because a mistake
like using a = instead of == will be caught at compile time instead of
runtime.


Both of them will be caught at compile-time with C# though. There's no
reason to use this unnatural ordering in C#.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
I swear I saw a code doing like that ( case 1 and case 2) in one method
and throwing difference exception for each. That's why I wanted to
know.

Juanph31

Nov 16 '05 #5
I swear I saw a code doing like that ( case 1 and case 2) in one method
and throwing difference exception for each. That's why I wanted to
know.

Juanph31

Nov 16 '05 #6
I don't think that

if(null == MyValue)
{
....
}

is a good habit to get into. I personally have not seen code like that in
any real world projects, but feel it would not be a proper coding standard
even if it makes no difference with comparison operators. And that would go
for any language IMHO.

Bob Calvanese
<ju******@hotmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
Hello there,

Why is it that I sometimes see this construct

//case 1
if ( null == myVariable)
{
//...etc
}
//case 2
if ( myVariable == null)
{
//...etc
}

Basically, what is the difference between the two?

Nov 16 '05 #7
juanph31 <ju******@hotmail.com> wrote:
I swear I saw a code doing like that ( case 1 and case 2) in one method
and throwing difference exception for each. That's why I wanted to
know.


Well, if you could show that code, I'd be interested to see it.
Admittedly there could be a difference if someone had overloaded the ==
operator and done it badly, but that's a bug in their operator
overloading rather than a feature of the language.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
Hi All,

I think that a "key" is operator overloading.
Like a Jon post it before.

MyClass myVar;

if( null==myVar ) {
}
Doesn't have a chance to fire MyClass "==" operator function.

Marcin
juanph31 <ju******@hotmail.com> wrote:
I swear I saw a code doing like that ( case 1 and case 2) in one method
and throwing difference exception for each. That's why I wanted to
know.

Well, if you could show that code, I'd be interested to see it.
Admittedly there could be a difference if someone had overloaded the ==
operator and done it badly, but that's a bug in their operator
overloading rather than a feature of the language.

Nov 16 '05 #9
Marcin Grz?bski <mg*******@taxussi.no.com.spam.pl> wrote:
I think that a "key" is operator overloading.
Like a Jon post it before.

MyClass myVar;

if( null==myVar ) {
}
Doesn't have a chance to fire MyClass "==" operator function.


Yes it does. Compile and run the following, ignoring the warnings:

using System;
using System.Data;

class Test
{
static void Main()
{
Test t = new Test();
Console.WriteLine (t==null);
Console.WriteLine (null==t);
}

public static bool operator == (Test t1, Test t2)
{
Console.WriteLine ("Operator== called");
Console.WriteLine ("t1={0}", t1);
Console.WriteLine ("t2={0}", t2);
return true;
}

public static bool operator!= (Test t1, Test t2)
{
return false;
}
}

The output is:
Operator== called
t1=Test
t2=
True
Operator== called
t1=
t2=Test
True

In other words, the overloaded operator is called both times.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10
Hi Jon,

Hmmm... it looks very strange for me.
But if that work then it seems that i was wrong.

:-(

I'm very curious how "operator==" will work with
class inheritance?

Marcin
Marcin Grz?bski <mg*******@taxussi.no.com.spam.pl> wrote:
I think that a "key" is operator overloading.
Like a Jon post it before.

MyClass myVar;

if( null==myVar ) {
}
Doesn't have a chance to fire MyClass "==" operator function.

Yes it does. Compile and run the following, ignoring the warnings:

using System;
using System.Data;

class Test
{
static void Main()
{
Test t = new Test();
Console.WriteLine (t==null);
Console.WriteLine (null==t);
}

public static bool operator == (Test t1, Test t2)
{
Console.WriteLine ("Operator== called");
Console.WriteLine ("t1={0}", t1);
Console.WriteLine ("t2={0}", t2);
return true;
}

public static bool operator!= (Test t1, Test t2)
{
return false;
}
}

The output is:
Operator== called
t1=Test
t2=
True
Operator== called
t1=
t2=Test
True

In other words, the overloaded operator is called both times.

Nov 16 '05 #11
Marcin Grz?bski <mg*******@taxussi.no.com.spam.pl> wrote:
Hmmm... it looks very strange for me.
But if that work then it seems that i was wrong.

:-(

I'm very curious how "operator==" will work with
class inheritance?


It doesn't, basically. At least, it's an overload rather than an
override. The situation is exactly the same as method overloads - if
you were given

void Foo (object x, object y)
and
void Foo (SomeClass x, SomeClass y)

and it were called with:

SomeClass y = new SomeClass();
Foo (null, y);

you'd expect the latter to be called, wouldn't you? That's exactly the
same with the == operator.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #12
The reason someone may use this is so when they compile and put "=" instead
of "==" when doing comparisons, it will not compile and they will catch
their mistake.

But...

I feel that a programmer should not make these types of mistakes often
enough to get into bad coding habits such as this is (IMHO).

--
Bob Calvanese
<ju******@hotmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
Hello there,

Why is it that I sometimes see this construct

//case 1
if ( null == myVariable)
{
//...etc
}
//case 2
if ( myVariable == null)
{
//...etc
}

Basically, what is the difference between the two?

Nov 16 '05 #13
Bob Calvanese <bc********@comcast.net> wrote:
The reason someone may use this is so when they compile and put "=" instead
of "==" when doing comparisons, it will not compile and they will catch
their mistake.

But...

I feel that a programmer should not make these types of mistakes often
enough to get into bad coding habits such as this is (IMHO).


.... especially in C# when it won't compile if you put the "=" instead
anyway, unless you're comparing with a boolean.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #14
Hi Jon,

I wrote two classes:

class OperatorTest {
public static bool operator==(OperatorTest ot1
, OperatorTest ot2) {
Console.WriteLine("OperatorTest.operator==({0}, {1})"
, ot1
, ot2);
return true;
}
};

and

class OperatorTestNext {
public static bool operator==(OperatorTestNext ot1
, OperatorTestNext ot2) {
Console.WriteLine("OperatorTestNext.operator==({0} , {1})"
, ot1
, ot2);
return true;
}
};

and i wrote the main code:

OperatorTest otA=new OperatorTest();
OperatorTest otB=new OperatorTestNext();
OperatorTestNext otC=new OperatorTestNext();

if( otA==null ) {
}
if( otB==null ) { // Here OperatorText's "==" has been called
}
if( otC==null ) {
}

And "otB" example works with .NET overload way.

Thanks!
Marcin
I'm very curious how "operator==" will work with
class inheritance?

It doesn't, basically. At least, it's an overload rather than an
override. The situation is exactly the same as method overloads - if
you were given

void Foo (object x, object y)
and
void Foo (SomeClass x, SomeClass y)

and it were called with:

SomeClass y = new SomeClass();
Foo (null, y);

you'd expect the latter to be called, wouldn't you? That's exactly the
same with the == operator.

Nov 16 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Matthew Clement | last post by:
I'm currently building a form (called frmReports) to set the criteria for a query, but I'm having some trouble with syntax and hope that one of the guru's here can help me achieve what I'm do. ...
5
by: Danilo Kempf | last post by:
Folks, maybe one of you could be of help with this question: I've got a relatively portable application which I'm extending with a plugin interface. While portability (from a C perspective) is...
4
by: Dim | last post by:
I found that C# has some buggy ways to process string across methods. I have a class with on global string var and a method where i add / remove from this string Consider it a buffer... with some...
14
by: Santi | last post by:
I see in some code, I don´t remember now if it is c# or c++, that the when they perform a comparison they use the value first and then the variable, like: if( null == variable ){} Is there an...
4
by: metalinc | last post by:
hi...im new to C programming...need help...tried to run this code but got this error fork.c: In function ‘parse’: fork.c:44: warning: comparison between pointer and integer fork.c:51: warning:...
1
by: saundap | last post by:
Hi, Access 2000, Windows 2000 Professional I have a form I've developed that is designed for those not familiar with access and its query tool. WIthin a subform in the form there is presented...
20
by: chutsu | last post by:
I'm trying to compare between pointer and integer in an "IF" statement how do I make this work? if(patient.id != NULL){ } Thanks Chris
3
by: DragonLord | last post by:
Here is the situation I am inserting rows into a datagridview and then using a function to group similar rows together based on a column. When I call to compare the lastrow with the current row...
11
by: Andrus | last post by:
I created dynamic extension methods for <= and < SQL comparison operators: public static IQueryable<TLessThanOrEqual<T>(this IQueryable<Tsource, string property, object value); public static...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.