Huh? Can this be logical?
-
Looking for some answers on MSDN I ran across the following code.
If (null != email)
Is that legal in C#, and even if it does compile, is it logical? Shouldn't it more likely read:If (email != null)
Best, JerryContrary to the cliche, genuinely nice guys most often finish first or very near it.--Malcolm Forbes
This is a wierd convention that older C programmers follow. It is perfectly legal. The reason that it is done, is that in C, it is legal to say
if (email = null)
, (note the single equal sign) and it will always evaluate to true. So, to prevent possible errors caused by mistypes, C programmers switch the arguments, i.e.if (null = email)
. This second version is not legal syntax, and will give a compiler error. The programmer can then fix it by changing it toif (null == email)
. This is not necessary in C# (but neither is it illegal), because the C# compiler will complain atif (email = null)
.
-
This is a wierd convention that older C programmers follow. It is perfectly legal. The reason that it is done, is that in C, it is legal to say
if (email = null)
, (note the single equal sign) and it will always evaluate to true. So, to prevent possible errors caused by mistypes, C programmers switch the arguments, i.e.if (null = email)
. This second version is not legal syntax, and will give a compiler error. The programmer can then fix it by changing it toif (null == email)
. This is not necessary in C# (but neither is it illegal), because the C# compiler will complain atif (email = null)
.
Steven Campbell wrote: if (email = null), (note the single equal sign) and it will always evaluate to true. allways be false i think u mean :p top secret
Download xacc-ide 0.0.3 now!
See some screenshots -
Looking for some answers on MSDN I ran across the following code.
If (null != email)
Is that legal in C#, and even if it does compile, is it logical? Shouldn't it more likely read:If (email != null)
Best, JerryContrary to the cliche, genuinely nice guys most often finish first or very near it.--Malcolm Forbes
To expand on what Steven said, the two are the same because it's a simple comparison. The IL generated from about would be:
ldnull
ldfld email
beq Equal // If the two are equal skip the if block
// if block runs here
Equal:
// After the if blockIt doesn't matter if you load null or the field (or variable, whichever) first, you're still comparing the value of two objects. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]
-
Looking for some answers on MSDN I ran across the following code.
If (null != email)
Is that legal in C#, and even if it does compile, is it logical? Shouldn't it more likely read:If (email != null)
Best, JerryContrary to the cliche, genuinely nice guys most often finish first or very near it.--Malcolm Forbes
Thanks for the answers all. Would this also hold true for the is-equal-to '==' also? Best, Jerry
Contrary to the cliche, genuinely nice guys most often finish first or very near it.--Malcolm Forbes
-
Thanks for the answers all. Would this also hold true for the is-equal-to '==' also? Best, Jerry
Contrary to the cliche, genuinely nice guys most often finish first or very near it.--Malcolm Forbes
OMG! I just woke up and realized what a stupid question that was. That's what I get for watching the grandchildren and coding at the same time... Best, Jerry
Contrary to the cliche, genuinely nice guys most often finish first or very near it.--Malcolm Forbes
-
Steven Campbell wrote: if (email = null), (note the single equal sign) and it will always evaluate to true. allways be false i think u mean :p top secret
Download xacc-ide 0.0.3 now!
See some screenshotsI'd expect it to be true, because the assignment would not fail. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
-
This is a wierd convention that older C programmers follow. It is perfectly legal. The reason that it is done, is that in C, it is legal to say
if (email = null)
, (note the single equal sign) and it will always evaluate to true. So, to prevent possible errors caused by mistypes, C programmers switch the arguments, i.e.if (null = email)
. This second version is not legal syntax, and will give a compiler error. The programmer can then fix it by changing it toif (null == email)
. This is not necessary in C# (but neither is it illegal), because the C# compiler will complain atif (email = null)
.
Steven Campbell wrote: The programmer can then fix it by changing it to if (null == email). Ok, but how can
null
be "equal to" anything? This has got to be a concept I'm not able to fathom or I am just unfamiliar with it. And, for now, it just seem logical. Best, Jerry
Contrary to the cliche, genuinely nice guys most often finish first or very near it.--Malcolm Forbes
-
Steven Campbell wrote: The programmer can then fix it by changing it to if (null == email). Ok, but how can
null
be "equal to" anything? This has got to be a concept I'm not able to fathom or I am just unfamiliar with it. And, for now, it just seem logical. Best, Jerry
Contrary to the cliche, genuinely nice guys most often finish first or very near it.--Malcolm Forbes
Any reference object - like
email
(presumably a string, which is a reference type) - is null if it doesn't reference an object. So, it's "value" is null.string email = null;
null == email; // Returns true
email = "you@codeproject.com";
null == email; // Returns false, references the string "you@codeproject.comThis posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]
-
I'd expect it to be true, because the assignment would not fail. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
The condition isn't checking the assignment, it would check the resultant expression. In ths case, that'd be
email
. The code breaks apart like so:email = null;
if (email)
{
// ...
}In C#, however, the above expression isn't valid like it would be in C/C++ (where
NULL
is defined as 0). You would get an error in this case, but not because you'd be using an assignment operator instead of an equivalence operator. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] -
The condition isn't checking the assignment, it would check the resultant expression. In ths case, that'd be
email
. The code breaks apart like so:email = null;
if (email)
{
// ...
}In C#, however, the above expression isn't valid like it would be in C/C++ (where
NULL
is defined as 0). You would get an error in this case, but not because you'd be using an assignment operator instead of an equivalence operator. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]Oh, OK. Obviously I had not tried it. Heath Stewart wrote: Software Design Engineer Developer Division Sustained Engineering Microsoft Cool. Do you know anything about DX9, specifically the AudioVideoPlayback namespace ? Was it released by mistake ? I've had a lot of fun figuring out it's many flaws..... Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
-
Steven Campbell wrote: The programmer can then fix it by changing it to if (null == email). Ok, but how can
null
be "equal to" anything? This has got to be a concept I'm not able to fathom or I am just unfamiliar with it. And, for now, it just seem logical. Best, Jerry
Contrary to the cliche, genuinely nice guys most often finish first or very near it.--Malcolm Forbes
if 2+2 = 4, then 4 = 2+2. The concept is the same. The two values are checked for equality, that the constant value is on the left makes no difference to the compiler, and in C++ was a good was to avoid a bug that can be a nightmare to find. I know, I still sometimes write code like this, a habit I formed in my C++ days. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
-
Oh, OK. Obviously I had not tried it. Heath Stewart wrote: Software Design Engineer Developer Division Sustained Engineering Microsoft Cool. Do you know anything about DX9, specifically the AudioVideoPlayback namespace ? Was it released by mistake ? I've had a lot of fun figuring out it's many flaws..... Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
I've worked with DX9 a little, but I admit I don't know a whole lot about it (mostly from a graphics manipulation regard, not API use). I recommend reading Managed DirectX 9 Kick Start : Graphics and Game Programming[^]. It's a very good book and you'd probably have a better time with it. Like I said, I understand all the API stuff, but matrices - when it comes to graphics - always seem to elude me. I will say, however, that you're articles have helped clear a few things up. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]
-
if 2+2 = 4, then 4 = 2+2. The concept is the same. The two values are checked for equality, that the constant value is on the left makes no difference to the compiler, and in C++ was a good was to avoid a bug that can be a nightmare to find. I know, I still sometimes write code like this, a habit I formed in my C++ days. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
Ok, thanks Heath and Christian. I think I understand what's going on with that now. Best, while(null==jerry) { null++; return; }
Contrary to the cliche, genuinely nice guys most often finish first or very near it.--Malcolm Forbes
-
I've worked with DX9 a little, but I admit I don't know a whole lot about it (mostly from a graphics manipulation regard, not API use). I recommend reading Managed DirectX 9 Kick Start : Graphics and Game Programming[^]. It's a very good book and you'd probably have a better time with it. Like I said, I understand all the API stuff, but matrices - when it comes to graphics - always seem to elude me. I will say, however, that you're articles have helped clear a few things up. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]
Heath Stewart wrote: I recommend reading Managed DirectX 9 Kick Start : Graphics and Game Programming[^]. I have it. I assume the rest of the book is better, the bit on audio video playback is 3 pages which glosses over the API, tells me that Microsoft does not regard it as a full implimentation, and it's not going to be updated. It doesn't cover any of the bugs in the API, I presume the author has never actually used this part of DirectX9, and felt they needed to cover it, so they skimmed the docs. I'm writing an article on all the ways that the AudioVideoPlayback namespace fails to perform as expected, and a wrapper class that will make it a little bit close to usable. I've built an entire project around DX9, and I'm kind of wishing I hadn't. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
-
Looking for some answers on MSDN I ran across the following code.
If (null != email)
Is that legal in C#, and even if it does compile, is it logical? Shouldn't it more likely read:If (email != null)
Best, JerryContrary to the cliche, genuinely nice guys most often finish first or very near it.--Malcolm Forbes
In C++, it's possible to write: if (x = 0) when you mean to write if (x == 0) Because of that, some developers invert the order of what they write, as: if (0 = x) is illegal, while if (0 == x) is legal. This is not a problem in C#, where if (x = 0) is prohibited.