&& Operation in .NET

Which of the following two options should be preferred when doing && & for two values.

if (!StartTime.Equals(DateTime.MinValue) && !CreationTime.Equals(DateTime.MinValue)) 

or

  if (!(StartTime.Equals(DateTime.MinValue) && CreationTime.Equals(DateTime.MinValue)) 

What is the difference between the two?

+4
source share
6 answers

Personally, I use the first, it’s easier for me to read if as much information as possible is as close to the point that I need it as possible.

For example, only now I was wondering if you had a question: was it better to put the expression on two lines or only one until I noticed a part about this and the brackets.

That is, if they mean the same thing that your expressions do not have.

t

 if (!a && !b) 

- this is not the same as:

 if (!(a && b)) 

Instead

 if (!(a || b)) 
+5
source
 (Not A) AND (Not B) 

NOT equal

 Not (A And B) 
+13
source

Well, it depends on what you want. They both do different things and may be right in this context.

+1
source

As for formatting only, I prefer:

 if(MyFirstExtremlyLongExpressionWhichBarelyFitsIntoALine && MySecondExtremlyLongExpressionWhichBarelyFitsIntoALine && MyThirdExtremlyLongExpressionWhichBarelyFitsIntoALine ) { // ... } 

But if the expressions are really long and full, you should define temporary variables to increase readability:

 bool condition1 = MyFirstExtremlyLongExpressionWhichBarelyFitsIntoALine; bool condition2 = MySecondExtremlyLongExpressionWhichBarelyFitsIntoALine; bool condition3 = MyThirdExtremlyLongExpressionWhichBarelyFitsIntoALine; if(condition1 && condition2 && condition3) { // ... } 

The latter also clarifies your intent if you execute more complex Boolean expressions:

 if((!condition1 && !condition2) && condition3) { // ... } 
+1
source

The first of them is much more readable, and readability is most important here.

Is the second actually equivalent to the first statement? Shouldn't it be ||

0
source

If you give put values, you will see

(! 1 & 1) == (0 & 0) == 0 (! 1 &! 0) == (0 & 1) == 0

! (1 & 1) ==! 1 == 0! (1 & 0) ==! 0 == 1

0
source

Source: https://habr.com/ru/post/1308010/


All Articles