Introduction and examples of logical operators and other operators of the ES0en. net operator

  • 2020-06-19 10:05:53
  • OfStack

The logical (Boolean) operator is used to operate on the expression of the result of the boolean type, which is of the boolean type. The operation results are as follows:

Operator operates on the example result
& AND false (with) & true false
| OR(or) false|true true
^ XOR ^true ^ true
! NOT (non)! false true
& & AND(short circuit) false & & true false
|| OR(short circuit) false||true true


The following is a brief explanation of some relatively prone problems:

1, "^" will calculate the operand logic "xor"; That is, the result is true if and only if only one operand is true.

2." & "And" & & "The difference is that if you use the former connection, then in any case," & Both expressions are evaluated. If you use the latter connection, when" & & "To the left is false, and the expression to the right will not be evaluated.

3. The difference between "|" and "||" is that "|" means that any Boolean expression on both sides is true, and the combination will return true value. For "||", it is similar to the second one. If the left is true, it returns true; if the left is falsh, it looks at the right; if the right is true, it is true; otherwise, it is falsh.

case


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int x = 0;
        string name = " Search! ";
        //& Operation, any of the two 1 If a is false, it is false 
        Response.Write("x != 0 & name = /" Search! /" The calculation results are as follows: "+ (x != 0 & name == " Search! "));
        Response.Write("<br>");
        //| Operation when any of the two 1 When n is true, the operation result is true; otherwise, it is plus 
        Response.Write("x != 0 | name = /" Search! /" The calculation results are as follows: " + (x != 0 | name == " Search! "));
        Response.Write("<br>");
        //^ Operations, if and only if 1 One is True Is true, otherwise false 
        Response.Write("x != 0 ^ name = /" Search! /" The calculation results are as follows: " + (x != 0 ^ name == " Search! "));
        Response.Write("<br>");
        Response.Write("x == 0 ^ name = /" Search! /" The calculation results are as follows: " + (x == 0 ^ name == " Search! "));
        Response.Write("<br>");
        //! Operation, if true then false, if false then true 
        Response.Write("x != 0  The calculation results are as follows: " + !(x != 0));
        Response.Write("<br>");
        //&& Short circuit operation, if the left is false exit, if the left is true, then see the right 
        Response.Write("x != 0 && name = /" Search! /" The calculation results are as follows: " + (x != 0 && name == " Search! "));
        Response.Write("<br>");
        //|| Short circuit operation, if the left side is true, it is true exit; If the left side is false, look at the right side, if the right side is true, then it is true, otherwise it is false 
        Response.Write("x != 0 || name = /" Search! /" The calculation results are as follows: " + (x != 0 || name == " Search! "));
    }
}

The operator

C# provides a number of operators that are symbols that specify which operations to perform in an expression. Enumerations are generally allowed to be evaluated as integers, such as ==,! =, < , > , < =, > =, binary +, binary -, ^, & , |, ~, ++, -- and sizeof(). In addition, many operators can be overloaded by users, thus changing the meaning of those operators when applied to user-defined types.

Operator class operator
Basic x y
f(x)
a[x]
x++
x--
new
typeof
checked
unchecked
- >
RMB 1 +
-
!
~
++x
--x
(T)x
True
False
&
sizeof
Multiplication *
/
%
Add +
-
transform < <
> >
Relationship and type detection <
>
< =
> =
is
as
Equal = =
!=
Logic "and" &
Logic XOR ^
Logic "or" |
Conditions AND & &
Conditions OR | |
Conditional operation? :
Assignment =
+=
-=
*=
/=
%=
& =
|=
^=
< < =
> > =
??

Operator precedence
Before ++ before - + (plus sign) - (minus sign)! ~
* / %
+ -
< < > >
< > < = > =
== !=
&
^

& &

The assignment operation
After + + -
Operator can be overloaded
C# allows user-defined types to override operators by defining static member functions using the operator keyword. But not all operators can be overloaded. The following table lists operators that cannot be overloaded:
Operator reloadability
+, -,! , ~, ++, --, true and false can overload these 1-element operators.
+, -, *, /, %, & , |, ^, < < , > > You can override these binary operators.
==, !=, < , > , < =, > = The comparison operator can be overridden (but see instructions later in this table).
& & , || conditional logic operators cannot be overridden, but can be overridden & And |.
Array index operators cannot be overridden, but indexers can be defined.
() cannot overload transformation operators, but new transformation operators can be defined (see explicit and implicit).
+=, -=, *=, /=, %=, & =, |=, ^=, < < =, > > The = assignment operator cannot be overridden, but += can be evaluated using +, and so on.
=...? :, - > , new, is, sizeof, and typeof cannot overload these operators.

grammar


public static Complex operator +(Complex c1, Complex c2)


Related articles: