Csharp/CSharp Tutorial/Operator/Prefix Postfix Operator

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Increment and Decrement

The ++ and the -- are the increment and decrement operators.

The increment operator adds 1 to its operand, and the decrement operator subtracts 1.


x = x + 1;

Operators ++

using System;
  class Class1
  {
    [STAThread]
    static void Main(string[] args)
    {
      MyCar car = 5;
            for( int i = 0; i < 20; ++i )
            {
                car++;
                Console.WriteLine( "{0}", car.GetSpeed());
            }
    }
  }
  public class MyCar
  {
        private static int speed = 2;
        private const int maxSpeed = 200;
        
        public bool ChangeSpeed(int newSpeed)
        {
            if( newSpeed > maxSpeed )
                return false;
            speed = newSpeed;
            return true;
        }
        public static MyCar operator ++( MyCar car )
        {
            car.ChangeSpeed(++speed);
            return car;
        }
        public static implicit operator MyCar( int from )
        {
            MyCar car = new MyCar();
            car.ChangeSpeed( from );
            return car;
        }
        public int GetSpeed()
        {
            return speed;
        }
  }

postfix decrement

class MainClass
{
  public static void Main()
  {
    int length = 3;
    int newLength = length--;
    System.Console.WriteLine("Postfix decrement example");
    System.Console.WriteLine("length = " + length);
    System.Console.WriteLine("newLength = " + newLength);
  }
}
Postfix decrement example
length = 2
newLength = 3

postfix increment

class MainClass
{
  public static void Main()
  {
    int length = 3;
    int newLength = length++;
    System.Console.WriteLine("Postfix increment example");
    System.Console.WriteLine("length = " + length);
    System.Console.WriteLine("newLength = " + newLength);
  }
}
Postfix increment example
length = 4
newLength = 3

Pre And Post Increment

using System;
class MainClass
{
   static void Main()
   {
      int x = 5, y;
      y = x++;                        
      Console.WriteLine("y: {0}, x: {1}", y, x);
      x = 5;
      y = ++x;                        
      Console.WriteLine("y: {0}, x: {1}", y, x);
      x = 5;
      y = x--;                        
      Console.WriteLine("y: {0}, x: {1}", y, x);
      x = 5;
      y = --x;                        
      Console.WriteLine("y: {0}, x: {1}", y, x);
   }
}
y: 5, x: 6
y: 6, x: 6
y: 5, x: 4
y: 4, x: 4

prefix decrement

class MainClass
{
  public static void Main()
  {
    
    int length = 3;
    int newLength = --length;
    System.Console.WriteLine("Prefix decrement example");
    System.Console.WriteLine("length = " + length);
    System.Console.WriteLine("newLength = " + newLength);
  }
}
Prefix decrement example
length = 2
newLength = 2

prefix increment

class MainClass
{
  public static void Main()
  {
    int length = 3;
    int newLength = ++length;
    System.Console.WriteLine("Prefix increment example");
    System.Console.WriteLine("length = " + length);
    System.Console.WriteLine("newLength = " + newLength);
  }
}
Prefix increment example
length = 4
newLength = 4

The difference between prefix and postfix forms of ++

using System; 
 
class Example {  
  public static void Main() {    
    int x, y; 
    int i; 
 
    x = 1; 
    Console.WriteLine("Series generated using y = x + x++;"); 
    for(i = 0; i < 10; i++) { 
      y = x + x++; // postfix ++ 
      Console.WriteLine(y + " "); 
    } 
    Console.WriteLine(); 
 
    x = 1; 
    Console.WriteLine("Series generated using y = x + ++x;"); 
    for(i = 0; i < 10; i++) { 
      y = x + ++x; // prefix ++ 
      Console.WriteLine(y + " "); 
    } 
    Console.WriteLine(); 
    
  } 
}
Series generated using y = x + x++;
2
4
6
8
10
12
14
16
18
20
Series generated using y = x + ++x;
3
5
7
9
11
13
15
17
19
21

Using the Post-Increment Operator

class MainClass
{
  static void Main()
  {
    int count;
    int result;
    count = 0;
    result = count++; 
    System.Console.WriteLine("result = {0} and count = {1}",result, count);
  }
}

Using the Pre-Increment Operator

class MainClass
{
  static void Main()
  {
    int count;
    int result;
    count = 0;
    result = ++count;
    System.Console.WriteLine("result = {0} and count = {1}", result, count);
  }
}