comapring condition in if
-
Hi friends What is difference bet 1) if (null == variableName ) And 2) if (variableName == null) is both are same ?????????
yogsworld wrote:
is both are same ?????????
Yes, the are both the same. The reason you might see some people using the first varient is that in some languages (e.g. C, C++), if you made a mistake and typed
if (variableName = null)
by accident the compiler would accept it. (Note the single equal of assignment). By putting the constant on the left hand side of the operator the compiler would reject the assignment and the developer would realise they missed the extra equals sign. In .NET, the langage specififcation states that the result of an
if
statement must be a boolean so the compiler would reject an assignment in an if statement. It will give the error messageCannot implicitly convert type 'object' to 'bool'.
If someVariable was a boolean the error message you'd get is
Cannot convert null to 'bool' because it is a value type
However, there is one scenario where the compiler will accept an assignment in an if statement. That is, if the assignment evalulates to a boolean. e.g.
bool someVariable;
if (someVariable = false) {}The compiler will compile this, but issue a warning instead
Assignment in conditional expression is always constant; did you mean to use == instead of = ?
Does this help?
"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question." --Charles Babbage (1791-1871) My: Website | Blog
-
yogsworld wrote:
is both are same ?????????
Yes, the are both the same. The reason you might see some people using the first varient is that in some languages (e.g. C, C++), if you made a mistake and typed
if (variableName = null)
by accident the compiler would accept it. (Note the single equal of assignment). By putting the constant on the left hand side of the operator the compiler would reject the assignment and the developer would realise they missed the extra equals sign. In .NET, the langage specififcation states that the result of an
if
statement must be a boolean so the compiler would reject an assignment in an if statement. It will give the error messageCannot implicitly convert type 'object' to 'bool'.
If someVariable was a boolean the error message you'd get is
Cannot convert null to 'bool' because it is a value type
However, there is one scenario where the compiler will accept an assignment in an if statement. That is, if the assignment evalulates to a boolean. e.g.
bool someVariable;
if (someVariable = false) {}The compiler will compile this, but issue a warning instead
Assignment in conditional expression is always constant; did you mean to use == instead of = ?
Does this help?
"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question." --Charles Babbage (1791-1871) My: Website | Blog
hi i didn't get your ans ... I just simply ask difference bet if ( someVarial == null) and if (null == somevariable) i.e position of nulll should be first or not I know both expression are evaluating same result.but if u see some code they comaore like if (null == someVarible) i.e position of null is first why ?????? Here someVarible is an refrence type not a value type ...... YOgesh M
-
hi i didn't get your ans ... I just simply ask difference bet if ( someVarial == null) and if (null == somevariable) i.e position of nulll should be first or not I know both expression are evaluating same result.but if u see some code they comaore like if (null == someVarible) i.e position of null is first why ?????? Here someVarible is an refrence type not a value type ...... YOgesh M
yogsworld wrote:
position of null is first why ??????
I explained why. It is a historical thing. Developers who programmed in languages such as C or C++ put null first as a way of protecting themselves by having the compiler flag an error to them. The error being the accidental missing equals sign.
=
means assignment==
means a comparison These two concepts produce radically different results, yet are very very easy to accidentally mix up. Someone typing fast may miss one of the equals signs. In C# the compiler demands that the result of the conditional expression in anif
statement evaluates to a Boolean value. I demonstrated the various error messages that the C# compiler would generate that a C or C++ compiler would not likely generate. I should clarify: Putting thenull
first is not completely the same as putting it second IF there is an error in the code. If the code is written correctly with the double-equals (comparison) operator then the code is the same. Does this make sense?
"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question." --Charles Babbage (1791-1871) My: Website | Blog