Click here to Skip to main content
15,895,423 members
Please Sign up or sign in to vote.
3.36/5 (4 votes)
See more:
If string is not "" or is not null, do..., code:
C#
if (string != "" || string != null) {}

but this is not working, i have to write this:
C#
if (string != "" && string != null) {}

Now it works, but this is confusing for me, is it not | (or), & means and?
I need to check both of them or in many cases even more (5, 6,...) of them, what is the right one: || or &&?
Posted
Comments
Sergey Alexandrovich Kryukov 16-Apr-13 2:32am    
Don't use "", always use string.Empty. This will have the same effect, but without dirty immediate constant, even "".
—SA
Sergey Alexandrovich Kryukov 16-Apr-13 2:34am    
What prevented you from just reading the reference guide? You would learn it all much faster. You need to read all the manual, from first to last page, no skipping. If you want to do any programming, of course...
—SA
lewax00 16-Apr-13 17:53pm    
You might also want to look into String.IsNullOrEmpty ( http://msdn.microsoft.com/en-us/library/system.string.isnullorempty.aspx ).

I think youll find all of your answeres here:
http://msdn.microsoft.com/en-us/library/2a723cdk%28v=vs.110%29.aspx[^]

The documentation is always wonderful in such a case :)
 
Share this answer
 
Comments
Maciej Los 16-Apr-13 2:47am    
+5
In C#, there is not "or" and "and". The operators '||' and '&&' are 1) optimized variants of '|' and '&', 2) '||' and '&&' are only Boolean operators, but '|' and '&' are also bitwise ones.

Why use Boolean '|' and '&' then? They are really best avoided, but, for completeness of the Boolean arithmetic, they are needed. You will see the difference if you use a Boolean function with a side effect. Using such functions in a Boolean expression is highly discouraged, but what happens if you do it? Let's see:

C#
bool result = Left() || Right();


As the operator '||' is optimized, it won't evaluate the redundant operand. If Left() returns true, the whole expression will always be true, so the expression evaluation won't call Right(). If Right() has some side effect, it would be lost (never happen).

Likewise in
C#
bool result = Left() && Right();

if Left() returns false, the whole expression is always false, so the expression evaluation won't call Right(). If Right() has some side effect, it would be lost.

Expressions with '|' or '&' will always evaluate all the operands; this way, both functions will always be called. Same thing goes about properties with the getters which cause any side effect.

I hope everything is clear now.

—SA
[EDIT]Fixed apparent copy-paste errors - Matt Heffron[/EDIT]
 
Share this answer
 
v4
Comments
Maciej Los 16-Apr-13 2:47am    
+5
Sergey Alexandrovich Kryukov 16-Apr-13 2:49am    
Thank you, Maciej.
—SA
Sergey Alexandrovich Kryukov 16-Apr-13 17:59pm    
Matt, thank you very much for the fixes, again. This is exactly what it was, not checked up properly.
—SA
If string is not "" then it must be something else.
If string is not null then it must be something else.

So you are saying

If string is something other than "" OR it is something other than null...

Well, if it is "" it is something other than null and if it is null it is something other than "".

When it is confusing, look at it and replace each part of the if using an example. So, say the string is null.

If string != "" || string != null
becomes

if true || false

which = true.

Using && you are saying

IF the string is something other than "" AND it is also something other than null ... in other words, if it is neither null nor empty.


(In fact there is a method String.IsNullOrEmpty() you can use which helps!)


P.S. You should check for null first, as the || and && operators do the checking left to right, so you may get a null reference exception when what you are checking is null.
 
Share this answer
 
Comments
Maciej Los 16-Apr-13 2:47am    
+5
Beside other issues as mentioned in other solutions (e.g. use string.IsNullOrEmpty(str) or if you insist on using your code, first check for null and only then for a defined value, etc.), I think you are confusing with the basic logic: the De Morgan's Law (e.g. see http://en.wikipedia.org/wiki/De_Morgan's_laws[^]).

It defines the logic of negation (pseudo code):
not(a) and not(b) = not(a or  b)
not(a) or  not(b) = not(a and b)


What you have in the first code evolves as shown bellow according to De Morgan's law (I take the check for null first):
C#
  (str != null) ||  (str != "")
 !(str == null) || !(str == "")
!((str == null) &&  (str == ""))
!             false // not both can be true at the same time
              true

This shows clearly that the result is always true.

The second code evolves as follows (null check first again):
C#
  (str != null) &&  (str != "")
 !(str == null) && !(str == "")
!((str == null) ||  (str == ""))

This is one of
a)     !  (true || false)    --> str equals null
       !       true
               false         
b)     ! (false || true)     --> str equals ""
       !       true
               false
c)     ! (false || false)    --> str is neither null nor empty
       !       false
               true

So, the second line gives identical result to:
C#
if (!string.IsNullOrEmpty(str)) { ... }


Cheers
Andi
 
Share this answer
 
v5
MSDN help:

The predefined Boolean logical operators are:

C#
bool operator &(bool x, bool y);
bool operator |(bool x, bool y);
bool operator ^(bool x, bool y);

The result of x & y is true if both x and y are true. Otherwise, the result is false.

The result of x | y is true if either x or y is true. Otherwise, the result is false.

The result of x ^ y is true if x is true and y is false, or x is false and y is true. Otherwise, the result is false. When the operands are of type bool, the ^ operator computes the same result as the != operator.

Source: Boolean logical operators[^]

More at: http://msdn.microsoft.com/en-us/library/6a71f45d.aspx[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Apr-13 2:53am    
You should have at least mention where to read about '|' and '&&' vs. '|' and '&&', because it was really the OP's problem. My 4, for right references.
—SA
Maciej Los 16-Apr-13 2:59am    
Thank you, Sergey ;)

BTW, where is a 4?
;)
Sergey Alexandrovich Kryukov 16-Apr-13 10:36am    
Sorry, I fixed it now. Thanks for reminding me.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900