C# Operators

Operators perform operations by taking one or more values, to give another value. These operations are performed on variables and values. Operators in C# are discussed here. For example:

a + b

The following are the operators in C#:

  • Arithmetic Operators
  • Assignment Operators
  • Relational Operators
  • Logical Operators

Let us understand the operators one by one:

C# Arithmetic Operators

Arithmetic operators in C# perform arithmetical operations, such as addition, subtraction, division, multiplication, etc.

C# Arithmetic Operators

Let us see an example of Arithmetic Operators in C#:

using System;

namespace Demo
{
  class Studyopedia
  {
    static void Main(string[] args)
    {
      int a, b, c;
      a = 50;
      b = 100;
      c = 150;
      
      Console.WriteLine("Arithmetic Operators in C#:\n");
   
      Console.WriteLine("Addition Arithmetic Operator");
      Console.WriteLine("Value of (a + b) is "+(a+b));

      Console.WriteLine("\nSubtract Arithmetic Operator");
      Console.WriteLine("Value of (b - a) is "+(b-a));

      Console.WriteLine("\nDivision Arithmetic operator");
      Console.WriteLine("Value of (c / a) is "+(c/a));

      Console.WriteLine("\nMultiplication Arithmetic operator");
      Console.WriteLine("Value of (a * b) is "+(a*b));

      Console.WriteLine("\nModulus Arithmetic operator");
      Console.WriteLine("Value of (c % b) is "+(c%b));  
      
      Console.WriteLine("\nIncrement operator");
      Console.WriteLine("Value of (a++) is "+(a++));  
      
      Console.WriteLine("\nDecrement operator");
      Console.WriteLine("Value of (a--) is "+(a--));  
    }
  }
}

Output

Arithmetic Operators in C#:

Addition Arithmetic Operator
Value of (a + b) is 150

Subtract Arithmetic Operator
Value of (b - a) is 50

Division Arithmetic operator
Value of (c / a) is 3

Multiplication Arithmetic operator
Value of (a * b) is 5000

Modulus Arithmetic operator
Value of (c % b) is 50

Increment operator
Value of (a++) is 50

Decrement operator
Value of (a--) is 51

C# Assignment Operators

Use the Assignment Operator when you need to assign values to a variable. It also includes the Arithmetic Assignment Operators:

C# Assignment Operators

Let us see an example of Assignment Operators in C#:

using System;

namespace Demo
{
  class Studyopedia
  {
    static void Main(string[] args)
    {
      int a, b, c, d, e;
      a = 5;
      b = 10;
      c = 15;
      d = 20;
      e = 30;
      
      Console.WriteLine("Assignment Operators in C#:\n");
      
      c = a +b;
      Console.WriteLine("Assignment operator");
      Console.WriteLine("Value of c: = "+c);
      
      a += 5; 
      Console.WriteLine("\nAdd AND assignment operator");
      Console.WriteLine("Value of a: = "+a);

      b -= 5;
      Console.WriteLine("\nSubtract AND assignment operator");
      Console.WriteLine("Value of b: = "+b);

      c *= 5;
      Console.WriteLine("\nMultiple AND assignment operator");
      Console.WriteLine("Value of c: = "+c);

      d /= 10;
      Console.WriteLine("\nDivide AND assignment operator");
      Console.WriteLine("Value of d: = "+d);

      e %= 9;
      Console.WriteLine("\nModulus AND assignment operator");
      Console.WriteLine("Value of e: = "+e);  
    }
  }
}

Output

Assignment Operators in C#:

Assignment operator
Value of c: = 15

Add AND assignment operator
Value of a: = 10

Subtract AND assignment operator
Value of b: = 5

Multiple AND assignment operator
Value of c: = 75

Divide AND assignment operator
Value of d: = 2

Modulus AND assignment operator
Value of e: = 3

C# Relational Operators

Compare two values with C# relational operators, which are also known as Comparison Operators. Let us say a = 3, b =5;

C# Relational Operators

Let us see an example of Relational Operators in C#:

using System;

namespace Demo
{
  class Studyopedia
  {
    static void Main(string[] args)
    {
      int a, b, c, d;
      a = 5;
      b = 10;
      c = 15;
      
      Console.WriteLine("Relational Operators in C#:\n");
         
      Console.WriteLine("Value of (a == b) is " + (a == b) );
      Console.WriteLine("Value of (a != b) is " + (a != b) );
      Console.WriteLine("\nValue of (b > c) is " + (b > c) );
      Console.WriteLine("Value of (c < a) is " + (c < a) );
      Console.WriteLine("\nValue of (c >= b) is " + (c >= b) );
      Console.WriteLine("Value of (b <= c) is " + (b <= c) );
    }
  }
}

Output

Relational Operators in C#:

Value of (a == b) is False
Value of (a != b) is True

Value of (b > c) is False
Value of (c < a) is False

Value of (c >= b) is True
Value of (b <= c) is True

C# Logical Operators

Logical operators combine conditional statements. Considering Boolean variables, a and b,

C# Logical Operators

Let us see an example of Logical Operators in C#:

using System;

namespace Demo
{
  class Studyopedia
  {
    static void Main(string[] args)
    {         
      bool a, b, c, d;
      a = true;
      b = false;
      c = true;
      d = false;
     
      Console.WriteLine("\nRelational Operators in C#:");
   
      Console.WriteLine("Value of (a && b) = " + (a&&b));
      Console.WriteLine("Value of (b || c) = " + (b||c) );
      Console.WriteLine("Value of !(a && d) = " + !(a && d));
    }
  }
}

Output

Relational Operators in C#:
Value of (a && b) = False
Value of (b || c) = True
Value of !(a && d) = True
C# Data Types
C# - Installation and Setup
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment