C Sharp

1. An Overview of C# 2. OOP 3. C# Environment 4. IDE 5. Basic Requirement 6. Comments 7. My first program 8. Line Break 9. Escape Sequence 10. Identifiers 11. Variables 12. Datatypes 13. Range of datatypes in C# 14. Declare Variables 15. Reserved Key Words 16. Console.Write() 17. Chained & Embedded state 18. Prog.to print sum of 2 nos 19. Console.ReadLine() 20. Prg.to print sum & average 21. Area & Perimeter of Rectangle 22. Area Circumference of Circle 23. Prg.for Simple Calculator 24. Operatos in C# 25. Ex. Of Operators 26. Prg. to Swap two numbers 27. Hierarchy of Operators 28. if( ) statement 29. Prg.to check +ve,-ve or zero 30. if..else statement 31. Prg.to check Odd or Even 32. Prg.to print larger of 2 nos 33. Prg.to print largest of 3 nos 34. Prg.to check divisibility 35. Prg.to check print range 36. for. . . loop statement 37. prg.to print series of nos 38. Prg.to check divisibility 39. prg.to print sum of series 40. Prg.to print Pyramid 41. prg.for Factorial 42. prg.to fill screen 43. prg.for largest/smallest no 44. prg.to print reverse no 45. prg.to add each digit 46. prg.for sum of Factorial 47. prg.for fibonacci series 48. prg.to check Prime nos 49. prg. Prime no from 1 to 100 50. prg.Specified Prime nos 51. while( ) statement 52. do. . While statement 53. goto statement 54. break & continue statement 55. switch case statement 56. Array in C# 57. print reverse order using array 58. Ascending/Descending order 59. prg.to search no in Array 60. Multidimensional arrays 61. String in C# 62. String with spaces 63. prg.to print string in reverse 64. prg.to count A,E,I,O,U vowel 65. User Define Methods 66. Methods program example 67. General Purpose Programs 68. Loan & EMI calculation prg. 69. Table print prg. 70. Leap year program 71. lower to UPPER case prg. 72. Age Distribution prg. 73. Bank note calculation prg. 74. Simple Interest prg. 75. Compound Interest prg. 76. Simple Depreciation prg. 77. Reducing bal.Depreciation prg. 78. Marksheet prg. 79. Income Tax prg. 80. Time calculator prg. 81. Distance converter prg. 82. Volume Air Calculation prg. 83. Time to fill Water Tank prg. 84. Salary Calculation prg. 85. Total Sale Calculation prg. 86. Male/Female pecentage prg. 87. Library Rent prg. 88. Office Expance prg. 89. Total Salary Calculation prg. 90. Profit or Loss prg. 91. Total Profit/Loss prg. 92. Virtual Function . 93. C# Interview Part1 94. C# Interview Part2 95. C# Interview Part3 96. C# Interview Part4 97. C# Interview Part5 98. C# Interview Part6 99. C# Interview Part7 100. C# Interview Part8 101. C# Interview Part9 102. C# Interview Part10 103. C# Interview Part11 104. C# Interview Part12 105. C# Interview Part13 106. C# Interview Part14 107. C# Interview Part15
Pr.Pg Next Pg

Operators tutorials

  • The variables, which are declared and defined, are the operands which are operated upon by the operators.

  • Operators specify what operations are to be performed on the operands.

  • Some operators require two operands, while few operators require only one operand.

  • C# includes many operators, which fall into different categories as follows.

  1. Arithmetic Operators

  2. Unary Operators

  3. Relational Operators

  4. Assignment Operators

  5. Logical Operators

  6. Bitwise Operators


ARITHMETIC OPERATORS

  • There are 5 arithmetic operators in C#

  • Following table shows all the arithmetic operators supported by C#. Assume variable A holds 20 and variable B holds 30 than:

Operator

Description

Example

+

Adds two operands

A + B will give 50

-

Subtracts second operand from the first

A - B will give -10

*

Multiply both operands

A * B will give 600

/

Divide numerator by de-numerator

A / B will give 0

%

Modulus Operator and remainder of after an integer division

A % B will give 20

 

UNARY OPERATORS

  • As operators works on single operands, they are called unary operators.

  • Most common unary operator is unary minus, where a minus sign precedes a numeric variable or expression.

  • Unary minus operator is different from arithmetic minus (Substation). Arithmetic substation or minus operator requires two operands.

For example

-200

- ( a + b )

- 4 * ( 2 + 3 )

 

Other Unary Operator

Increment operator ++. The increment operator ++adds 1 to operand.

Decrement operator - -. The decrement operator -- subtracts 1 to operand.

  • Both Increment and Decrement operator can be used in two different ways.

  • Pre :- If unary operator precedes the operand for example (++ operand or - - operand), then value of operand will be increased / decreased by one, before it is utilized within program.

  • Post:- If unary operator follows the operand for example (operand++ or operand- - ), then value of operand will be increased / decreased by one, after the execution of statement within program.


RELATIONAL OPERATORS

  • The relational operators allow you to compare two values.

  • You can determine if the two values are equal or not and can check which of the two values is the larger.

  • All of the relational operations return a Boolean result.

  • In the case of the 'equal to' and 'not equal to' operations, any two compatible data types can be compared.

  • However, for the comparison operators that determine if one value is greater than another, the data type must support the concept of ordering.

  • This includes all of the basic numeric types but not Boolean values as it is not sensible to consider 'true' to be either greater than or less than 'false'.

Operator

Meaning

Less than

Greater than

= =

Equal to

< =

Less than or Equal to

> =

Greater than or Equal to

! =

Not equal to

 

ASSIGNMENT OPERATORS

  • Simple assignment operators: The equal (=) sign is used for assigning a value to variable. The left-hand side has to be a variable and right-hand side of equal sign has to be value of variable.

for e.g

a = 5; // this statement will assign 5 to variable a.

b = 2 + 10; // this statement will solve right hand side equation and assign 12 to b.

x = y = z = 20; // this this example of multiple assignment, it will assign 20 to x, y and z.

  • Shorthand assignment operator: The plus equal (+=), minus equal (-=), multiply equal (*=), divide equal (/=), modulo equal (%=), the use of shorthand assignment operator has three advantages listed below:

  • What appears on the left-hand side need not be repeated and therefore it becomes easier to write. The statement is more concise and easier to read.


You can also use shorthand assignment operator as follow.

Assume A = 5 and B=2 then check following.

Operator

Regular Expression

Shorthand assignment,

Expression can be written as

Result of A

+ =

A = A + B ;

A += B;

7

- =

A = A B ;

A - = B;

3

* =

A = A * B ;

A * = B;

10

/ =

A = A / B ;

A / = B;

2

% =

A = A % B ;

A % = B;

1

 

LOGICAL OPERATORS

  • The logical operator & logical AND, | meaning OR, ! meaning NOT.

  • The logical operator & and | are used when we want to test more than one condition and makes decisions.

  • When result of both operands are true (T), & (AND) operators gives true result, otherwise it gives false.

  • When result of any operands is true (T), | (OR) operators gives true result, otherwise it gives false.

  • ( ! ) NOT operators reverse Boolean result.

Truth table for each logical operators are below:

Result of A

Condition

Resultof B Condition

AND condition

A & B

OR condition

A | B

Not Condition

! A

T

T

T

T

F

T

F

F

T

F

F

T

F

T

T

F

F

F

F

T

 

Bitwise Operators

  • Bitwise operator works on bits and perform bit by bit operation. The truth tables for &, |, and ^ are as follows:

p

q

p & q

p | q

p ^ q

0

0

0

0

0

0

1

0

1

1

1

1

1

1

0

1

0

0

1

1

 

  • Assume if a=50 and b=15 now in binary format they will be shown as below:

a = 110010

b = 1111

  • Now let apply Binary operator in a and b

 

110010

1111

- - - - - --------

a &b = 0010 which is 2

a | b = 1111 which is 63

a ^b = 1101 which us 61

 

  • The Bitwise operators supported by C# are listed in the following table. Assume variable A holds 60 and variable B holds 13 than:

Operator

Description

Example

&

Binary AND Operator copies a bit to the result

if it exists in both operands.

(A & B) will give 12 which is 0000 1100

|

Binary OR Operator copies a bit if it exists in

either operand.

(A | B) will give 61 which is 0011 1101

^

Binary XOR Operator copies the bit if it is set in

one operand but not both.

(A ^ B) will give 49 which is 0011 0001

~

Binary Ones Complement Operator is unary

and has the effect of 'flipping' bits.

(~A ) will give -60 which is 1100 0011

<< 

Binary Left Shift Operator. The left operands

value is moved left by the number of bits

specified by the right operand.

A << 2 will give 240 which is 1111 0000

>> 

Binary Right Shift Operator. The left operands

value is moved right by the number of bits

specified by the right operand.

A >> 2 will give 15 which is 0000 1111

Pr.Pg border                                              Next Pg