Unary Operators in C#

In this article explain I will explain how to perform the unary operators in C#.
  • 3434

Introduction

The concept of unary operators are very useful in C#. When we are writing a program, you need unary operator to increment or decrement value. Unary operator is mostly used with loop constructs to increment or decrement loop by value 1. The detail information of unary Operators is given below

Unary  Operators

++ Increment Operator

Increment operator is used for incrementing value by 1. It is used in C# programming by two types

Pre-increment (++i)

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

Post-increment (i++)

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

Example of Post-increment (i++)

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Increment_Operator

{

    class Program

    {

        static void Main(string[] args)

        {

            int i = 0; // initialization

            i++; // i incremented by one. It is post increment

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

            Console.ReadLine();

        }

    }

}

 

The output of following program

Clipboard177.jpg

Example of Pre-increment (i++)

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Increment_Operator

{

    class Program

    {

        static void Main(string[] args)

        {

            int i = 0; // initialization

            ++i;  // i incremented by one. It is pre increment

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

            Console.ReadLine();

        }

    }

}

 

The output of following program


Clipboard177.jpg

© 2020 DotNetHeaven. All rights reserved.