What is null equal to?
-
I come before you with a question, a yearning in my heart. What is null..... Equal to?
...
if (value == null || value.equals(null)) {
return "null";
}
...Evidently, the concept of "null" and "null pointers" and "null pointer calls" is lost on the guys who wrote this particular line of code. It doesn't make any errors, probably because someone added the first condition after a few NPEs happened... EDIT: Clarification - the title is rhetorical.
“==” compares if the object references are same while “.Equals()” compares if the contents are same. So if you run the below code both “==” and “.Equals()” returns true because content as well as references are same.
object o = ".NET Interview questions";
object o1 = o;
Console.WriteLine(o == o1);
Console.WriteLine(o.Equals(o1));
Console.ReadLine();True True Now consider the below code where we have same content but they point towards different instances. So if you run the below code both “==” will return false and “.Equals()” will return true.
object o = ".NET Interview questions";
object o1 = new string(".NET Interview questions".ToCharArray());
Console.WriteLine(o == o1);
Console.WriteLine(o.Equals(o1));
Console.ReadLine();False True
Sankarsan Parida
-
I come before you with a question, a yearning in my heart. What is null..... Equal to?
...
if (value == null || value.equals(null)) {
return "null";
}
...Evidently, the concept of "null" and "null pointers" and "null pointer calls" is lost on the guys who wrote this particular line of code. It doesn't make any errors, probably because someone added the first condition after a few NPEs happened... EDIT: Clarification - the title is rhetorical.
sloosecannon wrote:
What is null equal to?
The place I worked in Germany?
Sign a petition calling for the boycott of Israel until it returns to its legal 1967 borders.
-
See the text at the top of the page: "a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance." Nearly everything here is found in real code and will hopefully make you go :doh: :WTF: :OMG: And laugh that anyone could think that the right thing to do... Generally, explanations and code fragment after the original are expected to make even less sense: just to prove it can be done! :laugh: For example, another way to do the original method would be
public object IsNull(object o)
{
try
{
return o.Equals(null) ? o : o;
}
catch
{
return null;
}
}But you'd have to be a complete moron to write that! Oops. I just did... :-O
You looking for sympathy? You'll find it in the dictionary, between sympathomimetic and sympatric (Page 1788, if it helps)
Q.E.D. (Sorry, it was just sitting there waiting for it).
"If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.