4

In reference to using .Equals() or == on strings, here is a question in regards to checking for string.Empty and null objects.

In comparing string.Empty and null objects, should I use == or should I use .Equals()?

// Add vars to instance variables
for (int i = 0; i < paramFirstList.Count; i++)
{
    // if the key is null, replace it
    // with a "null" string
    if (paramFirstList[i] == null)
    {
        _firstList.Add("null");
    }
    else if (paramFirstList[i] == string.Empty)
    {
        _firstList.Add("empty");
    }
    else
    {
        // Do something
    }
}

P.S. I understand it's better to store null and string.Empty as their object types, but for this particular purpose, it is in my requirements to store them as a string representation :).

P.P.S. Added hungarian notation for the sake of question clarity

16
  • The question is not clear, first you're talking of String.Empty and null but then you are adding "empty" and "null". Also you P.S is even more confusing. Commented Feb 26, 2014 at 15:59
  • 1
    @Tim: I believe that the PS is answering your question. He is saying that rather than storing null he is storing "null" and while he knows he should store them as object types (ie null) he is storing them as a string representation (ie "null"). His checks are workign on string.empty and null though so I'm not sure why you are worrying about what he is putting in the list. Commented Feb 26, 2014 at 16:02
  • 1
    @SriramSakthivel: Is that relevant? the thing we are looking at is, as the title says, "Using == or .Equals() on nulls and string.Empty". What he is putting in the list and what type it is seems irrelevant to me... Commented Feb 26, 2014 at 16:06
  • 1
    @Chris Nevermind, confusions due to poor naming conventions :p Commented Feb 26, 2014 at 16:09
  • 1
    Keep in mind that if you try to call s.Equals(null) when s is in fact null you'll end up with an NullReferenceException. Commented Feb 26, 2014 at 16:39

2 Answers 2

4

You should always favor == over Equals. The latter is a method of the base Object type which in this case will do useless casting.

If you mean to check whether a string value is null or empty, use String.IsNullOrEmpty method. If, instead, you need to act differently if it's one or the other, then do this:

if (value == null)
{
    //do stuff
}
else if (value == string.Empty)
{
    // do other stuff
}

EDIT:

As pointed out in the comments there is an overloaded Equals method on a string that receives a string parameter. Still, I think you should take the habit of using ==. It just reads better IMHO.

Sign up to request clarification or add additional context in comments.

11 Comments

"The latter is a method of the base Object type." Just to make it explicit, it is overriden (so it's a value comparision too) and there is also an overload which takes a String parameter.
Thanks, added an additional precision.
There is no boxing/unboxing involved in strings, because string is a reference type
I am rather still confused -- it's a better idea to use value == string.Empty than value.Equals(string.Empty)?
@theGreenCabbage This holds for many languages like e.g. Java, but C# knows operator overloading and indeed, == is overloaded for string. Thus, there is absolutely no difference between == and Equals in C#, besides readability and understandability
|
3

If you're concerned about null, you should use string.IsNullOrEmpty(), or perhaps string.IsNullOrWhitespace()

6 Comments

@Chris I had the feeling this was just sample code.
@Chris but that is no problem either, because comparing against null is no longer necessary he could do sth like if(string.IsNullOrEmpty(stringVar)) { if(stringVar.Equals(string.Empty)) { ..... so one could catch that case too seperately
@DrCopyPaste: Yes, there are ways around it but the title of the question says its about using the merits of == vs .Equals. An answer that doesn't cover either of those for the uses in question is surely not even close to answering the question...
I would note that I have no problem with recommending use of this method as an additional note, I just don't believe it is a valid answer (though voting would seem to disagree with me)
Yep! Detecting separately is important.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.