Operators

// -Operators can be roughly classified into three categories:
// -Unary — Act on single operands
// -Binary — Act on two operands
// -Ternary — Act on three operands

 

// ?:
// The conditional operator (?:) returns one of two values depending on the value of a Boolean expression. Following is the syntax for the conditional operator.
//
// The condition must evaluate to true or false. If condition is true, first_expression is evaluated and becomes the result. If condition is false, second_expression is evaluated and becomes the result. Only one of the two expressions is evaluated.
//
// Either the type of first_expression and second_expression must be the same, or an implicit conversion must exist from one type to the other.
//
//
// The conditional operator is right-associative. The expression a ? b : c ? d : e is evaluated as a ? b : (c ? d : e), not as (a ? b : c) ? d : e.
//
// The conditional operator cannot be overloaded.
//

 

Mathematical Operators

// Simple Mathematical Operators
// OPERATOR CATEGORY EXAMPLE EXPRESSION    RESULT
//    +     Binary   var1 = var2 + var3;   var1 is assigned the value that is the sum of var2 and var3.
//    -     Binary   var1 = var2 - var3;   var1 is assigned the value that is the value of var3 subtracted from the value of var2.
//    *     Binary   var1 = var2 * var3;   var1 is assigned the value that is the product of var2 and var3.
//    /     Binary   var1 = var2 / var3;   var1 is assigned the value that is the result of dividing var2 by var3.
//    %     Binary   var1 = var2 % var3;   var1 is assigned the value that is the remainder when var2 is divided by var3.
//    +     Unary    var1 = +var2;         var1 is assigned the value of var2.
//    -     Unary    var1 = -var2;         var1 is assigned the value of var2 multiplied by -1.
//
//
// Increment and Decrement Operators
// OPERATOR CATEGORY EXAMPLE EXPRESSION RESULT
//   ++     Unary    var1 = ++var2;     var1 is assigned the value of var2 + 1. var2 is incremented by 1.
//   --     Unary    var1 = --var2;     var1 is assigned the value of var2 - 1. var2 is decremented by 1.
//   ++     Unary    var1 = var2++;     var1 is assigned the value of var2. var2 is incremented by 1.
//   --     Unary    var1 = var2--;     var1 is assigned the value of var2. var2 is decremented by 1.
//
//
// ++ always results in its operand being incremented by one.
// −− always results in its operand being decremented by one.
//
// Placing one of these operators before its operand means that
// the operand is affected before any other computation takes place. Placing it after the operand means that the
// operand is affected after all other computation of the expression is completed.

 1 class MainClass
 2 {
 3     static void Main()
 4     {
 5         double x;
 6         x = 1.5;
 7         Console.WriteLine(++x);
 8         x = 1.5;
 9         Console.WriteLine(x++);
10         Console.WriteLine(x);
11     }
12 }
13 
14 Output
15 2.5
16 1.5
17 2.5

 

Assignment Operators

 

// Like =, they all result in a value being assigned to the variable on their left side based on the operands and operators on their right side.
//
// OPERATOR CATEGORY EXAMPLE EXPRESSION RESULT
//   =      Binary   var1 = var2;       var1 is assigned the value of var2.
//   +=     Binary   var1 += var2;      var1 is assigned the value that is the sum of var1 and var2.
//   -=     Binary   var1 -= var2;      var1 is assigned the value that is the value of var2 subtracted from the value of var1.
//   *=     Binary   var1 *= var2;      var1 is assigned the value that is the product of var1 and var2.
//   /=     Binary   var1 /= var2;      var1 is assigned the value that is the result of dividing var1 by var2.
//   %=     Binary   var1 %= var2;      var1 is assigned the value that is the remainder when var1 is divided by var2.

 

Operator Precedence

 

// PRECEDENCE   OPERATORS
// Highest      ++, −− (used as prefi xes); (), +, – (unary), !, ˜
//              *, /, %
//              +, –
//              <<, >>
//              <, >, <=, >=
//              ==, !=
//              &
//              ∧
//              |
//              &&
//              ||
//              =, *=, /=, %=, +=, −=, <<=, >>=, &=, ^=, |=
// Lowest       ++, –– (used as suffi xes)

 

posted on 2014-10-22 20:23  yoghourt  阅读(301)  评论(0编辑  收藏  举报