Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

&& Operation in .NET

Which one out of the following two should be preferred while doing && operation on 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?

like image 714
Ram Avatar asked Dec 01 '22 10:12

Ram


2 Answers

(Not A)  AND  (Not B)

is NOT equal to

Not (A And B)
like image 184
Mitch Wheat Avatar answered Dec 15 '22 11:12

Mitch Wheat


Personally I use the former, I find it easier to read if as much information as possible is as close to the point I need it as possible.

For instance, just now I was wondering if your question was whether it was better to put the expression on two lines or just one, until I noticed the part about the not and the parenthesis.

That is, provided they mean the same, which your expressions doesn't.

ie.

if (!a && !b)

is not the same as:

if (!(a && b))

Rather:

if (!(a || b))
like image 41
Lasse V. Karlsen Avatar answered Dec 15 '22 11:12

Lasse V. Karlsen