News
IBM to offer mobile security as a service IBM will start delivering mobile security-as-a-service (MaaS) through its own cloud infrastructure in India, a move that is expected to better address in-country data requirements... IBM Spectrum Computing: IBM has enlarged its portfolio of software-defined infrastructure solutions with cognitive features for workload management.  * LiFi internet technology has been introduced, the new type of wireless internet connection that gives 100 times faster than traditional WiFi -- Invented by Professor Harald Haas from the University of Edinburgh. * Sci. Rachid Yazami has developed a smart chip that charges smartphones in less than 10 minutes. BenQ has launched BlueCore projector - Consumer electronics major BenQ has launched its first BlueCore laser light source projector. For those unaware, devices with BlueCore laser technology have a high contrast output of 80000:1 with an extended lamp life and efficiency. *** 
  Jan 3 2015 10:38PM     Mohammad Shaik
  0 Comments    1832 Views  
Here Mohammad Shaik provided a short tutorial with example, about Operators in C#.
What is Operator ?
  1. Operators are used for building expressions in C#.
  2. To calculate the value of variable or performs operation in variable you will have to make proper expression.
  3. These expressions are made using C# operators.
  4. C# provides wide range of operators as arithmetic operators, assignment operators, unary operators, comparison operator, logical operator etc.
  5. In this chapter, you will learn deeply about all the C# operators with programming examples.
Types of Operator:
  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Misc Operators
Arithmetic Operators
Arithmetic Operators are used for basic mathematical calculation in C# programming.
The list of Arithmetic Operators is given below:
OperatorDescriptionExample
+Adds two operands X + y will give 30
-Subtracts second operand from the first X - Y will give -10
*Multiplies both operands X * Y will give 200
/Divides numerator by de-numerator Y / X will give 2
%Modulus Operator and remainder of after an integer division Y % X will give 0
++Increment operator increases integer value by one X++ will give 11
--Decrement operator decreases integer value by one X-- will give 9
Example:

using System;

namespace MyApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 21;
            int y = 10;
            int z;

            z = x + y;
            Console.WriteLine("Line 1 - Value of z is {0}", z);
            z = x - y;
            Console.WriteLine("Line 2 - Value of z is {0}", z);
            z = x * y;
            Console.WriteLine("Line 3 - Value of z is {0}", z);
            z = x / y;
            Console.WriteLine("Line 4 - Value of z is {0}", z);
            z = x % y;
            Console.WriteLine("Line 5 - Value of z is {0}", z);
            z = x++;
            Console.WriteLine("Line 6 - Value of z is {0}", z);
            z = x--;
            Console.WriteLine("Line 7 - Value of z is {0}", z);
            Console.ReadLine();
        }
    }
}

When the above code is compiled and executed, it produces the following result:

Line 1 - Value of z is 31
Line 2 - Value of z is 11
Line 3 - Value of z is 210
Line 4 - Value of z is 2
Line 5 - Value of z is 1
Line 6 - Value of z is 21
Line 7 - Value of z is 22

Relational Operators
The C# relational operator is used to compare two operands. It returns true or false based on comparison.
The complete list of comparison operators are listed in a table.
OperatorDescriptionExample
== Checks if the values of two operands are equal or not, if yes then condition becomes true. (X == Y) is not true.
!= Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. (X != Y) is true.
> Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (X > Y) is not true.
< Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (X < Y) is true.
>= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (X >= Y) is not true.
<= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (X <= Y) is true.
Example:

class Program
 {
        static void Main(string[] args)
        {
            int x = 25;
            int y = 12;

            if (x == y)
            {
                Console.WriteLine("X is equal to Y");
            }
            else
            {
                Console.WriteLine("X is not equal to Y");
            }
            if (x < y)
            {
                Console.WriteLine("X is less than Y");
            }
            else
            {
                Console.WriteLine("X is not less than Y");
            }
            if (x > y)
            {
                Console.WriteLine("X is greater than Y");
            }
            else
            {
                Console.WriteLine("X is not greater than Y");
            }
            Console.ReadKey();
        }
   }

When the above code is compiled and executed, it produces the following result:

X is not equal to Y
X is not less than Y
X is greater than Y

Logical Operators
The C# Logical Operator also evaluates the values and returns true or false as output. Based on true-false the program behaves dynamically at run time. This operator is widely used with C# programming.

&& Operator :It is pronounced as and operator. It returns true if both or all the condition is true and return false if any of the condition is false.
OperatorDescriptionExample
&& Called Logical AND operator. If both the operands are non zero then condition becomes true. (X && Y) is false.
||Called Logical OR Operator. If any of the two operands is non zero then condition becomes true. (X || Y) is true.
!Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. !(X && Y) is true.
Example:

class Program
    {
        static void Main(string[] args)
        {
            bool x = true;
            bool y = true;

            if (x && y)
            {
                Console.WriteLine("Condition is true");
            }
            if (x || y)
            {
                Console.WriteLine("Condition is true");
            }            
            Console.ReadKey();
        }
    }

When the above code is compiled and executed, it produces the following result:

Condition is true
Condition is true

Bitwise Operators
Bitwise operator works on bits and perform bit by bit operation. The truth tables for &, |, and ^ are as follows:
pqp & qp | qp ^ q
00000
01011
11110
10011
Assignment Operators
The C# assignment operator is generally suffix with arithmetic operators. The symbol of c sharp assignment operator is "=" without quotes. The assignment operator widely used with C# programming.
OperatorDescriptionExample
=Simple assignment operator, Assigns values from right side operands to left side operand Z = X + Y will assign value of X + Y into Z
+=Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand Z += X is equivalent to Z = Z + X
-=Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand Z -= X is equivalent to Z = Z - X
*=Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand Z *= X is equivalent to Z = Z * X
/=Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand Z /= X is equivalent to Z = Z / A
%=Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand Z %= X is equivalent to Z = Z % X
<<=Left shift AND assignment operator Z <<= 2 is same as Z = Z << 2
>>=Right shift AND assignment operator Z >>= 2 is same as Z = Z >> 2
&=Bitwise AND assignment operator Z &= 2 is same as Z = Z & 2
^=bitwise exclusive OR and assignment operator Z ^= 2 is same as Z = Z ^ 2
|=bitwise inclusive OR and assignment operator Z |= 2 is same as Z = Z | 2
Example:

class Program
    {
        static void Main(string[] args)
        {
            int X = 41;
            int Y;

            Y = X;
            Console.WriteLine("Value of c = {0}", Y);
            Console.ReadKey();
        }
    }

When the above code is compiled and executed, it produces the following result:

Value of c= 41

Misc Operators
There are few other important operators including sizeof, typeof and ? : supported by C#.
OperatorDescriptionExample
sizeof()Returns the size of a data type.sizeof(int), will return 4.
typeof()Returns the type of a class.typeof(StreamReader);
&Returns the address of an variable.&a; will give actual address of the variable.
*Pointer to a variable.*a; will pointer to a variable.
? :Conditional Expression If Condition is true ? Then value X : Otherwise value Y
isDetermines whether an object is of a certain type.If( Ford is Car) // checks if Ford is an object of the Car class.
asCast without raising an exception if the cast fails.Object obj = new StringReader("Hello");
StringReader r = obj as StringReader;
I hope this page will helps to learn Operators in C#. Thanks.
BackToTop
Comments



 
Search
Recent Posts
Create Amazon ElasticCache Using Memcached in CSharp
Nov 21 2017 12:09PM Posted By Amose
Get Google Map Lat Lng (Geo Point) By Pincode in C#
Nov 8 2017 10:26AM Posted By Pranav
Google URL Shortener in C#
Oct 26 2017 10:43AM Posted By Sanjay
Bind Gridview from CSV file in Asp.Net C#
Jun 18 2017 11:38PM Posted By Michael
Call WebService method from jQuery in every 1 minute
Feb 27 2017 11:51AM Posted By John
Gridview custom CSS in ASP.Net
Feb 25 2017 10:59AM Posted By Micheal Ryan
Read excel file and bind to Gridview in C#
Feb 25 2017 10:20AM Posted By Micheal
Tags
Follow us on Facebook
Follow us on Google +
Recent post in your Email inbox.
Enter your email address: