Professional Documents
Culture Documents
C Sharp Programming SLIDES 01-FP2005-Ver1.0
C Sharp Programming SLIDES 01-FP2005-Ver1.0
Course Objectives
• To introduce the participants to C# programming and object oriented features
of C#
• To understand the component oriented features of C# like indexers, properties,
attributes and illustrate their usage in programs
• To introduce development of Assembly
• To introduce Windows programming and delegate based event handling in C#
• To understand Exception handling techniques in C#
• To introduce Multithreading in C#
• To introduce to Reflection
• To introduce File Handling in C#
• To introduce Parsing
• To introduce concept of Remoting through C# programs
• To introduce Windows Services
• To introduce Application Deployment
• Day 2
− Interface
− Assembly
− Property
− Indexer
− Collection
− Preprocessor Directives
• Inheritance
− Process by which one object can acquire the properties of another object.
− Supports hierarchical classification
• Polymorphism
-Ability of objects to respond in their own unique way to the same message
Value types include simple types like Reference types include class types,
char, int, and float, enum types, and interface types, delegate types, and
struct types array types
Floating
Integer Character Boolean
Point
sbyte
double
short
decimal
ushort
int
uint
long
ulong
decimal Numeric type for financial calculations – 128 bits (supports 1E-28 to 7.9E+28
upto 28 decimal places)
char Character – 16 bit (uses Unicode)
• Initializing variables:
Ex:
int iNum = 10;
The scope of the variable is from the declaration till the end of the block in which it
is declared.
• Boolean literals
Ex:
bool b = true;
iVal 123
oVal System.Int32
123
iValue 123
int iNum=2;
switch(iNum)
{ compile error - control
cannot fall through from one
case 1: case to another
Console.WriteLine(iNum);
case 2:
Console.WriteLine(iNum);
break;
}
for loop:
for (initialization; condition; iteration)
{
statement/s;
}
break;
Copyright © 2005,
continue; 24 ER/CORP/CRS/LA30FC/003
Infosys Technologies Ltd Version no: 2.0
Types – User defined types
Enumerations
Arrays
Class
Structure
Interface
Delegate
The statement:
Console.WriteLine (eColors.green + “ has a value : “ +
(int) eColors.green);
Produces an output:
green has a value : 1
int [] aiNum;
aiNum = new int[10]; int [] aiNum = new int[10];
int [] aiNum;
int[] aiNum = {1,2,3};
aiNum = new int[] {1,2,3};
Jagged Array
A reference oCounterOne
to the Counter class is
The new operator dynamically
created .oCounterOne does
allocates memory for an object of type
not define Counter
Counter and returns a reference to it.
Object
Note: The difference between a simple variable and the
reference type variable is that, simple variable holds the value
where a reference type variable points to a value.
Reference
Po
Variable: int
oCounterOne s to
ocounterOne oCounterTwo
Object Reference
public to everyone
Out Parameter
class Demo
{
int i; // instance variable
public void fun (int i)
{
i = 10; // the local variable is referred
this.i = i; // to refer to the instance variable, this
is used
}
}
Method Overload
Constructor
Static Constructor
~Employee()
{
// destruction code
}
• In C# the declaration of the destructor is a short cut for the Finalize() method.
• Explicitly called.
class Car {
string vid;
public static bool operator ==(Car x, Car y) {
return x.vid == y.vid;
}
}
+ - ! ~
true false ++ --
+ - * / ! ~
% & | ^ == !=
Operator Overload
The constructor of the base class can be called from the derived class using the
keyword base.
Inheritance
Base Class
Reference
} }
} }
Copyright © 2005, 65 ER/CORP/CRS/LA30FC/003
Infosys Technologies Ltd Version no: 2.0
Sealed modifier : methods
• Applied to methods:
− Cannot be overridden
ERROR
class class1
{
class class2 : class1
public override sealed int F1()
{ {
……. public override int F1()
} {
} ………
}
}
} }
}
Copyright © 2005, 69 ER/CORP/CRS/LA30FC/003
Infosys Technologies Ltd Version no: 2.0
Abstract modifier - Demos
}
}
namespace N2 { // is referred to as N1.N2 Namespace
class C2 { // is referred to as
N1.N2.C2
}
}
}
Copyright © 2005, 71 ER/CORP/CRS/LA30FC/003
Infosys Technologies Ltd Version no: 2.0
Predefined Types - Object
• Root of object hierarchy
• Storage (book keeping) overhead
− 0 bytes for value types
− 8 bytes for reference types
String System.String
Command Line
Arguments
Structure
Class Structure
A class is inheritable from other existing A structure is not inheritable.
classes.
A class can have instance constructors A structure can have instance
with or without parameters. constructors only if they take
parameters.
A class is a reference type. A structure is a value type.