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? 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?
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
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
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
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
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?
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
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.
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
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.
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
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?
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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.
...
|
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...
|
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...
|
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...
|
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:...
|
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...
|
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
|
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...
|
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...
|
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,...
|
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...
|
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
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |