C# Examples

C# Example Code


C# Operators

Like any other language C# has extensive set of operators that can categories as : arithmetic, bitwise, relational, assignment, logical and special operators. These operators gives you powerfull control over logical evaluation of any expression in programming. CSharp operators allows you to use any number of operators with any conbinations of operators to evaluate the expression for example : int variable1 = 2 + 2 * 5; // 2 + (2 * 5) = 12 or int variable2 = (10 + 12) * 2; // (10 + 12) * 2 = 44.

In CSharp, by default all binary operators are left-associative and are performed left to right, but there are other operators like assignment and conditional (?:) operators, which are performed right to left.

CSharp has the following types of operators:

  • Arithmetic Operators
  • Bitwise Operators
  • Relational Operators
  • Assignment Operators
  • Logical Operators
  • Special Operators

Arithmetic Operators

CSharp has a set of predefined arithmetic operators.

Assume variable A holds 5 and variable B holds 10 then:

OperatorDescriptionExample
+This will add two operands A + B will give 15
-This will subtract second operand from the first A - B will give -5
*This will multiply both the operands A * B will give 50
/This will divide numerator by denominator B / A will give 2
%This is modulus operator and remainder of after an integer division B % A will give 0
++This will increase integer value by one A++ will give 6
--This will decrease integer value by one A-- will give 4

Arithmetic Operators Example

using System;
class ArithmeticOperatorsDemo
{
	static void main()
	{
		int var1_int, var2_int; // integer variables;
		double var1_dbl,var2_dbl ; // double variables;
		
		var1_int = 20 + 30;  //demostrate '+' operator
		Console.WriteLine(var1_int); // printing variable value as 50;
		
		var2_int = 30 - 20;  //demostrate '-' operator
		Console.WriteLine(var1_int); // printing variable value as 10;
		
		var2_int = 30 * 20;  //demostrate '*' operator
		Console.WriteLine(var2_int); // printing variable value as 600;
		
		var1_int = 30 / 20;  //demostrate '/' operator
		Console.WriteLine(var1_int); // printing variable value as 1.5;
		
		var1_dbl = 30 % 20;  //demostrate '%' operator
		Console.WriteLine(var1_dbl); // printing variable value;
		
		var1_int = 30; 
		var1_int++;  // demostrate '++' operator
		Console.WriteLine(var1_int); // printing variable value as 31;
		
		var1_int = 30; 
		var1_int++;  // demostrate '--' operator
		Console.WriteLine(var1_int); // printing variable value as 29;
	}

}

Bitwise Operators

CSharp has a set of predefined bitwise operators.

OperatorDescription
&This is used to performs a Boolean AND operation on each bit of its integer arguments.
|This is used to performs a Boolean OR operation on each bit of its integer arguments.
^This is used to performs a Boolean exclusive OR operation on each bit of its integer arguments. Exclusive OR means that either operand one is true or operand two is true, but not both.
~This is a unary operator and operates by reversing all bits in the operand.
<<This is used to moves all bits in its first operand to the left by the number of places specified in the second operand. New bits are filled with zeros.
>>This is used to moves all bits in its first operand to the right by the number of places specified in the second operand. The bits filled in on the left depend on the sign bit of the original operand, in order to preserve the sign of the result.

C# bitwise operator works on bit-by-bit basis. Each bitwise operator perform different operation on bit. Below table illustrate the result of each biwise operation using 0 and 1s.

pqp&qp|qp^q~p
000001
100110
010 11 1
111 10 0

Relational Operators

In C# a relational operator compares its operands and returns a logical value based on whether the comparison is true. The operands can be string,numerical, logical, or object values. Strings are compared on lexicographical ordering, using Unicode values.

C# has a set of predefined relational operators.

Assume variable A holds 5 and variable B holds 10 then:

OperatorDescriptionExample
==This allows to check if the value of two operands are equal or not. (A == B) is not true.
!=This allows to check if the value of two operands are equal or not. (A != B) is true.
>This allows to check if the value of left operand is greater than the value of right operand. (A > B) is not true.
<This allows to check if the value of left operand is less than the value of right operand. (A < B) is true.
>=This allows to check if the value of left operand is greater than or equal to the value of right operand. (A >= B) is not true.
<=This allows to check if the value of left operand is less than or equal to the value of right operand. (A <= B) is true.

Relational Operators Example

using System;
class ArithmeticOperatorsDemo
{
	static void main()
	{
		int var1_int, var2_int; // integer variables;
		bool var1_dbl,var2_dbl ; // bool variables;
		var1_int = 2;
		var2_int  = 3;
		if(var1_int == var2_int)
			Console.WriteLine("Equals"); // printing variable value 
										// if both variables are same;
		if(var1_int >= var2_int)
			Console.WriteLine("Grater or Equals"); // printing variable Equal if 
												// both variables are Grater or Equals;
		if(var1_int > var2_int)
			Console.WriteLine("Grater"); // printing variable value 
										 // if both variables are Grater;
		if(var1_int < var2_int)
			Console.WriteLine("Less"); // printing variable value 
									   // if both variables are Less
		if(var1_int <= var2_int)
			Console.WriteLine("Less or Equal"); // printing variable value 
												// if both variables are Less or Equal;
		if(var1_int != var2_int)
			Console.WriteLine("Not Equal"); // printing variable value 
											// if both variables are Not Equal;
	}
}

Assignment Operators

In C# an assignment operator assigns a value to its left operand based on the value of its right operand. The basic assignment operator is equal (=), which assigns the value of its right operand to its left operand. That is, x = y assigns the value of y to x.

C# has a set of predefined assignment operators.

Assume variable X holds 5 and variable Y holds 10 then:

OperatorDescriptionExample
=This allows to assign values from right side operands to left side operand Z = X + Y will assigne value of X + Y into Z
+=This allows to add right operand to the left operand and assign the result to left operand. Z += X is equivalent to Z = Z + X
-=This allows to subtract right operand from the left operand and assign the result to left operand. Z -= X is equivalent to Z = Z - X
*=This allows to multiply right operand with the left operand and assign the result to left operand. Z *= X is equivalent to Z = Z * X
/=This allows to divide left operand with the right operand and assign the result to left operand. Z /= X is equivalent to Z = Z / X
%=This allows to take modulus using two operands and assign the result to left operand. Z %= X is equivalent to Z = Z % X

Logical Operators

In C# logical operators are typically used with Boolean (logical) values; when they are, they return a Boolean value. C# has a set of predefined logical operators.

Assume variable X holds 5 and variable Y holds 10 then:

OperatorDescriptionExample
&&This is logical AND operator. If both the operands are non zero then then condition becomes true. (X && Y) is true.
||This is logical OR Operator. If any of the two operands are non zero then then condition becomes true. (X || Y) is true.
!This is 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 false.

Special Operators

C# has the following special type of operators

OperatorDescriptionExample
(?:)Also called ternary operator, select value from two expression based on a boolean expressionvalue = (x < 10) ? 15 : 5;
(??)This is used to provide a default value for a null value.string s = name ?? "unknown";
typeofThis is used to get the type of the object. typeof(File);
isThis is used to determine object reference can be converted to a specific type or interface. If(iop is object)
asThis is similar to is operator, additionally performs the explicit conversion to that type. Object obj = new File(); File r = obj as File;
&Returns the address of an variable. &var1_int; returns actual address of the variable.

Operator Precedence

In CSharp operator precedence determine the precedence of operator from highest to lowest and evaluate the expression. Below table illustrate the operator precedence from highest to lowest.

Highest  
() [] -> . ++ - -Left to right
+ - ! ~ ++ - - (type)* & sizeofRight to left
* / %Left to right
+ -Left to right
<< >>Left to right
< <= > >=Left to right
== !=Left to right
&Left to right
^Left to right
|Left to right
&&Left to right
||Left to right
?:Right to left
= += -= *= /= %=>>= <<= &= ^= |=Right to left
,Left to right
Lowest