The logical operators of the asp.net operator and other operators Introduction and example _ Practical tips

Source: Internet
Author: User
Tags logical operators true true

The logical (Boolean) operator is used to perform an operation on an expression of the result of a Boolean, and the result of the operation is Boolean. The results of the operation are as follows:

Operator Operation Example Results
& and (with) False&true false
| or (or) false|true true
^ XOR (exclusive OR) False^true true
! Not (non)!false true
&& and (short circuit) false&&true false
|| OR (Short circuit) false| | True True


Here are some simple explanations for some of the more likely problems:

1. "^" Computes the logical "XOR" of the operand, that is, the result is true if and only if only one operand is true.

2. The difference between "&" and "&&" is that if you use the former connection, then the expressions on both sides of "&" will participate in the calculation in any case. If the latter connection is used, the expression to the right of "&&" will not be evaluated if the left side is false.

3, "|" and "| |" The difference is that "|" Indicates that either of the Boolean expressions on either side are true, the combination returns a true value, and for "| |", the same as the second, and true if the left is true, or to the right if the left is Falsh, or falsh if the right is true.

Cases

Copy Code code as follows:

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 Bar";

& operation, two of any one of the false is False
Response.Write ("x!= 0 & name =/Search bar/" result is: "+ (x!= 0 & name = =" Search bar "));

Response.Write ("<br>");

| operation, when either of the two is true, the result of the operation is true, otherwise the
Response.Write ("x!= 0 | The result of the name =/"Search bar/" operation is: "+ (x!= 0 | name = =" Search bar "));

Response.Write ("<br>");

^ operation, true if and only if one is true, otherwise false
Response.Write ("x!= 0 ^ name =/" Search bar/"result is:" + (x!= 0 ^ Name = = "Search bar"));
Response.Write ("<br>");
The result of the Response.Write ("x = = 0 ^ name =/Search Bar/" is: "+ (x = = 0 ^ Name = =" Search bar "));
Response.Write ("<br>");

Operation, if True false, if False is true
Response.Write ("x!= 0 of the operation result is:" +! ( X!= 0));

Response.Write ("<br>");

&& short-circuit operation, if left is false then exit, if the left is true, then look to the right
Response.Write ("x!= 0 && name =/Search bar/" result is: "+ (x!= 0 && name = =" Search bar "));

Response.Write ("<br>");
//|| Short-circuit operation, if the left is true, then the true exit; If the left is false, look to the right, the right is true, it is true, otherwise false
Response.Write ("x!= 0 | | The result of the name =/"Search bar/" operation is: "+ (x!= 0 | | | | name = =" Search bar "));
}
}

Operator

     C # provides a large number of operators, which are symbols that specify what actions to perform in an expression. Enumeration is generally allowed for integer operations, such as = =,!=, <, >, <=, >=, binary +, binary-, ^, &, |, ~, + + 、--, and sizeof (). In addition, many operators can be overloaded by the user, thereby changing the meaning of these operators when applied to user-defined types.

Operator category operator
Basic x.y
F (x)
A[x]
x +
x--
New
typeof
checked
unchecked
->
unary +
-
!
~
++x
--x
(T) x
True
False
+
sizeof
Multiply *
/
%
Add +
-
transform <<
> >
Relationship and type detection

<=
>=
is
as
equal = =
!=
Logic and &
Logical XOR ^
Logic or |
Conditions and &&
Conditions OR | |
Conditional operation?:
Assignment =
=
-=
*=
/=
% =
&=
|=
^=
<<=
>>=
??

Operator Precedence
Front + + + (plus)-(minus sign)! ~
* / %
+ -
<< >>
< > <= >=
== !=
&
^

&&

Assignment operations
After + +--
Overloaded operators
C # allows user-defined types to overload operators by defining static member functions using the operator keyword. However, not all operators can be overloaded, and the following table lists the operators that cannot be overloaded:
operator can be overloaded
+ 、-、!、 ~, + + 、--、 true and false can overload these unary operators.
+,-, *,/,%,, |, ^, <<, >> can overload these binary operators.
= =,!=,, <=, >= comparison operators can be overloaded (but see the instructions later in this table).
&&, | | Conditional logical operators cannot be overloaded, but can be overloaded with & and | For calculation.
[] The array index operator cannot be overloaded, but the indexer can be defined.
() You cannot overload a conversion operator, but you can define a new conversion operator (see explicit and implicit).
+ =, =, *=,/=,%=, &=, |=, ^=, <<=, >>= assignment operators cannot be overloaded, but = = can use the + calculation, and so on.
= 、.、?:,->, New, is, sizeof, and TypeOf cannot overload these operators.

Grammar

Copy Code code as follows:

public static Complex operator + (Complex C1, Complex C2)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.