Introduction and examples of asp.net operators and other operators

Source: Internet
Author: User

The logical (boolean) operator is used to calculate the expression of the result of the boolean type. The result of the operation is boolean type. The calculation result is as follows:

Operator example result
& AND (AND) false & true false
| OR (OR) false | true
^ XOR (XOR) false ^ true
! NOT (NOT )! False true
& AND (Short Circuit) false & true false
| OR (Short Circuit) false | true


The following is a simple explanation of some problems:

1. "^" refers to the logic of the calculated operand "exclusive or". That is to say, the result is true only when only one operand is true.

2. the difference between "&" and "&" is that if the former is used for join, expressions on both sides of "&" are involved in calculation under any circumstances. If the latter connection is used, if the left side of "&" is false, the expression on the right side is not calculated.

3. the difference between "|" and "|" is that "|" indicates that any Boolean expression on both sides is true, and true is returned for the combination. For "| ", similar to the second. If the left side is true, true is returned. If the left side is falsh, the right side is displayed. If the right side is true, true is returned. Otherwise, falsh is returned.

Example

Copy codeThe Code is 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 ";

// & Operation. If either of the two values is false, the operation is false.
Response. Write ("x! = 0 & name =/"Search/": "+ (x! = 0 & name = "Search "));

Response. Write ("<br> ");

// | Operation. If either of the two values is true, the operation result is true. Otherwise
Response. Write ("x! = 0 | name =/"Search/": "+ (x! = 0 | name = "Search "));

Response. Write ("<br> ");

// ^ Operation. if and only if one of them is True, the operation is True. Otherwise, the operation is false.
Response. Write ("x! = 0 ^ name =/"Search/": "+ (x! = 0 ^ name = "Search "));
Response. Write ("<br> ");
Response. write ("x = 0 ^ name =/" Search/"returns:" + (x = 0 ^ name = "Search "));
Response. Write ("<br> ");

//! Operation. If it is true, it is false. If it is false, it is true.
Response. Write ("x! The calculation result of = 0 is: "+! (X! = 0 ));

Response. Write ("<br> ");

// & Short circuit operation. If the left side is false, exit. If the left side is true, check the right side.
Response. Write ("x! = 0 & name =/"Search/": "+ (x! = 0 & name = "Search "));

Response. Write ("<br> ");
// | Short-circuit operation. If the left side is true, the system exits. If the left side is false, the system returns to the right side. If the right side is true, the system exits. Otherwise, the system returns false.
Response. Write ("x! = 0 | name =/"Search/": "+ (x! = 0 | name = "Search "));
}
}

Operator

C # provides a large number of operators that specify which operations are performed in the expression. You can perform integer operations on enumeration, such as = ,! =, <,>, <=,> =, Binary +, binary-, ^, &, | ,~ , ++, --, And sizeof (). In addition, many operators can be reloaded by users, thus 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
->
RMB +
-
!
~
++ X
-- X
(T) x
True
False
&
Sizeof
Multiplication *
/
%
Addition +
-
Transform <
>
Link and type detection <
>
<=
> =
Is
As
Equal =
! =
Logic "and "&
Logic XOR ^
Logic "or" |
Conditions AND &&
Condition OR |
Conditional operation? :
Assignment =
+ =
-=
* =
/=
% =
& =
| =
^ =
<=
>>=
??

Operator priority
Front + + front -- + (positive)-(negative )! ~
*/%
+-
<>
<> <=> =
=! =
&
^

&&

Value assignment
Post ++ post --
Reload Operators
C # allows users to define types and use the operator keyword to define static member functions to overload operators. However, not all operators can be overloaded. The following table lists operators that cannot be overloaded:
Operator Overloading
+,-, And ,-,! ,~ , ++, --, True, and false can be used to reload these unary operators.
+,-, *,/, %, &, |, ^, <,> Can overload these binary operators.
= ,! =, <,>, <=,> = The comparison operator can be overloaded (but see the description below this table ).
&, | Conditional logical operators cannot be overloaded, but can be computed using the & and | that can be reloaded.
[] The array index operator cannot be overloaded, but the indexer can be defined.
() The conversion operator cannot be overloaded, but the new conversion operator can be defined (see explicit and implicit ).
+ =,-=, * =,/=, % =, & =, | =, ^ =, <=, >>= The value assignment operator cannot be overloaded, but + = Can Be Used + computing, and so on.
= ,.,? :,->, New, is, sizeof, and typeof cannot be overloaded.

Syntax

Copy codeThe Code is 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.