. NET Foundation 2

Source: Internet
Author: User
Tags arithmetic operators logical operators

In this article we will explain the operators in C #

1. Arithmetic operators in C #

  5 Arithmetic operators: +-*/%

They are all two-dollar operators, */% with the same priority, and higher than +-, +-the same operation priority. We can use parentheses to change the priority of the calculation, note: Parentheses must appear in pairs.

The type of operands on either side of the operator is consistent, and the result of the operation is consistent with the type of the operand.

            int Ten 3 ;             // the result is 3.            Console.WriteLine ("num = {0}"1.0);    

x/y * 1.0 This expression, the first X/y, the result is 3, 3 is multiplied by 1.0, the result is 3.0, but the output, will not output 3.0, but 3

            Double 3 1.0 ;             // Get the result is 3            Console.WriteLine ("VVV = {0}", VVV);

   The int type is multiplied by the double type, and the result is automatically converted to a double type.

            int Ten 3 ;             // the result is 3.33333333333333.            Console.WriteLine ("num = {0}"1.0));

2. Assignment operators in C #

  6 Assignment operators: = + = = *=/=%=

They require the same type of operand to be involved in the operation, and the result of the operation is the same as the type of the operand. If the type of operand that participates in the operation is inconsistent, the conversion is based on the type auto-conversion rule.

The 6 operators have the same precedence, and the precedence is the lowest among all operators .

3. Logical operators in C #

  3 Logical operators: && (and) | | (OR)! Non-

  Only! is a unary operator, and the other is a two-tuple operator

  Priority level! > && > | |

  The participating operations must be expressions that evaluate to type bool , such as 4 > 2

(expression) && (expression) The result is true only if all two expressions are true

(expression) | | (expression) The result is false only if two expressions are all false

! (expression) for inverse operation, when the expression is true, the result is false;

  Note: && | | A short circuit is possible.

            intA =Ten; intb =5; //when the result of the preceding expression is false, the subsequent expression does not participate in the operation, resulting in a short-circuit            BOOLresult = ++a > -&& ++b >1; Console.WriteLine ("a = {0} b = {1}", A, b); //a = one b = 5//when the result of a logical or previous expression is true, the subsequent expression does not participate in the operation, resulting in a short-circuit            BOOLres = ++a < -|| ++b <1; Console.WriteLine ("a = {0} b = {1}", A, b); //A = b = 5

4. Relational operators in C #

  6 Relational operators: > < = = = >= <=

They all operate as bool types, and the operands that participate in the operation must be of the same type.

5. Self-increment, decrement operator in C #

This includes both: + +--operator positions before and after the operand can be divided into pre-and post-

The post priority is higher than the predecessor

  1. Rear-facing

When a number++ or number--participates in an expression operation, because the operator is behind number, the operation is preceded by the value in the expression, and then the value of number +1 or-1.

    int  Ten + number++;    We can understand it as    :int + number ;     1;

  So we get the conclusion:+ +--when the post is placed, no matter how complex the expression is, wait for the expression to be evaluated after the execution of the + + or--operation.

            intAge = -; /*when the post-add is in an expression, no matter how complex the expression is, only wait until the expression operation is complete, and then execute the Post + +*/            intsum = (age++ + A) *3-Ten; //the above sentence is equivalent to//int sum = (age + 12) * 3-10; //age++;Console.WriteLine ("Age = {0}", age);//Age =Console.WriteLine ("sum = {0}", sum);//sum =

  2. Front-facing

    When the ++number or-number participates in an expression operation, because the operator precedes the number, the value +1 or-1is first used in the operation, andthen the value of # participates in the expression's operation .

    int Ten + + + number;    We can understand it as    :1;     int ten + number;

Example:

            intvar1, var2 =5, Var3 =6; Var1= var2++ *--Var3; //var1 = 25,var2 = 6,VAR3 = 5Console.WriteLine ("var1 = {0},var2 = {1},var3 = {2}", Var1, Var2, VAR3); Var1=0; VAR2 =5; VAR3 =6; Var1= ++var2 * var3--; //var1 = 36,var2 = 6,var3 = 5;Console.WriteLine ("var1 = {0},var2 = {1},var3 = {2}", Var1, VAR2, VAR3);

6. Ternary operators in C #

Syntax: condition? result 1: Result 2

    int 2 ;     int 3 ;     int b = (a>c)? a:c;     // The value of B is 3

equivalent to the If-else statement

    int 2 ;     int 3 ;     int b;     if (A > c)    {       = A;      }     Else     {       = c;     }

7. The operator table in C #

Arrange according to the precedence of operators

The precedence of some commonly used operations is listed here, followed by the precedence of operators in order from highest to lowest . For more information about operators, refer to the https://msdn.microsoft.com/zh-cn/library/ms173145.aspx

 

The precedence of */% in arithmetic operators is higher than +-

8. Binding of operators in C #

Divide by direction, divided into left and right combination

x * y/z is evaluated as (x * y)/z . " For example, x * y/z  will be calculated as   (x * y)/Z.   For example, the assignment operator is right-associative.

the assignment operator and the ternary operator (?:) are right associative operators. all other two-tuple operators are left associative operators.   

regardless of whether the operator in the expression is a left-associative or right-associative operator, the operands of each expression are evaluated first from left to right.

9. Operator overloading in C #

For custom classes and structs, you can change the behavior of the operators. This process is called operator overloading.

. NET Foundation 2

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.