Please note, this is a STATIC archive of website technosmarter.com from 20 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
 

C# operators with examples


Operators are mostly used in every programming language to create logic. If you are a beginner or you have already done C programming language then it will be easy to understand. In C programming, we already learned about Operators. In this tutorial, we are going to learn operators in C# programming.

C# Operators -


An operator looks like a symbol. The operators are used in a different type of operations. Operators place before or between operands. Operands like - a , b, c,x,y,z, etc.

Let's have a simple example to understand operators and operands.

a>b     a and b are two operands and > greater than is an operator.

x<=y     x and y are two operands and <= less than equals in another operator.

This is the basic of operands and operators in C# programming.

There are many operators in C# programming like arithmetic, logical, relational operator, etc.

List of operators in C# language which are used for different operations.


Arithmetic Operators


Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Increment and Decrement Operators
Unary Operators
Ternary Operators
Misc Operators

C# Arithmetic operators -

The arithmetic operators are used to addition, subtraction, multiplication, division etc on numerical values (constants and variables).

Table of arithmetic operators -

OperatorMeaning of Operator
+ addition or unary plus
- subtraction or unary minus
* multiplication
/ division
% remainder after division (modulo division)

Let's understand the arithmetic operators in C# language.We are going to create an example with all arithmetic operators in C# programming.

Let's have a look.


using System;

namespace OperatorsApplication {
   
   class Program {

      static void Main(string[] args) {
                
      int x=9; 
      int y=8; 
      int sum; 
       int subt; 
        int mul; 
         int dev; 
          int mod; 
      
     /*Addition + operator */ 
     sum=x+y; 
      /*Subtraction - operator */ 
     subt=x-y; 
     
       /*Multiplication * operator */ 
      mul=x*y; 
       
       /*division/ operator */ 
       dev=x/y; 
       /*Modulo,reminder % operator */ 
       mod=x%y; 
       
        Console.WriteLine(sum);
            Console.WriteLine(subt);
             Console.WriteLine(mul);
               Console.WriteLine(dev);
                  Console.WriteLine(mod);
                  Console.ReadLine();
      }
   }
}

In the above example, we created mathematics operations using arithmetic operations in C# language. Compile and execute this example.

Output

17
1
72
1
1


C# Relational Operators-

OperatorMeaning of OperatorExample
== Equal to 7 == 2 is evaluated to 0, false
> Greater than 7 > 2 is evaluated to 1, true
< Less than 7 < 2 is evaluated to 0, false
!= Not equal to 7 != 2 is evaluated to 1, true
>= Greater than or equal to 7 >= 2 is evaluated to 1, true
<= Less than or equal to 7 <= 2 is evaluated to 0, false


The relational operators are used to creating relations between two operands. In other words, the relation operator checks the relationship between two operands like a>b, etc. If the relation is true, it returns 1 and if the relation is false, it returns false. In C# language, we used relational operators in decision making and loops. The below operators table will help you understand the relational operators.


Let's create an example using these relation operators and if-else condition in C# programming language.

using System;      
public class RelationalOperatorsExample  
    {  
       public static void Main(string[] args)  
        {  
            int a = 24; 
            int b=9; 
                /*Relational operator == equals to*/
            if (a==b)  
            {  
                Console.WriteLine("a is equal to b");  
            }  
              else{
                  Console.WriteLine("a is not equal to b");  
                  
              
              }
              
              /*Relational operator > greter than */
               if (a>b)  
            {  
                Console.WriteLine("a is greater than  b");  
            }  
              else{
                  Console.WriteLine("a is not greater than b");  
                  
              
              }
               /*Relational operator < less than */
               if (a=b)  
            {  
                Console.WriteLine("a is greater than or equal to b");  
            }  
              else{
                  Console.WriteLine("a is not greater than or equal to b");  
                  
              
              }
               /*Relational operator <=	Less than or equal to  */
               if (a<=b)  
            {  
                Console.WriteLine("a is Less than or equal to b");  
            }  
              else{
                  Console.WriteLine("a is not Less than or equal to b");  
                  
              
              }
              
        }  
    } 

Compile and execute the example.

Output

 
Output - 
a is not equal to b
a is greater than  b
a is not less than b
a is not equal to b
a is greater than or equal to b
a is not Less than or equal to b


In the above example, we used relational operators using C# language.

If the condition is true to execute the if block of code and if the condition is false, execute the else block of code.


C# Logical Operators-



Logical operators are used to making an expression more effectively. In C# programming, local operators are used in decision making. An expression can contain logical operators and returns either 1 or 0 . 1 state denotes to true and 0 state denotes to false. If the condition is true, it means returned value 1 else for false 0.

OperatorMeaning
&& Logical AND. True only if all operands are true
|| Logical OR. True only if either one operand is true
! Logical NOT. True only if the operand is 0

Let's create an example using all logical operators and if-else condition.


using System;      
public class LogicalOperatorsExample  
    {  
       public static void Main(string[] args)  
        {  
            int a = 10, b = 5, c = 10;
            /*logical operator && AND */
      if(a==b&&a==c){
          
       Console.WriteLine("true , 1");  /*if the condition is true*/
       
      }
      else{ 
          Console.WriteLine("false , 0");  /*if the condition is false*/
        
      }
      /*logical operator || OR */
         
      if(a==b||a==c){
           
       Console.WriteLine("true , 1");  
       
      }
      else{ 
          Console.WriteLine("false , 0");  
        
      }
        /*logical operator ! NOT */
      
      if(a!=c){
           
       Console.WriteLine("true , 1");    
       
      }
      else{ 
          Console.WriteLine("false , 0");  
        
      }
                    
                 Console.ReadLine();
        }  
    }  

In the above example, we used logical operators in C# programming.
Compile and execute the program on the console application. It gives the result as output.

false, 0
true, 1
false, 0

C# Bitwise Operators-


There are six types of bitwise operators in C# programming. In the arithmetic logic unit, mathematical operations like addition, subtraction, multiplication, and division are done in bit-level. In C# language, we used bitwise operators for bit-level operations.

The list of bitwise operators in C# language-

OperatorsMeaning of operators
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
<< Shift left
>>

Shift right


12 = 00001100 (In Binary)
25 = 00011001 (In Binary)

Bit Operation of 12 and 25
  00001100
& 00011001
  ________
  00001000  = 8 (In decimal)

using System;      
public class LogicalOperatorsExample  
    {  
       public static void Main(string[] args)  
        {  
       int a = 12, b = 25;
       Console.WriteLine(a&b);  /*if the condition is true*/
        }  
    }

Output

8


C# Assignment Operators-


In the C programming language, we learned assignment operators. In this tutorial, we are going to learn assignment operators in C# programming. Assignment operators are used to assigning a value to the variable. We mostly use one assignment operator = (equals to).

The list of assignment operators in C# language.

OperatorExample
= x = y
+= x += y
-= x-= y
*= x *= y
/= x /= y
%= x %= y

Let's create an example using these assignment operators in C sharp (C#) .


using System;      
public class AssignmentOperatorsExample  
    {  
       public static void Main(string[] args)  
        {  
       int x = 8, y;
       y=x; 
Console.WriteLine("Value of y= "+y);  
 
  y+=x; 
Console.WriteLine("Value of y= "+y);  
  y-=x; 
Console.WriteLine("Value of y= "+y); 
 y*=x; 
Console.WriteLine("Value of y= "+y); 
 y/=x; 
Console.WriteLine("Value of y= "+y); 
 
 y%=x; 
Console.WriteLine("Value of y= "+y);
 Console.ReadLine();

        } 

In the above example, we used assignment operators in C#.

It gives the output.

Value of y= 8
Value of y= 16
Value of y= 8
Value of y= 64
Value of y= 8
Value of y= 0


Increment and Decrement Operators in C#

There are two types of operators in cC# to decrement and increment the value by 1. The Increment operator is denoted by ++ and the decrement operator is denoted by -- operator. We used Increment and Decrement operators to increment and decrement values of variable and constants. The increment operator ++ increments the value by 1 and the decrement operator decrements the value by 1.

Let's create an example using Increment and Decrement operators in C# programming.


using System;      
public class IncrementDecrementOperatorsExample  
    {  
       public static void Main(string[] args)  
        {  
            /* ++ Post increment operator */
   int num=85; 
   num++; 
   int num2=num; 
   
Console.WriteLine(num2); 
        /* -- Post decrement operator */
int num3=85; 
   num3--; 
   int num4=num3; 
 Console.WriteLine(num4);  
     /* -- pre decrement operator */
 
 int num5=85; 
   --num5; 
 
   int num6=num5; 
 Console.WriteLine(num6);  
     /* ++ pre increment operator */
 int num7=85; 
   ++num7; 
 
   int num8=num7; 
 Console.WriteLine(num8);  

 Console.ReadLine();

        }  
    }

Output

 
86
84
84
86

In the above example, we used pre and post decrement, increment operators with C#.

C# Unary Operators-

Th two operators(++,--) are unary operators, meaning they only operate on a single operand. The unary operators only operate on a single operand. The above increment and decrement operators are unary operators in c# programming because the operate only on a single operand.

The unary operators are -

++ , --

C# Ternary Operators -

Ternary is known as the conditional operator. If any operator is used on three operands or variable is known as Ternary Operator. It can be represented with? :. It is also called a conditional operator.

Binary Operators- The binary operators operate on two operands. There are many binary operators in c# language.

The form of the binary operator-
The binary operators are categorized in five listed below -

1. Arithmetic Operators.
2. Relational Operators
3. Logical operators
4.Bitwise Operators.
5. Assignment operators.


We have discussed above these binary operators with C# language examples.


Please Share

Recommended Posts:-