codescracker


c#

C# Operators



« Previous Tutorial Next Tutorial »


An operator is simply a symbol used in C# programming, to perform some particular mathematical or logical manipulations in a program. There are these types of operators available in C# programming:

Arithmetic Operators in C#

There are the following arithmetic operators available in C#:

Operator Name
- subtraction operator
+ addition operator
/ division operator
* multiplication operator
% modulus operator
-- decrement operator
++ increment operator

Example

Here is an example program, uses arithmetic operators in C#:

/* C# Operators - Example Program */
		
using System;
namespace ArithmeticOperator
{
	class MyClass 
	{ 
		static void Main(string[] args) 
		{ 
			int num1=21, num2=10, res;
			res=num1+num2;
			Console.WriteLine("Value = {0}", res);
			res=num1-num2;
			Console.WriteLine("Value = {0}", res);
			res=num1*num2;
			Console.WriteLine("Value = {0}", res);
			res=num1/num2;
			Console.WriteLine("Value = {0}", res);
			res=num1%num2;
			Console.WriteLine("Value = {0}", res);
			res=num1++;
			Console.WriteLine("Value = {0}", res);
			res=num1--;
			Console.WriteLine("Value = {0}", res);
			Console.ReadLine();
		}
	}
}

When the above code is compiled and executed, it would produce the following output:

Value = 31
Value = 11
Value = 210
Value = 2
Value = 1
Value = 21
Value = 22

Relational Operators in C#

Here the following table lists all the relational operators available in C#:

Operator Meaning
== This operator is used to check, if values of the two operands are equal or not. If equal, then the condition becomes true, otherwise false
!= This operator is used to check, if values of the two operands are equal or not. If the values are not equal, then condition becomes true, otherwise false
< This operator is used to check, if the value of the left operand is less than the value of right operand or not. If yes, then the condition becomes true, otherwise false
> This operator is used to check, if the value of the left operand is greater than the value of right operand or not. If yes, then the condition becomes true, otherwise false
<= This operator is used to check, if the value of the left operand is less than or equal, to the value of the right operand or not. If yes, then the condition becomes true, otherwise false.
>= This operator is used to check, if the value of the left operand is greater than or equal, to the value of the right operand or not. If yes, then the condition becomes true, otherwise false.

Here is an example program, uses relational operators in C#:

/* C# Operators - Example Program */
		
using System;
class MyClass
{
	static void Main(string[] args)
	{
		int num1=21, num2=10;
		if(num1==num2)
		{
			Console.WriteLine("num1 is equal to num2");
		}	
		else 
		{
			Console.WriteLine("num1 is not equal to num2");
		}
		if(num1<num2)
		{
			Console.WriteLine("num1 is less than num2");
		}
		else
		{
			Console.WriteLine("num1 is not less than num2");
		}
		if(num1>num2)
		{
			Console.WriteLine("num1 is greater than num2");
		}
		else
		{
			Console.WriteLine("num1 is not greater than num2");
		}
		// changing the values of num1 and num2
		num1=10;
		num2=20;
		if(num1<=num2) 
		{	 
			Console.WriteLine("num1 is either less than or equal to num2");
		}      
		if(num2>=num1)
		{
			Console.WriteLine("num2 is either greater than or equal to num1");
		}
	}
}

When the above code is compiled and executed, it would produce the following output:

num1 is not equal to num2
num1 is not less than num2
num1 is greater than num2
num1 is either less than or equal to num2
num2 is either greater than or equal to num1

Logical Operators in C#

Here the following table lists all the logical operators available in C#:

Operator Name Meaning
|| Logical OR Operator If any of the two operands is non-zero then the condition becomes true, otherwise false
&& Logical AND Operator If both the operands are non-zero then the condition becomes true, otherwise false
! Logical NOT Operator This operator simply used to reverse the logical state of its operand. If a condition is true then Logical NOT operator will make it false

Here is an example program, uses logical operators in C#:

/* C# Operators - Example Program */
		
using System;
namespace LogicalOperators 
{
	class MyClass 
	{
		static void Main(string[] args) 
		{
			bool b1 = true; 
			bool b2 = true;
			if(b1 && b2)
			{
				Console.WriteLine("true");
			}
			if(b1 || b2)
			{
				Console.WriteLine("true");
			}
			// changing the values of b1 and b2
			b1 = false;
			b2 = true;
			if(b1 && b2)
			{
				Console.WriteLine("true");
			}
			else
			{
				Console.WriteLine("false");
			}
			if(!(b1 && b2))
			{
				Console.WriteLine("true");
			}	
			Console.ReadLine();
		}
	}
}

When the above code is compiled and executed, it would produce the following output:

true
true
false
true

Bitwise Operators in C#

Bitwise operators works on bits and perform bit by bit operation. Here, the following table lists all the bitwise operators available in C#:

Operator Name Meaning
| Binary OR Operator This operator is used to copy a bit if exists in either the operand
& Binary AND Operator This operator is used to copy a bit to the result if exists in both the operands
~ Binary Ones Complement Operator This is unary operator and has the effect of 'flipping' bits
^ Binary XOR Operator This operator is used to copy the bit if it is set in one operand but not in both
>> Binary Right Shift Operator The left operands value is moved right by the number of bits specified by right operand
<< Binary Left Shift Operator The left operands value is moved left by the number of bits specified by right operand

Here is the truth table given, for &, | and ^ operators

a b a & b a | b a ^ b ~a
0 0 0 0 0 1
0 1 0 1 1 1
1 1 1 1 0 0
1 0 0 1 1 0

Assignment Operators in C#

Here, the following table lists the assignment operators available in C#:

Operator Name Meaning
= Simple Assignment Operator This operator is used to assign values from right side operand to the left side operand
-= Subtract AND Assignment Operator This operator is used to subtract the operand from the left operand and then assign the result to the left operand
+= Add AND Assignment Operator This operator is used to add the right operand to the left operand and then assign the result to the left operand
/= Divide AND Assignment Operator This operator is used to divide the left operand with the right operand and then assign the result to the left operand
*= Multiply AND Assignment Operator This operator is used to multiply the right operand with the left operand and then assign the result to the left operand
%= Modulus AND Assignment Operator This operator takes the modulus using the two operands and then assign the result to the left operand
>>= Right Shift AND Assignment Operator This operator right shifts the value and then assign the result to the left operand
<<= Left Shift AND Assignment Operator This operator left shifts the value and then assign the result to the left operand
&= Bitwise AND Assignment Operator This is a bitwise and assignment operator.
|= Bitwise Inclusive OR AND Assignment Operator This is a bitwise inclusive OR and assignment operator
^= Bitwise Exclusive OR AND Assignment Operator This is a exclusive OR and assignment operator

Here is an example program, uses assignment operators in C#:

/* C# Operators - Example Program */
		
using System;
namespace AssignmentOperators 
{
	class OperatorClass
	{
		static void Main(string[] args)
		{
			int num=21, res;
			res=num;
			Console.WriteLine("Value = {0}", res);
			res += num;
			Console.WriteLine("Value = {0}", res);
			res -= num;
			Console.WriteLine("Value = {0}", res);
			res *= num;
			Console.WriteLine("Value = {0}", res);
			res /= num;
			Console.WriteLine("Value = {0}", res);
			res = 200;
			res %= num;
			Console.WriteLine("Value = {0}", res);
			res <<= 2;
			Console.WriteLine("Value = {0}", res);
			res >>= 2;
			Console.WriteLine("Value = {0}", res);
			res &= 2;
			Console.WriteLine("Value = {0}", res);
			res ^= 2;
			Console.WriteLine("Value = {0}", res);
			res |= 2;
			Console.WriteLine("Value = {0}", res);
			Console.ReadLine();
		}		
	}
}

When the above code is compiled and executed, it would produce the following output:

Value = 21
Value = 42
Value = 21
Value = 441
Value = 21
Value = 11
Value = 44
Value = 11
Value = 2
Value = 0
Value = 2

Miscellaneous Operators in C#

There are some other important operator available in C# which are listed in this table:

Operator Meaning
typeof() Returns the type of a class
sizeof() Returns the size of a data type
& Returns the address of an variable
? : Conditional Expression
* Pointer to a variable
as Cast without raising an exception if the cast fails
is Determines whether an object is of a certain type

Operator Precedence in C#

Higher
() [] -> . ++ --
+ - ! ~ ++ -- (type)* & sizeof
* / %
+ -
<< >>
< <= > >=
== !=
&
^
|
&&
||
?:
= += -= *= /= %= >>= <<= &= ^= |=
,
Lower

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test