C# - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C# Operators

C# Operators

shape Description

C# includes several different kinds of operators. Operators specify the type of operations (assigning values, math, indexing, etc.,) to be performed in an expression.

shape Example

Arithmetic Operators

shape Description

Using these operators we can perform basic mathematical operations. Ex: Addition, Subtraction, Multiplication, Division…etc.

shape Operators

Operator Description Example
+ Adds two operands x+y will give 30
- Subtracts second operand from the first x-y will give -10</td
* Multiplies operands x*y will give 200
/ Divide numerator by denominator x/y will give 2
% Modulus operator gives remainder 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

shape Example

The example provided below shows how to use Arithmetic Operators within a program.  [csharp] using System; using System.Text; namespace SPLessons { class Program { static void Main(string[] args) { int a=10,b=12,c; c=a+b; //Addition Console.Write( ("\nAddition of operands:"+c)); c=a-b; //Subtraction Console.Write( ("\nSubtraction of operands:"+c)); c=a*b; //Multiplication Console.Write( ("\nMultiplication of operands:"+c)); c=a/b; //Division Console.Write( ("\nDivision of operands:"+c)); c=a++; //Increment Console.Write("\nIncrement of operands:"+c); c=a--; //Decrement Console.Write( ("\nDecrement of operands:"+c+"\n")); } } } [/csharp]

Output:

Assignment Operators

shape Description

These operators are used for assigning a value to the operands.

shape Operators

Operator Description Example
= Assignment operator: Assigns right-hand side(called rvalue)to left-hand side(called lvalue) z=x+y assigns value of x+y into z
+= Add AND Assignment operator: Adds right operand to left operand and assigns result to left operand y+=x is equivalent to x=y+x
-= Subtract AND Assignment operator: Subtracts right operand from left operand and assigns result to left operand y-=x is equivalent to y=y-x
*= Multiply AND Assignment operator: Multiplies right operand from left operand and assigns result to left operand y*=x is equivalent to x=y*x
/= Divide AND Assignment operator: Divides right operand from left operand and assigns result to left operand y/=x is equivalent to x=y/x

shape Example

The example provided below shows how to use Assignment Operators within a program.  [csharp] using System; namespace SPLessons { class Assignment { public static void main(String[] args) { int a=10,b; b=a; //Assignment of Operands Console.Write( (“Value:”+b); b+=a; //Add value and then Assign value to Operand Console.Write( (“Value:”+b); b-=a; // Subtract value and then Assign value to Operand Console.Write( (“Value:”+b); b*=a; // Multiply value and then Assign value to Operand Console.Write( (“Value:”+b); b/=a; // Divide value and then Assign value to Operand Console.Write(“Value:”+b); } } } [/csharp]

Output

Relational/Comparison Operators

shape Description

Using these operators, values of two operands can be compared. Boolean values(True or False) are returned after the comparison.

shape Operators

Operator Description
== Checks equal or not, if yes then returns true
!= Checks Equal or not, if values are not equal then condition becomes true
> Checks for greater value
< Checks for lower value
>= Checks for greater than or equal to the value
<= Checks for less than or equal to the value

shape Example

The example provided below shows how to use Relational/Comparison Operators within a program.  [csharp] using System; namespace SPLessons { class Comparison { public static void main(String[] args) { if(a>b) { Console.WriteLine("a is greater than b"); } if(a<b) { Console.WriteLine("a is less than b"); } if(a>=b) { Console.WriteLine("a is greater than,equal to b"); } if(a<=b) { Console.WriteLine("a is less than,equal to b"); } if(a==b) { Console.WriteLine("a is equal to b"); } if(a!=b) { Console.WriteLine("a is not equals to b"); } } } } } [/csharp]

Output:

Bitwise Operators

shape Description

Below mentioned are some of the bitwise operators supported by C#.

shape Operators

Operator Symbol Form Operation
Right shift >> x>>y all bits in x shifted right y bits
Left shift << x<<y all bits in x shifted left y bits
Bitwise AND & x&y each bit in x AND each bit in y
Bitwise OR | x|y each bit in x OR each bit in y
Bitwise XOR ^ x^y each bit in x XOR each bit in y
Bitwise NOT ~ ~x all bits in x flipped

shape Example

The example provided below shows how to use Bitwise Operators within a program.  [csharp] using System; namespace SPLessons { Class Bitwise { static void main(string[] args) { int w=2, x=10,y=20,z; //binary equivalent of 10 =01010 and 20 =10100 z=x|y; Console.Write("\nBinary OR operator:"+z); z=x&y; Console.Write("\nBinary AND operator:"+z); z=~x; Console.Write("\nBinary Ones Complement:"+z); z=x^y; Console.Write("\nBinary XOR operator:"+z); z=x<<w; Console.Write("\nBinary Left Shift operator:"+z); z=x>>w; Console.Write("\nBinary Right Shift operator:"+z); } } } [/csharp]

Output

Logical Operator

shape Description

Using these operators the operations are performed on operands which are either true or false. Let us consider x=true, y=false.

shape Operators

Operator Name of Operator Output
&& AND Operator Output is 1 only when conditions on both sides of operator becomes true
|| OR Opeator Output is 0 only when conditions on both sides of operator becomes false
! NOT Operator Gives inverted output

shape Example

The example provided below shows how to use Logical Operators within a program. [csharp] using System; using System.Text; namespace SPLessons { class Program { static void Main(string[] args) { bool x=true,y=false; if(x||y) { Console.Write("\nTrue"); } if(x&&y) { Console.Write("\nfalse"); } if(!( x&&y )) { Console.Write("\ntrue"); } } } } [/csharp]

Output