What are postfix operators in C#?

The postfix operators in C# are the increment (++) and decrement (--) operators that are placed after the variable. With postfix operators, the current value of the variable is returned first, and then the variable is incremented or decremented by 1.

Syntax

Following is the syntax for postfix increment and decrement operators −

variable++;  // postfix increment
variable--;  // postfix decrement

How Postfix Operators Work

The key behavior of postfix operators is that they return the original value before performing the increment or decrement operation. This is different from prefix operators which perform the operation first and then return the new value.

Postfix vs Prefix Operators Postfix (a++) 1. Return current value 2. Increment variable a=5 ? returns 5, a=6 Prefix (++a) 1. Increment variable 2. Return new value a=5 ? a=6, returns 6

Using Postfix Increment Operator

Example

using System;

class Program {
    static void Main() {
        int a, b;

        a = 10;
        Console.WriteLine("Original value: " + a);
        Console.WriteLine("Postfix a++: " + a++);
        Console.WriteLine("Value after increment: " + a);

        b = a;
        Console.WriteLine("b = a: " + b);
    }
}

The output of the above code is −

Original value: 10
Postfix a++: 10
Value after increment: 11
b = a: 11

Using Postfix Decrement Operator

Example

using System;

class Program {
    static void Main() {
        int x = 15;
        int y;

        Console.WriteLine("Original value of x: " + x);
        y = x--;
        Console.WriteLine("y = x-- result: " + y);
        Console.WriteLine("x after decrement: " + x);
    }
}

The output of the above code is −

Original value of x: 15
y = x-- result: 15
x after decrement: 14

Common Use Cases

Postfix operators are commonly used in loops, array indexing, and situations where you need the current value before modification −

Example

using System;

class Program {
    static void Main() {
        int[] numbers = {10, 20, 30, 40, 50};
        int index = 0;

        Console.WriteLine("Using postfix in array access:");
        Console.WriteLine("numbers[" + index + "] = " + numbers[index++]);
        Console.WriteLine("Index after postfix: " + index);

        Console.WriteLine("\nUsing postfix in while loop:");
        int count = 3;
        while (count-- > 0) {
            Console.WriteLine("Count: " + count);
        }
    }
}

The output of the above code is −

Using postfix in array access:
numbers[0] = 10
Index after postfix: 1

Using postfix in while loop:
Count: 2
Count: 1
Count: 0

Conclusion

Postfix operators in C# (++ and --) return the current value first, then modify the variable. This behavior makes them useful in scenarios where you need the original value while also incrementing or decrementing the variable in the same statement.

Updated on: 2026-03-17T07:04:35+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements