top
upGrad KnowledgeHut SkillFest Sale!

Search

C# Tutorial

Operators are used to perform mathematical or logical manipulations in a program. There are many types of built-in operators in C#. Some of these are the following: Arithmetic Operators, Relational Operators, Logical Operators, Bitwise Operators, etc.Details about all these operators are given as follows:Arithmetic OperatorsArithmetic operators are used to perform arithmetic operations such as addition, subtraction, multiplication, division etc.A table that shows all the arithmetic operators in C# is as follows:Table: Arithmetic Operators in C#Operator NameOperatorOperator DescriptionAddition Operator+This operator adds two operandsSubtraction Operator-This operator subtracts the second operand from the firstMultiplication Operator*This operator multiplies two operandsDivision Operator/This operator divides the numerator by the denominatorModulus Operator%This operator provides the remainder of a division operation.Increment Operator++This operator increments the integer value by one.Decrement Operator--This operator decrements the integer value by one.A program that demonstrates the arithmetic operators in C# is as follows:Source Code: Program to implement arithmetic operators in C#using System; namespace ArithmeticOperators {  public class Example {    public static void Main(string[] args) {      int a, b;      a = 10;      b = 5;      Console.WriteLine("Arithmetic Operators in C#\n");      Console.WriteLine("Value of a: {0} ", a);      Console.WriteLine("Value of b: {0} \n", b);      // performing operations      Console.WriteLine("Addition of a and b: {0} ", a + b);      Console.WriteLine("Subtraction of a and b: {0} ", a - b);      Console.WriteLine("Multiplication of a and b: {0} ", a * b);      Console.WriteLine("Division of a and b: {0} ", a / b);      Console.WriteLine("Modulus of a and b: {0} ", a % b);      Console.WriteLine("Increment of a: {0} ", ++a);      Console.WriteLine("Decrement of b: {0} ", --b);    }  } }The output of the above program is as follows:Arithmetic Operators in C# Value of a: 10 Value of b: 5 Addition of a and b: 15 Subtraction of a and b: 5 Multiplication of a and b: 50 Division of a and b: 2 Modulus of a and b: 0 Increment of a: 11 Decrement of b: 4Relational OperatorsRelational Operators are used for comparison purposes. This includes finding if a variable is greater than another variable, lesser than another variable etc.A table that shows all the relational operators in C# is as follows:Table: Relational Operators in C#Operator NameOperatorOperator DescriptionEquality Comparison Operator==This operator checks if the values of the two operands are equal. If they are, then the condition is true.Inequality Operator!=This operator checks if the values of two operands are not equal. If they are not, then the condition is true.Greater than Operator>This operator checks if the value of the left operator is greater than the value of right operator. If it is, then the condition is true.Less than Operator<This operator checks if the value of the left operator is lesser than the value of right operator. If it is, then the condition is true.Greater than Equal to Operator>=This operator checks if the value of the left operator is greater than or equal to the value of right operator. If it is, then the condition is true.Less than Equal to Operator<=This operator checks if the value of the left operator is lesser than or equal to the value of right operator. If it is, then the condition is true.A program that demonstrates the relational operators in C# is as follows:Source Code: Program to implement relational operators in C#using System; namespace RelationalOperators { public class Example {  public static void Main(string[] args) {   int a, b;   a = 5;   b = 10;   Console.WriteLine("Relational Operators in C#\n");   if (a == b)    Console.WriteLine("{0} and {1} are equal", a, b);   else if (a != b)    Console.WriteLine("{0} and {1} are not equal", a, b);   if (a > b)    Console.WriteLine("{0} is greater than {1}", a, b);   else if (a < b)    Console.WriteLine("{0} is less than {1}", a, b);   if (a >= b)    Console.WriteLine("{0} is greater than or equal to {1}", a, b);   else if (a <= b)    Console.WriteLine("{0} is less than or equal to {1}", a, b);  } } }The output of the above program is as follows:Relational Operators in C# 5 and 10 are not equal 5 is less than 10 5 is less than or equal to 10Logical OperatorsThe logical operators are used to perform specific logical operations in c#. There are three logical operators namely, logical AND, logical OR and logical NOT.A table that shows all the logical operators in C# is as follows:Table: Logical Operators in C#Operator NameOperatorOperator DescriptionLogical AND&&If both the operands are non-zero then the condition evaluates to true otherwise it is false.Logical OR||If both the operands are zero then the condition evaluates to false otherwise it is true.Logical NOT!This makes the operand true if it is false and false if it is true.A program that demonstrates the logical operators in C# is as follows:Source Code: Program to implement logical operators in C#using System; namespace LogicalOperators {  public class Example {    public static void Main(string[] args) {      int a = 5, b = 10, c = 1, d = 0;      bool val = false;      Console.WriteLine("Logical Operators in C#\n");      if ((a > b) && (c > d))        Console.WriteLine("{0} is greater than {1} and {2} is greater than {3}", a, b, c, d);      if ((a > b) || (c > d))        Console.WriteLine("{0} is greater than {1} or {2} is greater than {3}", a, b, c, d);      if (!val)        Console.WriteLine("Value of d is false");      else        Console.WriteLine("Value of d is true");    }  } }The output of the above program is as follows:Logical Operators in C# 5 is greater than 10 or 1 is greater than 0 Value of d is falseBitwise OperatorsThe bitwise operators work at the level of bits and perform bit-by-bit operation. There are many bitwise operators such as bitwise AND, bitwise OR, bitwise XOR, bitwise NOT etc.A table that shows all the bitwise operators in C# is as follows:Table: Bitwise Operators in C#Operator NameOperatorOperator DescriptionBitwise AND&This performs AND operation at bit level and copies 1 into the result if it is in both the operands.Bitwise OR|This performs OR operation at bit level and copies 0 into the result if it is in both the operands.Bitwise XOR^This performs XOR operation at bit level and copies 1 into the result if it is in one of the operands only.Bitwise COMPLEMENT~This converts the 1 into 0 and 0 into 1 in the operand.Bitwise Left Shift Operator<<This shifts the left operand bits to the left by the value specified by the right operand.Bitwise Right Shift Operator>>This shifts the left operand bits to the right by the value specified by the right operand.A program that demonstrates the bitwise operators in C# is as follows:Source Code: Program to implement bitwise operators in C#using System; namespace BitwiseOperators { public class Example {  public static void Main(string[] args) {   int a = 4, b = 14;   Console.WriteLine("Bitwise Operators in C#\n");   // a=0100 b=1110, therefore result a&b=0100   Console.WriteLine("Bitwise AND of {0} and {1} is {2}", a, b, a & b);   // a=0100 b=1110, therefore result a|b=1110   Console.WriteLine("Bitwise OR of {0} and {1} is {2}", a, b, a | b);   // a=0100 b=1110, therefore result a^b=1010   Console.WriteLine("Bitwise XOR of {0} and {1} is {2}", a, b, a ^ b);   // a=0100, therefore result ~a=1011,   Console.WriteLine("Bitwise NOT of {0} is {1}", a, ~a);   // a=0100 a<<1=1000   Console.WriteLine("Bitwise Left Shift of {0} by 1 position is {1}", a, a << 1);   // a=0100 a>>1=0010   Console.WriteLine("Bitwise Right Shift of {0} by 1 position is {1}", a, a >> 1);  } } }The output of the above program is as follows:Bitwise Operators in C# Bitwise AND of 4 and 14 is 4 Bitwise OR of 4 and 14 is 14 Bitwise XOR of 4 and 14 is 10 Bitwise NOT of 4 is -5 Bitwise Left Shift of 4 by 1 position is 8 Bitwise Right Shift of 4 by 1 position is 2Assignment OperatorsThe assignment operators in C# are used to assign values to variables. There are various shorthand’s to assign values.A table that shows all the assignment operators in C# is as follows:Table: Assignment Operators in C#Operator NameOperatorOperator DescriptionAssignment Operator=This operator assigns the values from the right side to the left side operand.Addition Assignment Operator+=This operator adds the value of the right operand to left operand and stores it in the left operand.Subtraction Assignment Operator-=This operator subtracts the value of the right operand from the left operand and stores it in the left operand.Multiplication Assignment Operator*=This operator multiplies the value of the right operand to left operand and stores it in the left operand.Division Assignment Operator/=This operator divides the value of the right operand from the left operand and stores it in the left operand.Remainder Assignment Operator%=This operator uses modulus operator using two operands and the result is assigned to the left operand.A program that demonstrates the assignment operators in C# is as follows:Source Code: Program to implement assignment operators in C#using System; namespace AssignmentOperators { public class Example {  public static void Main(string[] args) {   int a = 10, b = 4;   Console.WriteLine("Assignment Operators in C#\n");   a += b;   Console.WriteLine("Value of a is {0}", a);   a -= b;   Console.WriteLine("Value of a is {0}", a);   a *= b;   Console.WriteLine("Value of a is {0}", a);   a /= b;   Console.WriteLine("Value of a is {0}", a);   a %= b;   Console.WriteLine("Value of a is {0}", a);  } } }The output of the above program is as follows:Assignment Operators in C# Value of a is 14 Value of a is 10 Value of a is 40 Value of a is 10 Value of a is 2Miscellaneous OperatorsThere are many miscellaneous operators in C# such as sizeof(), & (address specifier),* (pointer dereference), ?: (conditional expression) etc.A table that shows all the miscellaneous operators in C# is as follows:Table: Miscellaneous Operators in C#Operator NameOperatorOperator DescriptionSizeof Operatorsizeof()This operator returns the size of a data type.Reference Operator*This is used to create a pointer to a variable.Address Specifier&This operator returns the address of a variable.Conditional Expression?:This is used as a conditional statement.A program that demonstrates different miscellaneous operators in C# is as follows:Source Code: Program to implement miscellaneous operators in C#using System; namespace MiscellaneousOperators { public class Example {  public static void Main(string[] args) {   int a = 10, b = 4, c;   Console.WriteLine("Miscellaneous Operators in C#\n");   // Usage of sizeof()   Console.WriteLine("Size of int variable is {0}", sizeof(int));   // Usage of Conditional Expression   c = (a > b) ? a : b;   Console.WriteLine("{0} is greater between {1} and {2}", c, a, b);  } } }The output of the above program is as follows:Miscellaneous Operators in C# Size of int variable is 4 10 is greater between 10 and 4Operator Precedence in C#Operator precedence specifies the order in which operators will be evaluated. Some operators have more precedence than others have and are evaluated first. In the table given below, the operators with the higher precedence are given at the top and the precedence decreases further down the table.Table: Operator Precedence in C#CategoryOperatorAssociativityPostfix() [] -> .Left to RightUnary! ~ ++ -- sizeof() & * + -Right to LeftMultiplicative/  % *Left to RightAdditive- +Left to RightShift<< >>Left to RightRelational< <= > >=Left to RightEquality== !=Left to RightBitwise AND&Left to RightBitwise xOR^Left to RightBitwise OR|Left to RightLogical AND&&Left to RightLogical OR||Left to RightConditional?:Right to LeftAssignment= += -= *= /= %=Right to LeftComma,Left to Right
logo

C# Tutorial

Operators in C#

Operators are used to perform mathematical or logical manipulations in a program. There are many types of built-in operators in C#. Some of these are the following: Arithmetic Operators, Relational Operators, Logical Operators, Bitwise Operators, etc.

Details about all these operators are given as follows:

Arithmetic Operators

Arithmetic operators are used to perform arithmetic operations such as addition, subtraction, multiplication, division etc.

A table that shows all the arithmetic operators in C# is as follows:

Table: Arithmetic Operators in C#

Operator NameOperatorOperator Description
Addition Operator+This operator adds two operands
Subtraction Operator-This operator subtracts the second operand from the first
Multiplication Operator*This operator multiplies two operands
Division Operator/This operator divides the numerator by the denominator
Modulus Operator%This operator provides the remainder of a division operation.
Increment Operator++This operator increments the integer value by one.
Decrement Operator--This operator decrements the integer value by one.

A program that demonstrates the arithmetic operators in C# is as follows:

Source Code: Program to implement arithmetic operators in C#

using System;
namespace ArithmeticOperators {
 public class Example {
   public static void Main(string[] args) {
     int a, b;
     a = 10;
     b = 5;
     Console.WriteLine("Arithmetic Operators in C#\n");
     Console.WriteLine("Value of a: {0} ", a);
     Console.WriteLine("Value of b: {0} \n", b);
     // performing operations
     Console.WriteLine("Addition of a and b: {0} ", a + b);
     Console.WriteLine("Subtraction of a and b: {0} ", a - b);
     Console.WriteLine("Multiplication of a and b: {0} ", a * b);
     Console.WriteLine("Division of a and b: {0} ", a / b);
     Console.WriteLine("Modulus of a and b: {0} ", a % b);
     Console.WriteLine("Increment of a: {0} ", ++a);
     Console.WriteLine("Decrement of b: {0} ", --b);
   }
 }
}

The output of the above program is as follows:

Arithmetic Operators in C#
Value of a: 10
Value of b: 5
Addition of a and b: 15
Subtraction of a and b: 5
Multiplication of a and b: 50
Division of a and b: 2
Modulus of a and b: 0
Increment of a: 11
Decrement of b: 4

Relational Operators

Relational Operators are used for comparison purposes. This includes finding if a variable is greater than another variable, lesser than another variable etc.

A table that shows all the relational operators in C# is as follows:

Table: Relational Operators in C#

Operator NameOperatorOperator Description
Equality Comparison Operator==This operator checks if the values of the two operands are equal. If they are, then the condition is true.
Inequality Operator!=This operator checks if the values of two operands are not equal. If they are not, then the condition is true.
Greater than Operator>This operator checks if the value of the left operator is greater than the value of right operator. If it is, then the condition is true.
Less than Operator<This operator checks if the value of the left operator is lesser than the value of right operator. If it is, then the condition is true.
Greater than Equal to Operator>=This operator checks if the value of the left operator is greater than or equal to the value of right operator. If it is, then the condition is true.
Less than Equal to Operator<=This operator checks if the value of the left operator is lesser than or equal to the value of right operator. If it is, then the condition is true.

A program that demonstrates the relational operators in C# is as follows:

Source Code: Program to implement relational operators in C#

using System;
namespace RelationalOperators {
public class Example {
 public static void Main(string[] args) {
  int a, b;
  a = 5;
  b = 10;
  Console.WriteLine("Relational Operators in C#\n");
  if (a == b)
   Console.WriteLine("{0} and {1} are equal", a, b);
  else if (a != b)
   Console.WriteLine("{0} and {1} are not equal", a, b);
  if (a > b)
   Console.WriteLine("{0} is greater than {1}", a, b);
  else if (a < b)
   Console.WriteLine("{0} is less than {1}", a, b);
  if (a >= b)
   Console.WriteLine("{0} is greater than or equal to {1}", a, b);
  else if (a <= b)
   Console.WriteLine("{0} is less than or equal to {1}", a, b);
 }
}
}

The output of the above program is as follows:

Relational Operators in C#
5 and 10 are not equal
5 is less than 10
5 is less than or equal to 10

Logical Operators

The logical operators are used to perform specific logical operations in c#. There are three logical operators namely, logical AND, logical OR and logical NOT.

A table that shows all the logical operators in C# is as follows:

Table: Logical Operators in C#

Operator NameOperatorOperator Description
Logical AND&&If both the operands are non-zero then the condition evaluates to true otherwise it is false.
Logical OR||If both the operands are zero then the condition evaluates to false otherwise it is true.
Logical NOT!This makes the operand true if it is false and false if it is true.

A program that demonstrates the logical operators in C# is as follows:

Source Code: Program to implement logical operators in C#

using System;
namespace LogicalOperators {
 public class Example {
   public static void Main(string[] args) {
     int a = 5, b = 10, c = 1, d = 0;
     bool val = false;
     Console.WriteLine("Logical Operators in C#\n");
     if ((a > b) && (c > d))
       Console.WriteLine("{0} is greater than {1} and {2} is greater than {3}", a, b, c, d);
     if ((a > b) || (c > d))
       Console.WriteLine("{0} is greater than {1} or {2} is greater than {3}", a, b, c, d);
     if (!val)
       Console.WriteLine("Value of d is false");
     else
       Console.WriteLine("Value of d is true");
   }
 }
}

The output of the above program is as follows:

Logical Operators in C#
5 is greater than 10 or 1 is greater than 0
Value of d is false

Bitwise Operators

The bitwise operators work at the level of bits and perform bit-by-bit operation. There are many bitwise operators such as bitwise AND, bitwise OR, bitwise XOR, bitwise NOT etc.

A table that shows all the bitwise operators in C# is as follows:

Table: Bitwise Operators in C#

Operator NameOperatorOperator Description
Bitwise AND&This performs AND operation at bit level and copies 1 into the result if it is in both the operands.
Bitwise OR|This performs OR operation at bit level and copies 0 into the result if it is in both the operands.
Bitwise XOR^This performs XOR operation at bit level and copies 1 into the result if it is in one of the operands only.
Bitwise COMPLEMENT~This converts the 1 into 0 and 0 into 1 in the operand.
Bitwise Left Shift Operator<<This shifts the left operand bits to the left by the value specified by the right operand.
Bitwise Right Shift Operator>>This shifts the left operand bits to the right by the value specified by the right operand.

A program that demonstrates the bitwise operators in C# is as follows:

Source Code: Program to implement bitwise operators in C#

using System;
namespace BitwiseOperators {
public class Example {
 public static void Main(string[] args) {
  int a = 4, b = 14;
  Console.WriteLine("Bitwise Operators in C#\n");
  // a=0100 b=1110, therefore result a&b=0100
  Console.WriteLine("Bitwise AND of {0} and {1} is {2}", a, b, a & b);
  // a=0100 b=1110, therefore result a|b=1110
  Console.WriteLine("Bitwise OR of {0} and {1} is {2}", a, b, a | b);
  // a=0100 b=1110, therefore result a^b=1010
  Console.WriteLine("Bitwise XOR of {0} and {1} is {2}", a, b, a ^ b);
  // a=0100, therefore result ~a=1011,
  Console.WriteLine("Bitwise NOT of {0} is {1}", a, ~a);
  // a=0100 a<<1=1000
  Console.WriteLine("Bitwise Left Shift of {0} by 1 position is {1}", a, a << 1);
  // a=0100 a>>1=0010
  Console.WriteLine("Bitwise Right Shift of {0} by 1 position is {1}", a, a >> 1);
 }
}
}

The output of the above program is as follows:

Bitwise Operators in C#
Bitwise AND of 4 and 14 is 4
Bitwise OR of 4 and 14 is 14
Bitwise XOR of 4 and 14 is 10
Bitwise NOT of 4 is -5
Bitwise Left Shift of 4 by 1 position is 8
Bitwise Right Shift of 4 by 1 position is 2

Assignment Operators

The assignment operators in C# are used to assign values to variables. There are various shorthand’s to assign values.

A table that shows all the assignment operators in C# is as follows:

Table: Assignment Operators in C#

Operator NameOperatorOperator Description
Assignment Operator=This operator assigns the values from the right side to the left side operand.
Addition Assignment Operator+=This operator adds the value of the right operand to left operand and stores it in the left operand.
Subtraction Assignment Operator-=This operator subtracts the value of the right operand from the left operand and stores it in the left operand.
Multiplication Assignment Operator*=This operator multiplies the value of the right operand to left operand and stores it in the left operand.
Division Assignment Operator/=This operator divides the value of the right operand from the left operand and stores it in the left operand.
Remainder Assignment Operator%=This operator uses modulus operator using two operands and the result is assigned to the left operand.

A program that demonstrates the assignment operators in C# is as follows:

Source Code: Program to implement assignment operators in C#

using System;
namespace AssignmentOperators {
public class Example {
 public static void Main(string[] args) {
  int a = 10, b = 4;
  Console.WriteLine("Assignment Operators in C#\n");
  a += b;
  Console.WriteLine("Value of a is {0}", a);
  a -= b;
  Console.WriteLine("Value of a is {0}", a);
  a *= b;
  Console.WriteLine("Value of a is {0}", a);
  a /= b;
  Console.WriteLine("Value of a is {0}", a);
  a %= b;
  Console.WriteLine("Value of a is {0}", a);
 }
}
}

The output of the above program is as follows:

Assignment Operators in C#
Value of a is 14
Value of a is 10
Value of a is 40
Value of a is 10
Value of a is 2

Miscellaneous Operators

There are many miscellaneous operators in C# such as sizeof(), & (address specifier),* (pointer dereference), ?: (conditional expression) etc.

A table that shows all the miscellaneous operators in C# is as follows:

Table: Miscellaneous Operators in C#

Operator NameOperatorOperator Description
Sizeof Operatorsizeof()This operator returns the size of a data type.
Reference Operator*This is used to create a pointer to a variable.
Address Specifier&This operator returns the address of a variable.
Conditional Expression?:This is used as a conditional statement.

A program that demonstrates different miscellaneous operators in C# is as follows:

Source Code: Program to implement miscellaneous operators in C#

using System;
namespace MiscellaneousOperators {
public class Example {
 public static void Main(string[] args) {
  int a = 10, b = 4, c;
  Console.WriteLine("Miscellaneous Operators in C#\n");
  // Usage of sizeof()
  Console.WriteLine("Size of int variable is {0}", sizeof(int));
  // Usage of Conditional Expression
  c = (a > b) ? a : b;
  Console.WriteLine("{0} is greater between {1} and {2}", c, a, b);
 }
}
}

The output of the above program is as follows:

Miscellaneous Operators in C#
Size of int variable is 4
10 is greater between 10 and 4

Operator Precedence in C#

Operator precedence specifies the order in which operators will be evaluated. Some operators have more precedence than others have and are evaluated first. In the table given below, the operators with the higher precedence are given at the top and the precedence decreases further down the table.

Table: Operator Precedence in C#

CategoryOperatorAssociativity
Postfix() [] -> .Left to Right
Unary! ~ ++ -- sizeof() & * + -Right to Left
Multiplicative/  % *Left to Right
Additive- +Left to Right
Shift<< >>Left to Right
Relational< <= > >=Left to Right
Equality== !=Left to Right
Bitwise AND&Left to Right
Bitwise xOR^Left to Right
Bitwise OR|Left to Right
Logical AND&&Left to Right
Logical OR||Left to Right
Conditional?:Right to Left
Assignment= += -= *= /= %=Right to Left
Comma,Left to Right

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments

austin faith

Avery good write-up. Please let me know what are the types of C# libraries used for AI development.

kariya arti

very satisfied!!

jean-Francois Michaud

Good tutorial. Small question: Say, there is : enum numbers { one, two, three} and a string field_enum ="one" how would I from the variable field_enum have a response with value numbers.one so that it can be treated as an enum and not as a string. making a list from the enum, and loop into the list. is not elegant... and may not work is forced value on field is forced ( one = 100).

Kshitiz

Hi Team Knowledge Hut, Thank you for such an informative post like this. I am completely new to this digital marketing field and do not have much idea about this, but your post has become a supportive pillar for me. After reading the blog I would expect to read more about the topic. I wish to get connected with you always to have updates on these sorts of ideas. Regards, Kshitiz

Ed

The reason abstraction can be used with this example is because, the triangle, circle. Square etc can be defined as a shape, for example.....shape c = new circle(5,0)...the abstract object c now points at the circle class. Thus hiding implementation

Suggested Tutorials

Swift Tutorial

Introduction to Swift Tutorial
Swift Tutorial

Introduction to Swift Tutorial

Read More

R Programming Tutorial

R Programming

Python Tutorial

Python Tutorial