首页 > 代码库 > Operators

Operators

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

 

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 Output15 2.516 1.517 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 suffixes)

 

Operators