c sharp programming slides 01-fp2005-ver1.0
TRANSCRIPT
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
1/82
C Sharp Programming Day 1
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
2/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.0
2
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
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
3/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.0
3
Day Wise Plan
Day1
Recap of OO
- Data Types
- Class & Object
- Inheritance
- Namespace
- Structure
Day 2
Interface
Assembly
Property Indexer
Collection
Preprocessor Directives
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
4/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.0
4
Day Wise Plan
Day 3
GUI
Delegates
Unsafe Code
Exception Handling
Debugging
Day 4
Threading
Reflection
Serialization
File Handling
Day 5
Parsing
Remoting
Windows Services
Deployment
Day 6 and Day 7 - Project
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
5/82
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
6/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.06
Session Plan
Recap of OO
Classes and Objects
Basic Data Types and Control structures,
Data Members and Methods of a Class (Creation of objects)
Constructors
Destructors
Garbage Collection
Static
Command Line Arguments
Inheritance
Overloading and Overriding
Abstract and Sealed Namespaces
Structures
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
7/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.07
Recap of Object Oriented Concepts
Classes
templates used for defining new types.
Contains attributes and behavior
Instance of a class is called an Object
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
8/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.0
8
Recap of Object Oriented Concepts
Encapsulation
Mechanism that binds together code and the data it manipulates in a class
Data members are made private and are prevented from being accessed
by outside classes thus achieves Data Hiding
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
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
9/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.0
9
Structure of a C# program
using System;
namespace InsideCSharp
{
class FirstProgram
{
public static void Main()
{
Console.WriteLine(Hello World);
}
}
}
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
10/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.010
Structure of a C# program
The extension of a C# program is cs There is no restriction on the filename unless it follows the naming convention
of the OS
Every statement in C# ends with ;
Main is the starting point of execution of a C# program
{ } describes a block of code
Main function must be written inside the class
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
11/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.011
Data Types
Value Types Reference Types
Value types include simple types like
char, int, and float, enum types, and
struct types
Reference types include class types,
interface types, delegate types, and
array types
Variable holds the actual value Variable holds memory location
Allocated on stack Allocated on heap
Assignment of one value type to
another copies the value
Assignment of one reference type to
another copies the reference
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
12/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.012
Data Types Value Types
Data Types
IntegerFloating
PointCharacter Boolean
byte
sbyte
short
ushort
int
float
double
Char bool
uint
long
ulong
decimal
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
13/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.013
Data Types Value Types
Data Type Description Range
bool Represents true/false
byte 8 bit unsigned integer 0 to 255
sbyte 8 bit signed integer -128 to 127
short Short integer 16 bits -32,768 to +32,767
ushort Unsigned short integer 16 bits 0 to 65535
int Integer 32 bits -2,147,483,648 to 2,147,483,647
uint Unsigned integer 32
bits 0 to4,294
,967,
295
long Long integer 64 bits -9,223,372,036,854,775,808 to9,223,372,036,854,775,807
ulong Unsigned long integer 64 bits 0 to 18,446,744,073,709,551,615
float Single precision floating point 32 bits 1.5E-45 to 3.4E+38
double Double precision floating point 64 bits 5E-324 to 1.7E+308
decimal Numeric type for financial calculations 128 bits (supportsupto 28 decimal places)
1E-28 to 7.9E+28
char Character 16 bit (uses Unicode)
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
14/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.014
Variables Value Types
Declaration syntax of a variable in C#:
Ex:
int iNum;
Initializing variables:
Ex:
int iNum = 10;
Variables in C# can be declared anywhere before referencing them.
The scope of the variable is from the declaration till the end of the block in whichit is declared.
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
15/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.015
What is the output of the following program?
using System;
class Test
{
public static void Main()
{
int iNum = 10;
{int iNum = 100;
Console.WriteLine(iNum);
}
Console.WriteLine(iNum);
}}
ERROR
The variable iNumis still in
scope in the inner block
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
16/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.016
This slide has been intentionally left blank : Notes
Page Contd.
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
17/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.017
C# is a strongly typed language
Automatic conversions (or widening conversions) from one type to another
takes place if: the source and the destination types are compatible
the destination type is larger than the source type
Automatic conversions in C# does not happen that leads to data loss.
Explicit conversions require type casting.
int iVal = 1234;
long lVal = iVal;
double dVal = 34.23;
float fVal = dVal;
long lVal = 23456;
int iVal = (int) lVal;
This implicit conversion
does not lead to data loss
The above statement does not
compile as conversion from
double to float leads to data loss.
This is an example of
explicit conversion
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
18/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.018
Literals Value Types
Also called constants.
Integer literals can be of type int, uint, long or ulong depending on their value.
Ex:
12l or 12L is of long type
12ul or 12UL is of ulong type
12u of 12U is of unsigned integer type, uint
Floating point literals of type double
Ex:
12.3F or 12.3f is of type float
9.92M or 9.92m is of type decimal
Boolean literals
Ex:
bool b = true;
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
19/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.019
Types Boxing
Conversion of Value type to Reference Type
Allocates box, copies value into it Unboxing
Conversion of Reference Type back to Value Type
Checks type of box, copies value out
int iVal = 123;int iVal = 123;object oVal = iVal;object oVal = iVal;
int iValue = (int)oVal;int iValue = (int)oVal;
123123iVal
oVal
123123System.Int32System.Int32
123123iValue
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
20/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.020
Control Structures if statement
Requires a boolean expression
int iNum = 10;
if ((iNum % 2) == 0)
{Console.WriteLine("Even Number");
}
else
{Console.WriteLine("Odd Number");
}
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
21/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.021
Control Structure switch statement
No two case constants in the same switch can have identical values.
Fall through rule:
Statement sequence from one case cannot continue to next case.
Can be avoided by using the goto
Default statement cannot fall through
Empty cases can fall through.
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
22/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.022
Control Structure switch statement
What is the output of the following code snippet?
int iNum=2;
switch(iNum)
{
case 1:
Console.WriteLine(iNum);
case 2:
Console.WriteLine(iNum);
break;
}
compile error - control
cannot fall through from one
case to another
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
23/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.023
Control Structure switch statement
char cChoice='i';
switch(cChoice)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
Console.WriteLine("Vowels");
break;
default:Console.WriteLine("Consonants");
break;
}
Vowels
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
24/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.024
Control Structures - loops
while loop:
while (condition)
{
statement/s ;
}
do-while loop:
do
{
statement/s;
}while(condition)
for loop:
for (initialization; condition; iteration)
{
statement/s;
}
break;
continue;
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
25/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.025
Types User defined types
Enumerations
Arrays
Class
Structure
Interface
Delegate
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
26/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.026
Enumerations
Enumeration is a set of named integer constants.
General form of an enumeration:
enum name { enumeration list }
The members of an enumeration are accessed through their type-name and
the dot operator.
The statement:
Produces an output:
green has a value : 1
enum eColors { red, green, blue};
Console.WriteLine (eColors.green + has a value : +
(int) eColors.green);
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
27/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.027
Enumerations
Consider:
enum eColors { red, green=10, blue} ;
What are the values of the members?
red 0
green 10blue 11
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
28/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.028
Arrays
Two ways of creating one dimensional array:
Two ways of initializing one dimensional array:
Two Dimensional array or rectangular array syntax:
int [] aiNum;
aiNum = new int[10]; int [] aiNum = new int[10];
int[] aiNum = {1,2,3};int [] aiNum;aiNum = new int[] {1,2,3};
int[,] aiNum = new int[10,20];
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
29/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.029
This slide has been intentionally left blank : Notes
Page Contd.
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
30/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.030
Jagged Arrays
Are two dimensional arrays in which the number of elements in each row can
vary.
int[][] aiNum = new int[2][]; // array with 2 rows
aiNum[0] = new int[10]; // first row has 10 elements
aiNum[1] = new int[12]; // second row has 12 elements
Jagged Array
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
31/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.031
Types : Reference Type - Class Instantiated with new operator
It consists of members like
Constants, fields, methods, operators, constructors, destructors
Properties, indexers, events
Members can be
Instance members.
Static members.
class Employee{
int iEmpId;
float fSalary;
void findSalary ()
{
// statements to calculate the salary
}
}
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
32/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.032
Object
Steps to instantiate a class
Counter oCounterOne ;
oCounterOne = new Counter();
A reference oCounterOne to
the Counter class is created
.oCounterOne does notdefine Counter
The new operator dynamically allocates
memory for an object of type Counterand returns a reference to it.
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.
Object
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
33/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.033
Objects in memory
Objects can be created in one step.
Ex: Counter oCounterOne = new Counter();
Object of type Counter
Reference
Variable:oCounterOne
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
34/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.034
Object Reference Assignment
ocounterOne oCounterTwo
Statement: oCounterTwo = oCounterOne;
oCounterOne oCounterTwo
Consider: Counter oCounterOne, oCounterTwo;
Object Reference
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
35/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003Version no: 2.0
35
Access Modifiers
Access modifiers specify who can use a type or a member
Access modifiers control encapsulation Class members can be public, private, protected, internal, or
protected internal
to T or types derived from Tprotected
within T onlyprivate
to T or types derived from T
or to types within Aprotected internal
to types within Ainternal
to everyonepublic
Then a member defined in type T and Program A
is accessible
If the access
modifier is
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
36/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003Version no: 2.0
36
Class - Fields
A field
is a member variable of a class holds data for a class .By default each object of a class has its own copy of every
field
Static fields are shared by multiple objects.
public float fSalary;
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
37/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003Version no: 2.0
37
Class Read only Fields
Read only fields are constants
A read only field cannot be modified once initialized. Are initialized in its declaration or in a constructor.
public class MyClass {public static readonly double dNum = Math.Sin(Math.PI);public readonly string sStrOne;
public MyClass(string sStr) { sStrOne = sStr; } }
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
38/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003Version no: 2.0
38
Class Methods
All the executable code of a class is in its methods
Constructors, destructors and operators are special types of methods Methods can have argument lists, statements, can return a value
By default, data is passed by value where a copy of the data is created and
passed to the method
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
39/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003Version no: 2.0
39
A complete class
///
/// This is a counter class to count///
class Counter
{
//Counter variable
private int iCount;
///
/// This method is used to increment the value of thecount.
///
public void increment()
{
iCount++;
}
}
Class comment
block
Method comment
block
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
40/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003Version no: 2.0
40
Methods - ref parameters
ref modifier:
The ref modifier causes arguments to be passed by reference The ref modifier has to be used in the method definition and the code that
calls it
Call statement:
void RefFunction(ref int iP)
{
iP++;
}
int iNu
m = 10;RefFunction(ref iNum);
// iNum is now 11
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
41/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003Version no: 2.0
41
This slide has been intentionally left blank : Notes
Page Contd.
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
42/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003Version no: 2.0
42
This slide has been intentionally left blank : Notes
Page Contd.
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
43/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003Version no: 2.0
43
Methods - out parameters
out modifier:
the out parameter is used in cases where you need to return a value from amethod, but not pass a value to the method.
use out parameter to return more than one value from the method.
out parameter will not have any initial values but must be assigned a value
before the method terminates.
Out Parameter
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
44/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003Version no: 2.0
44
Methods passing variable number of arguments
Methods can have a variable number of arguments, called a parameter array
params keyword declares parameter array This must be last argument of the method and there can be only one such
argument.
int Sum(params int[] aiArr) {
int iSum = 0;
foreach (int iNum in aiArr)
iSum += iNum;
return iSum;
}
int iTotal = Sum(13,87,34);Parameter
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
45/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003Version no: 2.0
45
this keyword
When a local variable of a method has the same name as that of the instance
variable, the local variable hides the instance variable. In such cases, "this" can be used to refer to the instance variables.
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
}
}
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
46/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003Version no: 2.0
46
static
use static to define a member that is independent of any objects
static members can not be accessed using an object of a class. instead, theyare accessed using the syntax:
classname.staticmembername
when objects of a class containing static members are created, a copy of the
static members are not made
all the instances of the class share the same static variable
Restrictions on static methods:
(a) this cannot be used in static methods
(b) static methods can call only static methods - because instance
methods act on specific instances of the class but static methods do not.
(c) static methods can access static data directly.
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
47/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003Version no: 2.0
47
What is the output of the following code snippet?
using System;
class Test{
public static void Main()
{
anotherMethod();
}void anotherMethod()
{
Console.WriteLine(Another Method);
}
}
Compilation error: a non
static method cannot be
called from a static method
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
48/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003Version no: 2.0
48
Method Overloading
Two or more methods within the same class can have same name, but the
parameters must vary. In general, the type or the number of parameters must differ.
The signature (name of the method and its parameter list) does not include a
params parameter if one is present. params does not participate in
overloading
Method Overload
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
49/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003Version no: 2.0
49
Constructor
Is special method that is called whenever an instance of the class is created.
It guarantees that the object will go through proper initialization before beingused.
It is without any return value.
It has same name as its class.
Access specifier must be public as constructors are called from outside the
class.
It can be overloaded.
Constru tor
Thi lid h b i t ti ll l ft bl k N t
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
50/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003Version no: 2.0
50
This slide has been intentionally left blank : Notes
Page Contd.
Thi lid h b i t ti ll l ft bl k N t
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
51/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003Version no: 2.0
51
This slide has been intentionally left blank : Notes
Page Contd.
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
52/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003Version no: 2.0
52
Static Constructors
used to initialize the static variables
access modifiers like public are not allowed on static constructors are called only once during the lifetime of the program
Static Co structor
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
53/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003Version no: 2.0
53
Destructors
A destructor is a method that is called when an instance goes out of scope
Used to clean up any resources Only classes can have destructors
~Employee()
{
// destruction code
}
Thi lid h b i t ti ll l ft bl k N t
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
54/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003Version no: 2.0
54
This slide has been intentionally left blank : Notes
Page Contd.
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
55/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003Version no: 2.0
55
Garbage Collection
Automatic Memory Management Scheme
Garbage Collector always run in background of an application.
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
56/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003Version no: 2.0
56
Finalize
Finalize method is defined in Object class.
A finalizer executes when the object is destroyed.
In C# the declaration of the destructor is a short cut for the Finalize() method.
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
57/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003Version no: 2.0
57
Dispose
Free resources that need to be reclaimed as quickly as possible.
Explicitly called.
public void Dispose()
{
Console.WriteLine(Dispose());
}
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
58/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003Version no: 2.0
58
Operators - overview
C# provides a fixed set of operators, whose meaning is defined for the
predefined types Some operators can be overloaded (e.g. +)
Associativity
Assignment and ternary conditional operators are right-associative
Operations performed right to left
x = y = z evaluates as x = (y = z)
All other binary operators are left-associative Operations performed left to right
x + y + z evaluates as (x + y) + z
Use parentheses to control order
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
59/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003Version no: 2.0
59
class Car {
string vid;
public static bool operator ==(Car x, Car y) {return x.vid == y.vid;
}
}
Operator Overloading
Operator Overloading helps in creating user-defined operations
The method used to overload the operator must be a static method
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
60/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003Version no: 2.0
60
Operator Overloading
+ - ! ~
true false ++ --
Overloadable unary operators
Overloadable binary operators
+ - * / ! ~
% & | ^ == !=
> < > =
Oper t r Over
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
61/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003Version no: 2.0
61
Inheritance
Using inheritance, you can create a general class that defines a common set of
attributes and behaviors for a set of related items. A derived class is a specialized version of the base class and inherits all the
fields, properties, operators and indexers defined by base class
Protected members are private to the class but can be inherited and accessed
by the derived class
The constructor of the base class can be called from the derived class using the
keyword base.
Inherit nce
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
62/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003Version no: 2.0
62
Hidden Names and Inheritance
using System;
class Parent
{protected int p;
public Parent()
{
p = 10;
}
}
class Child : Parent
{
int p;
public Child()
{
p = 100;
}
public void displayParent()
{
Console.WriteLine(p);
}
}
class InheritanceTest
{public static void Main()
{
Child c = new Child();
c.displayParent();
}
}What does the statement
c.displayParent() display?
The child class member p hides the member p
of the parent class.
To access the member p of the Parent class,
use the syntax: base.member.
Refer to notes page for the above modified
program
This slide has been intentionally left blank : Notes
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
63/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003Version no: 2.0
63
This slide has been intentionally left blank : Notes
Page Contd.
Base class reference can refer to a derived class
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
64/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003Version no: 2.0
64
Base class reference can refer to a derived class
object A derived class object can be assigned to a base class reference
But the parent class reference cannot access the members of the derived class
Base Class
Reference
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
65/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003Version no: 2.0
65
Method Overriding and Virtual Methods
Method of a derived class can give another implementation to a method of the
base class Method of the base class must be marked virtual
Method of the derived class must be marked override
class Employee //base class
{
protected double basic;
protected double gross;
public virtual void CalcSal()
{
gross = basic + 0.5*basic;
}
}
class Manager : Employee
{
protected double allow;
public override void
CalcSal()
{
gross = basic +
0.5*basic + allow;
}
}
This slide has been intentionally left blank : Notes
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
66/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003Version no: 2.0
66
This slide has been intentionally left blank : Notes
Page Contd.
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
67/82
Copyright 2005,Infosys Technologies LtdER/CORP/CRS/LA30FC/003
Version no: 2.067
Sealed modifier : methods
Applied to methods:
Cannot be overridden
class class1
{public override sealed int F1()
{
.
}
}
class class2 : class1
{
public override int F1()
{
}}
ERROR
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
68/82
Copyright 2005,Infosys Technologies Ltd ER/CORP/CRS/LA30FC/003Version no: 2.068
Sealed modifier : Class
Applied to class
Cannot be derived
sealed class class1
{} class class2 : class1
{
}
ERROR
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
69/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.069
Abstract modifier
Applied to methods:
Methods cannot have implementation in base class Must be implemented in derived class and marked override
The class containing at least one abstract method must be declared
abstract
//base class
abstract class Employee
{
protected double basic;
protected double gross;
public abstract void
CalcSal();
}
class Manager : Employee
{
protected double allow;
public override void
CalcSal()
{
gross = basic +0.5*basic + allow;
}
}
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
70/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.070
Abstract modifier - Demos
Abstr ct (class1.cs) Abstr ct
(Class2.cs)
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
71/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.071
Namespaces Namespaces provide a way to uniquely identify a type.
Provides logical organization of types.
Namespaces can span assemblies.
Can be nested.
The fully qualified name of a type includes all namespaces.
namespace N1 { // is referred to as N1
class C1 { // is referred to as N1.C1
class C2 { // is referred to as N1.C1.C2
}
}
namespace N2 { // is referred to as N1.N2
class C2 { // is referred to as N1.N2.C2
}
}
}
Fully Qualified
name
Namespa e
P d fi d T Obj
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
72/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.072
Predefined Types - Object
Root of object hierarchy
Storage (book keeping) overhead 0 bytes for value types
8 bytes for reference types
P d fi d T St i
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
73/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.073
Predefined Types - String
A sequence of characters
Strings are immutable (once created the value does not change) String is Reference type and string class in C# is an alias of System.String
class in the dot net framework
The following special syntax for string literals is allowed as they are built in
data types.
String s = I am a string;
C# Type System Type
String System.String
P d fi d T St i B ild
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
74/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.074
Predefined Types - StringBuilder
Every time some modifications are done to the string a new String object
needs to be created StringBuilder class can be used to modify string without creating a new string
Ex : StringBuilder str = new StringBuilder (hi);
Properties:
Length
Methods
Append()
Insert()
Remove()
Replace()
StringBuilder str = new StringBuilder (hi);
str.Append(how are you?)
str.Insert(6,How do you do?);
String Builder
C d Li A t
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
75/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.075
Command Line Arguments
Arguments can be passed to Main function at the command prompt from
where the program is invoked. Main function can return an integer .
Four forms of Main:
public static void Main()
public static int Main()
public static void Main(string[] args)
public static int Main(string[] args)
Command Line
Arguments
St t
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
76/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.076
Structure
Is a generalization of a user-defined data type (UDT)-Value type.
Is created when you want a single variable to hold multiple types of relateddata.
Is declared by using the keyword struct.
Can also include methods as its members.
member access levels possible are only public, internal, private
Structure
This slide has been intentionally left blank : Notes
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
77/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.077
y
Page Contd.
This slide has been intentionally left blank : Notes
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
78/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.078
y
Page Contd.
Str ct re and Class Similarities
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
79/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.079
Structure and Class - Similarities
Both can have members, including constructors, properties, constants, andevents.
Both can implement interfaces.
Both can have static constructors, with or without parameters.
Structure and Class Differences
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
80/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.0
80
Structure and Class - Differences
Class Str ucture
A class is inheritable from other existing
classes.
A structure is not inheritable.
A class can have instance constructors
with or without parameters.
A structure can have instance
constructors only if they takeparameters.
A class is a reference type. A structure is a value type.
Summary
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
81/82
Copyright 2005,Infosys Technologies Ltd
ER/CORP/CRS/LA30FC/003
Version no: 2.0
8
1
Summary
Recap of OO
Basic Data Types and Control Structures Structure
Classes and Objects
Garbage Collection
Static
Command Line Arguments Inheritance
Overloading and Overriding
Abstract and Sealed
Namespaces
Structures
-
8/9/2019 C Sharp Programming SLIDES 01-FP2005-Ver1.0
82/82
Thank You!