What is the difference between (& &, | |) and (& , |) in C #
What is the difference between (&, |) and (&, |) in C #? I believe many inexperienced people are at a loss about it. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
For (& &, | |), the operation object is a logical value, that is, True/False
& & equivalent to Chinese and, | | equivalent to Chinese or. (called logical operator, also called short-circuit operator)
The result of the operation is only in the following four cases.
True & & True = True (true on the left, true on the right and true returned) if this is a query condition, execute it. True & & False = False (true on the left, false on the right, and return the result false) if this is a query condition, do not execute. False & & True = False (false on the left, short circuit occurs. No more execution on the right, return directly to false). Also do not carry out. False & & False = False (ditto) True | | True = True (true on the left, short circuit occurs, do not execute on the right, return true directly). Execute True | | False = True (true on the left, short circuit occurs, do not execute on the right, return true directly). Execute False | | True = True (false on the left, true on the right and return the result true). Execute False | | False = False (false on the left, false on the right, and return the result false). Do not execute
For (&, |), the object of the operation is the bit, which is called the bit operator.
Understanding: 0 is false,1 and true (generic: 0 means false, and all non-zero numbers represent true. # easy to remember: 0, if nothing is deceived, it will be false) the result of the operation is only the following four cases.
1 & 1 = 11 & 0 = 00 & 1 = 00 & 0 = 01 | 1 = 11 | 0 = 10 | 1 = 10 | 0 = 0
& & and & the result is the same for their respective operands.
The following is a code to illustrate the application of | | in the actual code
Var data = svc.DeclarationHeads.Include ("TaxType") .Where (f = > f.CorporationCode = = param.CorporationCode & & f.FlowSign = = 2 & & (! param.TaxTypeId.HasValue | | f.TaxTypeId = = param.TaxTypeId)) .ToList ()
When TaxTypeId==null is passed in the parameter, param.TaxTypeId.HasValue is true, which causes a short circuit. If it is not executed on the right, the result true is returned.
Then the code is actually executed: var data = svc.DeclarationHeads.Include ("TaxType") .Where (f = > f.CorporationCode = = param.CorporationCode & & f.FlowSign = = 2) .ToList ()
When TaxTypeId==123 is passed in the parameter, param.TaxTypeId.HasValue is false, execute the code on the right, that is, f.TaxTypeId = = 123
Then the code is actually executed: var data = svc.DeclarationHeads.Include ("TaxType") .Where (f = > f.CorporationCode = = param.CorporationCode & & f.FlowSign = = 2 & & f.TaxTypeId = = 123) .ToList ()
Scope of application: when we check the conditions to query or manipulate the database, we can select any number of conditions to query, and only need to call the same method. (that is, a service completes multiple conditional queries) reduces redundant code.
After reading the above, have you mastered the difference between (&, |) and (&, |) in c #? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!