C# Tutorial C# Advanced C# Resources
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

C# - decrement operator



The decrement (--) is an unary operator in C# and hence acts upon a single operand to produce a new value. It has two variant:

  • Pre-decrement: Decreases the value of the operand by 1, then returns the operand.
  • Post-decrement: Returns the operand, then decreases the value of the operand by 1.

Example: Pre-decrement operator

The example below describes the usage of pre-decrement operator.

using System;

class MyProgram {
  static void Main(string[] args) {
    int x = 10;
    int y = 20;
    int z;

    //below expression is equivalent to
    //x = x - 1; z = x + y;
    z = --x + y;    

    //Displaying the result
    Console.WriteLine("x = "+ x);
    Console.WriteLine("y = "+ y);
    Console.WriteLine("z = "+ z);
  }
}

The output of the above code will be:

x = 9
y = 20
z = 29

Example: Post-decrement operator

The example below describes the usage of post-decrement operator.

using System;

class MyProgram {
  static void Main(string[] args) {
    int x = 10;
    int y = 20;
    int z;

    //below expression is equivalent to
    //z = x + y; x = x - 1; 
    z = x-- + y;    

    //Displaying the result
    Console.WriteLine("x = "+ x);
    Console.WriteLine("y = "+ y);
    Console.WriteLine("z = "+ z);
  }
}

The output of the above code will be:

x = 9
y = 20
z = 30

❮ C# - Operators

5