BSc CSIT CSC367 : Prem Prasad Shrestha
NET Centric Computing
CSC367
Unit 1: Language Preliminaries
C# Language
Data Types
CSC367 Language Preliminaries Slide 2
1
BSc CSIT CSC367 : Prem Prasad Shrestha
C# Language
Simple Types
Simple Type
Integer Type Floating Point Type Decimal Boolean Character
Type Type Type
int short byte long float double decimal bool char
CSC367 Language Preliminaries Slide 3
Data Types
Type Description Range Suffix
byte 8-bit unsigned integer 0 to 255
sbyte 8-bit signed integer -128 to 127
short 16-bit signed integer -32,768 to 32,767
ushort 16-bit unsigned integer 0 to 65,535
int 32-bit signed integer -2,147,483,648
to
2,147,483,647
uint 32-bit unsigned integer 0 to 4,294,967,295 u
long 64-bit signed integer -9,223,372,036,854,775,808 l
to
9,223,372,036,854,775,807
ulong 64-bit unsigned integer 0 to 18,446,744,073,709,551,615 ul
float 32-bit Single-precision floating point type -3.402823e38 to 3.402823e38 f
double 64-bit double-precision floating point type -1.79769313486232e308 to d
1.79769313486232e308
decimal 128-bit decimal type for financial and monetary (+ or -)1.0 x 10e-28 to 7.9 x 10e28 m
calculations
char 16-bit single Unicode character Any valid character, e.g. a,*, \x0058
(hex), or\u0058 (Unicode)
bool 8-bit logical true/false value True or False
object Base type of all other types.
string A sequence of Unicode characters
CSC367 Language Preliminaries Slide 4
2
BSc CSIT CSC367 : Prem Prasad Shrestha
Variables
• Variables are special containers that hold some data at
specific memory locations. Variables can either be
numeric, string, date, character, Boolean and so on.
• Int a; or int a=20;
Constant
• A constant is a variable whose value can’t be changed
throughout its lifetime. By prefixing a variable with the
const keyword and later initializing it,
• const int i=10;
CSC367 Language Preliminaries Slide 5
C# Operators
• Arithmetic Operators
• Assignment Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
CSC367 Language Preliminaries Slide 6
3
BSc CSIT CSC367 : Prem Prasad Shrestha
Arithmetic Operators
Operator Name Description Example
Computes the sum of left and right
+ Addition int x = 5 + 5;
operands.
Subtract the right operand from the
- Subtraction int x = 5 - 1;
left operand
* Multiplication Multiply left and right operand int x = 5 * 1;
Divides the left operand by the right
/ Division int x = 10 / 2;
operand
Computes the remainder after
% Remainder dividing its left operand by its right int x = 5 % 2;
operand
Unary increment ++ operator
++ Unary increment x++
increases its operand by 1
Unary decrement -- operator
-- Unary decrement x--
decreases its operand by 1
CSC367 Language Preliminaries Slide 7
Assignment Operators
Operator Example Same As
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
&= x &= 3 x=x&3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3
CSC367 Language Preliminaries Slide 8
4
BSc CSIT CSC367 : Prem Prasad Shrestha
Relational or Comparision Operators
Operator Name Example
== Equal to x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
CSC367 Language Preliminaries Slide 9
Logical Operators
Operator Name Description Example
&& Logical AND Returns True if both statements are true x < 5 && x < 10
|| Logical OR Returns True if one of the statements is true x < 5 || x < 4
Reverse the result, returns False if the result
! Logical NOT !(x < 5 && x < 10)
is true
CSC367 Language Preliminaries Slide 10
10
5
BSc CSIT CSC367 : Prem Prasad Shrestha
Bitwise Operators
Example (a = 0,
Operator Name Description
b = 1)
It compares each bit of the first operand with the
corresponding bit of its second operand. If both bits are
& Bitwise AND 1, then the result bit will be 1; otherwise, the result will a & b (0)
be 0.
It compares each bit of the first operand with the
corresponding bit of its second operand. If either of the
| Bitwise OR bit is 1, then the result bit will be 1; otherwise, the result a | b (1)
will be 0.
It compares each bit of the first operand with the
Bitwise Exclusive corresponding bit of its second operand. If one bit is 0
^ and the other bit is 1, then the result bit will be 1; a ^ b (1)
OR (XOR)
otherwise, the result will be 0.
Bitwise It operates on only one operand, and it will invert each
~ ~(a) (1)
Complement bit of operand. It will change bit 1 to 0 and vice versa.
It shifts the number to the left based on the specified
<< Bitwise Left Shift) number of bits. The zeroes will be added to the least b << 2 (100)
significant bits.
It shifts the number to the right based on the specified
>> Bitwise Right Shift number of bits. The zeroes will be added to the least b >> 2 (001)
significant bits.
CSC367 Language Preliminaries Slide 11
11
Flow Control of Programming
• If/else construct (If/Else, If/Else If/Else)
• Switch construct
• Loops
• For loop
• Foreach loop
• While loop
• Do/while loop
• Jump Statement
• Break
• Continue
• Goto
CSC367 Language Preliminaries Slide 12
12
6
BSc CSIT CSC367 : Prem Prasad Shrestha
Flow Control of Programming
If/else construct
string str = "Welcome Home";
if (str.Length > 0)
Console.WriteLine("Length of Word is > 0.");
else
Console.WriteLine("Lenght of Word is < 0 ");
int data = 10;
if (data != 5)
{
Console.WriteLine("Value is not equal.");
}
CSC367 Language Preliminaries Slide 13
13
Flow Control of Programming
If/else construct
bool isFalse;
if (data == 0)
{
isFalse = true;
Console.WriteLine("Data is 0");
}
else
{
isFalse = false;
Console.WriteLine("Data is Not 0");
}
CSC367 Language Preliminaries Slide 14
14
7
BSc CSIT CSC367 : Prem Prasad Shrestha
Flow Control of Programming
If/else construct
int x, y, z;
x = int.Parse(Console.ReadLine());
y = Convert.ToInt32(Console.ReadLine());
z = int.Parse(Console.ReadLine());
if (x > y && x > z)
{
Console.WriteLine("x is Highest");
}
else if (y > x && y > z)
{
Console.WriteLine("y is Highest");
}
else if (z > x && z > y)
{
Console.WriteLine("z is Highest");
}
CSC367 Language Preliminaries Slide 15
15
Flow Control of Programming
Switch Case
int day;
Console.WriteLine("Enter the 1 to 7 for Day : ");
day = int.Parse(Console.ReadLine());
switch (day)
{
case 1:
Console.WriteLine("The Day is Sunday");
break;
case 2:
Console.WriteLine("The Day is Monday");
break;
………….
case 7:
Console.WriteLine("The Day is Saturday");
break;
default:
Console.WriteLine("The Day is Invalid");
break;
}
CSC367 Language Preliminaries Slide 16
16
8
BSc CSIT CSC367 : Prem Prasad Shrestha
Flow Control of Programming
Loops
(For Loop)
this construct allow us to iterate upto condition limit from the
value of initialization
for(initialize; condition; incre/decrement)
{
code block to be executed
}
for(int i=0; i<5; i++)
{
Console.WriteLine(i);
}
CSC367 Language Preliminaries Slide 17
17
Flow Control of Programming
Loops
(Foreach Loop)
this construct allow us to iterate through each item of the
collection, without upper limit.
string[] country = { "Nepal", "USA", "Canada", "India", "United Kingdom" };
foreach(string x in country)
{
Console.WriteLine(x);
}
CSC367 Language Preliminaries Slide 18
18
9
BSc CSIT CSC367 : Prem Prasad Shrestha
Flow Control of Programming
Loops
(while Loop)
Used to repeat the statement or block of statement number of
times which is known before execution.
initialization
while(condition)
statement;
increment/decrement
Int x=0;
while(x<10)
{
Console.WriteLine(x);
x++;
}
CSC367 Language Preliminaries Slide 19
19
Flow Control of Programming
Loops
(dowhile Loop)
Used to repeat the statement or block of statement number of
times which is unknown before execution.
initialization
do
{
statement; this construct can execute the loop
increment/decrement once if the condition is false.
} while(condition)
Int x=0;
do
{
Console.WriteLine(x);
x++;
} while(x<10)
CSC367 Language Preliminaries Slide 20
20
10
BSc CSIT CSC367 : Prem Prasad Shrestha
Flow Control of Programming
Jump Statements
break
Used to break the loop and exit from the execution.
for(int i=0; i<10;i++)
{
Console.WriteLine(“No. : “+i);
if(i==5)
break;
}
CSC367 Language Preliminaries Slide 21
21
Flow Control of Programming
Jump Statements
continue
Used to continue or start the loop without exiting from the
execution.
for(int i=0; i<10;i++)
{
Console.WriteLine(“No. : “+i);
if(i==5)
continue;
}
CSC367 Language Preliminaries Slide 22
22
11
BSc CSIT CSC367 : Prem Prasad Shrestha
Flow Control of Programming
Jump Statements
goto
Used to jump to any point of the code while executing.
int i = 0;
labelX: Console.WriteLine("Label Construct" + i++);
if (i < 5)
goto labelX;
CSC367 Language Preliminaries Slide 23
23
12