C# question

Can somebody explain to me what is the different betwen the following statement please:

userName != "" || userName !=string.Empty

It seems to me they are tesing for the same condition in both.
Who is Participating?
abelConnect With a Mentor Commented:
They are very close to being the same. In practice, the difference is negligible.  If you want an in-depth view of how things are the way they are with String.Empty, see this extensive answer of me about a month ago: http://www.experts-exchange.com/Q_24315307.html#24126618
nkhelashviliConnect With a Mentor Commented:
Both are same :)
abelConnect With a Mentor Commented:
bottom line is: your method is good, use String.Empty for clearer code (or String.IsNullOrEmpty(yourString), which also tests for null), but opinions vary on whether it really is clearer. Use never ever the code "userName Is String.Empty", which is not the same and may yield unexpected results (see earlier link on why that is).
tcullerConnect With a Mentor Commented:
Well, it looks like you've got your answer; however, I think abel got confused with his "username Is String.Empty" reference. This is just illegal syntax in C#; it won't return any results at all. The "is" operator (not "Is") is used for checking types, not references or values.

It is arguable as to which one to use (just use one, though). When you make your choice, though, keep it consistent. I would probably recommend String.Empty, as there is less room for error (what if you accidentally plugged in a letter between the quotes for ""?).
> I think abel got confused with his "username Is String.Empty" reference

aha, yes, you are right, thanks for pointing that out. I got mixed up with the referring thread I sent, where VB was used  instead of C# and where "Is" can be used for checking reference-identity. The thing being, that it will only return true in VB when both sides are Interned, but that's another discussion.

tx for the grade ube100 ;-)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.