SoFunction
Updated on 2025-03-07

Detailed explanation of the difference between (&&,|) and (&,|) in c#

For (&&,||), the object of the operation is a logical value, that is, True/False

&& is equivalent to Chinese and, || is equivalent to Chinese or . (called logical operators, also known as short-circuit operators)

There are only four cases in the following calculation results.

True  && True  = True    (The left is true, and then verify that the right is true, return the result true) If this is a query condition, execute.
True  && False = False   (The left is true, then verify that the right is false, return the result false) If this is a query condition, it will not be executed.
False && True  = False   (The left is false, a short circuit occurs. The right is no longer executed, and it will directly return to false).........I will not execute either.
False && False = False   (Similar to above)
True  || True  = True    (The left is true, a short circuit occurs, and the right is not executed, and it will directly return true)....Execute
True  || False = True    (The left is true, a short circuit occurs, the right is not executed, and it will directly return true)...Execute
False || True = True    (False on the left, verify that the right is true, return the result true).........Execute
False || False = False   (The left is false, and then verify that the right is also false, return the result false)....No execution

For (&,|), the object of the operation is bit, that is, 1/0, which is called a bit operator

Understanding: 0 is false, 1 is true (General: 0 means false, and all non-zero numbers represent true.  ###### Convenient memory: 0, nothing is lying, then false)
There are only four cases in the following calculation results.

1 & 1 = 1
1 & 0 = 0
0 & 1 = 0
0 & 0 = 0

1 | 1 = 1
1 | 0 = 1
0 | 1 = 1
0 | 0 = 0

&& and & are the same for their respective operation objects.

The following is a piece of code to illustrate the application of || in actual code

var data = ("TaxType").Where(f =>  ==  &&  == 2 &&
                          (! ||  == )).ToList();

When TaxTypeId==null in the passed parameter, it is true, and a short circuit occurs. The right side is not executed, and the result is true.

Then the actual code is executed: var data = ("TaxType").Where(f => == && == 2).ToList();

When TaxTypeId==123 is passed in the parameter, it is false, and the code on the right is executed, that is, == 123,

Then the actual code is executed: var data = ("TaxType").Where(f => == && == 2 &&  == 123).ToList();

Scope of application: When we check the conditions for query or manipulate the database, we can select any number of conditions for query, and just call the same method. (i.e. a service completes multiple conditions query) Reduce redundant code.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.