C# Unary Operators

C# Unary Operators

The C# unary operator is widely used for increment or decrement value by 1.

This operator widely used with loop constructs to increment loop by 1.

Another useful shortcut can be achieved using the C# increment and decrement operators. As with the compound assignment operators described in the previous chapter, consider the following C# code fragment:

x = x + 1; // Increase value of variable x by 1

x = x - 1; // Decrease value of variable y by 1

These expressions increment and decrement the value of x by 1. Instead of using this approach it is quicker to use the ++ and -- operators. 

The following examples perform exactly the same tasks as the examples above:

x++; Increment x by 1

x--; Decrement x by 1

These operators can be placed either before or after the variable name.

*** If the operator is placed before the variable name the increment or decrement is performed before any other operations are performed on the variable.

For example: in the following example, x is incremented before it is assigned to y, leaving y with a value of 10:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 9;
            int y;

            y = ++x;
            Console.WriteLine(y); // y = 10
            Console.ReadLine();
        }
    }
}

In the following example, the value of x (9) is assigned to variable y before the decrement is performed. After the expression is evaluated the value of y will be 9 and the value of x will be 8.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 9;
            int y;

            y = x--;
            Console.WriteLine(y); // y= 9
            Console.ReadLine();
        }
    }
}

 

++ increment operator

++ : This operator is pronounced as increment operator

It is used for incrementing value by 1.

 It is used in C# programming by two types: 

  • Pre-increment (++i) and 
  • Post-increment (i++). 

In pre-increment, first it increments by 1 then loop executes whereas.

In Post-increment, the loop executes then it increments by 1.

 

-- decrement operator

-- : This operator is pronounced as decrement operator

The behavior of decrement operator is just opposite from increment operator. 

It is used for decrementing the value by one. 

It has also two types: 

  • Pre-Decrement (--i) and 
  • Post Decrement (i--). 

In pre-decrement the value is decremented by 1 then loop executes whereas.

In post-decrement the loop executed then the value decrements by 1.

 

C# Unary Operators Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            // ++ increment operator
            int i = 9; // initialization
            Console.WriteLine("++ increment operator (post increment):");

            Console.WriteLine("The value of i is: {0}", i);

            i++; // i incremented by one => It is post increment

            Console.WriteLine("The value of i++ is: {0}", i);

            Console.WriteLine("\n-- increment operator (post-decrement):");

            int j = 9; // Initialization
            Console.WriteLine("The Value of j is: {0}", j);

            j--; // i decremented by one => It is post-decrement

            Console.WriteLine("The value of j-- is: {0}", j);
           
            Console.ReadLine();
        }
    }
}

Output:

C# unary operator example