c# and .net vtu
TRANSCRIPT
-
8/11/2019 c# and .net VTU
1/49
Subject Name: C# AND .NET CONCEPTS
Subject Code: 10CS761
Prepared By: ! Su"at$% S & A!!t. Pro'e!!or() Dr*o+e!$ &Pro'e!!or(
Departmet: CSE
Date : ,,.0-.,01
10//1 1C#AND .NET 10CS7612N3T 34
-
8/11/2019 c# and .net VTU
2/49
UNIT IV
OBJECT ORIENTED
PROGRAMMING WITH
C#
10//1 ,C#AND .NET 10CS7612N3T 34
-
8/11/2019 c# and .net VTU
3/49
-
8/11/2019 c# and .net VTU
4/49
10//1
DEFINITION OF C# CLASS
C#AND .NET 10CS7612N3T 34
Its a buildin! bloc$ that contains properties and functionalities that describe
so&e !roup of ob(ectsCreatin! ob(ect of a specific class type called instance of the classIs a )ser Defined Type*)DT+ that is co&posed of data and functions that act
upon this data,ccess specifiers specify the access rules for the &e&bers as %ell as the class
itself- if not &entioned then the default access specifier for a class type
is internal. Default access for the &e&bers is ri!ateData type specifies the type of variable- and return type specifies the data
type of the data- the ðod returns- if anyTo access the class &e&bers- you %ill use the dot *.+ operatorThe dot operator lin$s the na&e of an ob(ect %ith the na&e of a &e&ber
-
8/11/2019 c# and .net VTU
5/49
10//1 C#AND .NET 10CS7612N3T 34
S"NTA
/access specifier0 class class1na&e
2
33 &e&ber variables
/access specifier0 /data type0 variable45
/access specifier0 /data type0 variable65
... /access specifier0 /data type0 variable75
33 &e&ber ðods
/access specifier0 /return type0 ðod4*para&eter1list+
2 33 ðod body 8
/access specifier0 /return type0 ðod6*para&eter1list+2 33 ðod body 8
...
/access specifier0 /return type0 ðod7*para&eter1list+
2 33 ðod body 88
-
8/11/2019 c# and .net VTU
6/49
10//1
EAMPLEusin! Syste&5
na&espace 'o9,pplication
2
class 'o9
2
public double len!th5 33 en!th of a bo9
public double breadth5 33 'readth of a bo9
public double hei!ht5 33 ;ei!ht of a bo9
8
class 'o9tester 2
static void ar!s+
2
'o9 'o94 ? ne% 'o9*+5
33 Declare 'o94 of type 'o9
'o9 'o96 ? ne% 'o9*+5
33 Declare 'o96 of type 'o9
double volu&e ? @.@5
33 Store the volu&e of a bo9 here
33 bo9 4 specification
'o94.hei!ht ? A.@5
'o94.len!th ? B.@5
'o94.breadth ? .@5
6C#AND .NET 10CS7612N3T 34
-
8/11/2019 c# and .net VTU
7/49
10//1
EAMPLE$C%nt&'(()
7C#AND .NET 10CS7612N3T 34
33 bo9 6 specification
'o96.hei!ht ? 4@.@5
'o96.len!th ? 46.@5
'o96.breadth ? 4.@5
33 volu&e of bo9 4
volu&e ? 'o94.hei!ht 'o94.len!th 'o94.breadth5
Console.Friteine*GHolu&e of 'o94 : 2@8G- volu&e+5
33 volu&e of bo9 6
volu&e ? 'o96.hei!ht 'o96.len!th 'o96.breadth5 Console.Friteine*GHolu&e of 'o96 : 2@8G- volu&e+5
Console."eadey*+5
8
8
8
-
8/11/2019 c# and .net VTU
8/49
10//1
CONSTRUCTORS IN C#
, class +%n,tr-+t%ris a special &e&ber function of a class that is e9ecuted%henever %e create ne% ob(ects of that class
, constructor %ill have e9act sa&e na&e as the class and it does not have
any return type
, &e.a-lt +%n,tr-+t%rdoes not have any para&eter but if you need a
constructor can have para&eters. Such constructors are
called ara/eteri0e& +%n,tr-+t%r,. This techniJue helps you to assi!n
initial value to an ob(ect at the ti&e of its creation
C#AND .NET 10CS7612N3T 34
-
8/11/2019 c# and .net VTU
9/49
10//1
EAMPLEusin! Syste&5
na&espace ine,pplication
2 class ine
2 private double len!th5
33 en!th of a line
public ine*+
2Console.Friteine*GOb(ectis bein! createdG+5
8
public void seten!th* double len +
2 len!th ? len5 8
public double !eten!th*+
2
return len!th5 8
static void ar!s+ -C#AND .NET 10CS7612N3T 34
-
8/11/2019 c# and .net VTU
10/49
10//1
DESTRUCTORS
, &e,tr-+t%ris a special &e&ber function of a class that is e9ecuted%henever an ob(ect of its class !oes out of scope
, &e,tr-+t%r%ill have e9act sa&e na&e as the class prefi9ed %ith a tilde *K+
and it can neither return a value nor can it ta$e any para&eters
Destructor can be very useful for releasin! resources before co&in! out of
the pro!ra& li$e closin! files- releasin! &e&ories etc Destructors cannot be inherited or overloaded
10C#AND .NET 10CS7612N3T 34
-
8/11/2019 c# and .net VTU
11/49
10//1
EAMPLEusin! Syste&5
na&espace ine,pplication 2
class ine 2 private double len!th5
33 en!th of a line
public ine*+ 33 constructor
2 Console.Friteine*GOb(ect is bein! createdG+5 8
Kine*+ 33destructor
2 Console.Friteine*GOb(ect is bein! deletedG+5
8 public void seten!th* double len +
2 len!th ? len5 8
public double !eten!th*+
2 return len!th5 8
static void ar!s+?
11C#AND .NET 10CS7612N3T 34
-
8/11/2019 c# and .net VTU
12/49
10//1
Public Interface is any ite& declared in a class usin! public$ey%ord
Class &e&bers %hich are accessible fro& an ob(ect variable are
called as public interface of a class
Public interface of a class is populated by
4.ðods&e&ber functions
6.properties ,ccessors and &utators
.public fields or public variables
1,C#AND .NET 10CS7612N3T 34
PUBLIC INTERFACE
-
8/11/2019 c# and .net VTU
13/49
10//1
PILLARS OF OOP
Encapsulation
Inheritance
Poly&orphis&
15C#AND .NET 10CS7612N3T 34
-
8/11/2019 c# and .net VTU
14/49
10//1
ENCAPSULATION
Procedure of coverin! up of data and functions into a sin!le unit*called class+
,n encapsulated ob(ect is often called an abstract data type
NEED FOR ENCAPSULATION
Encapsulation provides a %ay to protect data fro& accidental
corruption. "ather than definin! the data in the for& of public- %e
can declare those fields as private. The Private data are &anipulated
indirectly by t%o %ays:
The first ðod is usin! a pair of conventional accessor and
&utator ðods
,nother one ðod is usin! a na&ed property
1C#AND .NET 10CS7612N3T 34
-
8/11/2019 c# and .net VTU
15/49
10//1
ENCAPSULATION USING ACCESSORS AND
MUTATORS To &anipulate the data in that class *Strin! departna&e+ %e define an
accessor *!et ðod+ and &utator *set ðod+
usin! syste&5public class Depart&ent2
private strin! departna&e5.......33 ,ccessor.
public strin! LetDepartna&e*+2return departna&e5833
-
8/11/2019 c# and .net VTU
16/49
10//1
;ere %e use t%o separate ðods to assi!n and !et the reJuired
data
public static int ar!s+
2
Depart&ent d ? ne% Depart&ent*+5
d.SetDepartna&e*GEECT"O7ICSG+5
Console.Friteine*GThe Depart&ent is :GMd.LetDepartna&e*++5
return @5
8
In the above e9a&ple %e canNt access the private data departna&e
fro& an ob(ect instance. Fe &anipulate the data only usin! those
t%o ðods
16C#AND .NET 10CS7612N3T 34
-
8/11/2019 c# and .net VTU
17/49
10//1
ENCAPSULATION USING PROPERTIES usin! syste&5
public class Depart&ent2
private strin! departna&e5
public strin! Departna&e
2
!et
2
return departna&e5
8set
2
departna&e?value5
8
8
8
public class Depart&ent&ain
2public static int ar!s+
2
Depart&ent d? ne% Depart&ent*+5
d.departna&e?GCo&&unicationG5
Console.Friteine*GThe Depart&ent is 2@8G- d.Departna&e+5
return @5
8
8 17C#AND .NET 10CS7612N3T 34
-
8/11/2019 c# and .net VTU
18/49
10//1
The property has t%o accessor !et and set. The !et accessor returns the
value of the so&e property field.
The set accessor sets the value of the so&e property field %ith the
contents of GvalueG. Properties can be &ade readonly.
This is acco&plished by havin! only a !et accessor in the property
i&ple&entation.
1C#AND .NET 10CS7612N3T 34
-
8/11/2019 c# and .net VTU
19/49
-
8/11/2019 c# and .net VTU
20/49
10//1
The class "eadDepart&ent has a Departna&e property that
only i&ple&ents a !et accessor.
It leaves out the set accessor. This particular class has a
constructor- %hich accepts a strin! para&eter.
The
-
8/11/2019 c# and .net VTU
21/49
10//1
WRITE ONL" PROPERT"
Properties can be &ade also Friteonly. This is acco&plished by havin! onlya set accessor in the property i&ple&entation.
usin! syste&5
public class FriteDepart&ent
2
private strin! departna&e5public strin! Departna&e
2
set
2
departna&e?value5
Console.Friteine*GThe Depart&ent is :2@8G-departna&e+5
8
8
8
public class FriteDepart&ain
2 ,1C#AND .NET 10CS7612N3T 34
-
8/11/2019 c# and .net VTU
22/49
10//1
The class FriteDepart&ent has no% has a Departna&e
property that only i&ple&ents a set accessor. It leaves out the !et accessor.
The set accessor ðod is varied a little by it prints the value
of the departna&e after it is assi!ned.
,,C#AND .NET 10CS7612N3T 34
-
8/11/2019 c# and .net VTU
23/49
10//1
INHERITANCE
"euse e9istin! code
"elationship bet%een 6 or &ore classes
,ll variables and ðods in the base class can be called in the
derived classes. 6 flavors of inheritance
4. classical inheritance*isa relationship+
6.contain&ent3dele!ation &odel*hasa relationship+
,5C#AND .NET 10CS7612N3T 34
-
8/11/2019 c# and .net VTU
24/49
10//1
CLASSICAL INHERITANCE
7e% classes &ay e9tend the functionality of other classes
Isa relationship
Sales person isa E&ployee
,C#AND .NET 10CS7612N3T 34
-
8/11/2019 c# and .net VTU
25/49
10//1
;ere base classes are used to define !eneral characteristics that are co&&on to all
subclasses
)sin! Syste&5
public class parentclass
2
public parentclass*+
2
Console.Friteine*parent constructor+58
public void print*+
2
Console.Friteine*I& a parent class+5
8
8
public class childclass:parentclass
2
,C#AND .NET 10CS7612N3T 34
-
8/11/2019 c# and .net VTU
26/49
10//1
C%ntain/ent1Dele2ati%n /%&el
;asa relationship
Specifies ho% one class is &ade up of other classes*the parts+
public class en!ine
2
public void start*+
2
Syste&.console.Friteine* En!ine started+5
88
public class car
2
public strin! &a$e5
public en!ine e5,6C#AND .NET 10CS7612N3T 34
-
8/11/2019 c# and .net VTU
27/49
10//1
BASE 3E"WORD
)sed to access the &e&bers of base class fro& %ithin a derived class
4.Call a ðod on the base class that has been overridden by another ðod
6. Specifies %hich base class constructor should be called %hen creatin!
instances of the derived class
,7C#AND .NET 10CS7612N3T 34
-
8/11/2019 c# and .net VTU
28/49
10//1
)sin! Syste&5
Class ,
2
Public int i5
Public a*int i+
2
This.i?i58
8
Class ':,
2 7e% int i?@5
Public '*int i+:base*i+2 This.i?4@@5
8
Public void display*+
2 Console.Friteine*value of base class 4 is 2@8-base-i+5
Console.Friteine*value of derived class 4 is 2@8-i+5
8 ,C#AND .NET 10CS7612N3T 34
-
8/11/2019 c# and .net VTU
29/49
10//1
PREVENTING INHERITANCE* SEALED CLASS
Fhen a class is declared %ith sealed $ey%ord- it cannot be inherited
Error %ill occur if a sealed class is declared as base class of another
class
It is used to prevent the derivation of a class
Structs are i&plicitly sealed.
,-C#AND .NET 10CS7612N3T 34
-
8/11/2019 c# and .net VTU
30/49
10//1
SEALED METHODS
, ðod cant be declared as sealed
Fhen overridin! a ðod in a derived class- the override ðod
can be declared as sealed.
Fe can avoid further overridin! of the ðod %hich is declared assealed.
50C#AND .NET 10CS7612N3T 34
-
8/11/2019 c# and .net VTU
31/49
10//1
EAMPLE
)sin! Syste&5
Sealed class ,
2 public void display*+
2
Console.Friteine*virtual ðod+58
8
public class ':,
2
public override sealed void display*+
2
Console.Friteine*sealed ðod+5
8 51C#AND .NET 10CS7612N3T 34
-
8/11/2019 c# and .net VTU
32/49
10//1
PROTECTED 3E"WORD
The r%te+te&$ey%ord is a &e&ber access &odifier. , protected &e&ber is
accessible %ithin its class and by derived class instances. or a co&parisonof r%te+te&%ith the other access &odifiers
, protected &e&ber of a base class is accessible in a derived class only if the
access occurs throu!h the derived class type
class ,
2 protected int 9 ? 465 8
class ' : ,
2 static void
-
8/11/2019 c# and .net VTU
33/49
10//1
PROTECTED 3E"WORD$CONTD(()
The state&ent a.9 ? 4@ !enerates an error because it is &ade %ithin the static
ðod
-
8/11/2019 c# and .net VTU
34/49
10//1
POL"MORPHISM
P%l4/%r5i,/&eans havin! &any for&s one interface- &ultiple functions
Poly&orphis& can be static or dyna&ic
In ,tati+ %l4/%r5i,/ the response to a function is deter&ined at the
co&pile ti&e
In &4na/i+ %l4/%r5i,/ - it is decided at runti&e
5C#AND .NET 10CS7612N3T 34
-
8/11/2019 c# and .net VTU
35/49
10//1
D"NAMIC POL"MORPHISM usin! Syste&5
public class Dra%in!Ob(ect
2 public virtual void Dra%*+ 2 Console.Friteine*GIN& (ust a !eneric dra%in! ob(ect.G+5 88
public class ine : Dra%in!Ob(ect
2 public override void Dra%*+
2 Console.Friteine*GIN& a ine.G+5 88
public class Circle : Dra%in!Ob(ect
2 public override void Dra%*+
2 Console.Friteine*GIN& a Circle.G+5 88
public class SJuare : Dra%in!Ob(ect
2 public override void Dra%*+
2 Console.Friteine*GIN& a SJuare.G+5 88 usin! Syste&5public class Dra%De&o
2 public static int dOb( ? ne% Dra%in!Ob(ect=>5
dOb(=@> ? ne% ine*+5
dOb(=4> ? ne% Circle*+5
dOb(=6> ? ne% SJuare*+5
dOb(=> ? ne% Dra%in!Ob(ect*+5
foreach *Dra%in!Ob(ect dra%Ob( in dOb(+
2 dra%Ob(.Dra%*+5 8
return @5 88
OUTPUT*
IN& a ine.
IN& a Circle.
IN& a SJuare.
IN& (ust a !eneric dra%in! ob(ect.
5C#AND .NET 10CS7612N3T 34
-
8/11/2019 c# and .net VTU
36/49
10//1
STATIC POL"MORPHISM
sa&e function rint$)is bein! used to print different data types
usin! Syste&5na&espace Poly&orphis&,pplication
2 class Printdata
2 void print*int i+
2 Console.Friteine*GPrintin! int: 2@8G- i +5 8
void print*double f+
2 Console.Friteine*GPrintin! float: 2@8G - f+5 8void print*strin! s+
2 Console.Friteine*GPrintin! strin!: 2@8G- s+5 8
static void ar!s+
2 Printdata p ? ne% Printdata*+5 33 Call print to print inte!er p.print*A+5
33 Call print to print float p.print*A@@.6B+5
33 Call print to print strin! p.print*G;ello CMMG+5 Console."eadey*+5 888
OUTPUT*
Printin! int: A
Printin! float: A@@.6B
Printin! strin!: ;ello CMM
56C#AND .NET 10CS7612N3T 34
-
8/11/2019 c# and .net VTU
37/49
10//1
ABSTRACT METHODS AND CLASSES
-
8/11/2019 c# and .net VTU
38/49
10//1
EAMPLE
public abstract class shape
2
public abstract void dra%*+5
8
,bstract ðods have no i&ple&entation Derived classes of the abstract class &ust i&ple&ent all the abstract
ðods
5C#AND .NET 10CS7612N3T 34
-
8/11/2019 c# and .net VTU
39/49
10//1
CASTING BETWEEN T"PES Castin! variables is not si&ple. , co&plicated set of rules resolves +a,t,
In so&e cases data is lost and the cast cannot be reversed In others an e9ception is provo$ed
Co&ple9 +%n!er,i%n,are often reJuired
Di..erent T4e, %. T4e Ca,tin2 %r T4e C%n!er,i%n
I/li+it +%n!er,i%n
is bein! done auto&atically by the co&piler and no data %ill be lost. It
includes conversion of a s&aller data type to a lar!er data types and
conversion of derived classes to base class. This is a safe type conversion
E6li+it +%n!er,i%n
E9plicit conversion is bein! done by usin! a cast operator. It includesconversion of lar!er data type to s&aller data type and conversion of base
class to derived classes. In this conversion infor&ation &i!ht be lost or
conversion &i!ht not be succeed for so&e reasons. This is an unsafe type
conversion
5-C#AND .NET 10CS7612N3T 34
-
8/11/2019 c# and .net VTU
40/49
-
8/11/2019 c# and .net VTU
41/49
10//1
EPLICIT CONVERSION
E9plicit castin! actually tells the co&piler that %e $no% about possible
infor&ation loss3&is&atch but still %e need to perfor& this cast
This is o$ for inbuilt nu&eric types but in case of reference types- there is a
possibility that the types are not at all co&patible i.e. castin! fro& one type
to another is not at all possible
or e9a&ple castin! a strin! GabcG to Inte!er is not possible
Such castin! e9pressions %ill co&pile successfully but they %ill fail at run
ti&e. Fhat C# co&piler does it that it chec$s %hether these t%o types are
cast co&patible or not and if not it raises an e9ceptionInvalidCastE9ception
1C#AND .NET 10CS7612N3T 34
-
8/11/2019 c# and .net VTU
42/49
10//1
lon! bi!7u& ? BABB533 E9plicit conversion
int s&allnu& ? *int+bi!7u&5
class 'ase
2
public int nu&4 2 !et5 set5 8
8
class Derived : 'ase2
public int nu&6 2 !et5 set5 8
8
class Pro!ra&
2static void
-
8/11/2019 c# and .net VTU
43/49
10//1
USER7DEFINED CONVERSION
)serdefined conversion is perfor&ed by usin! special ðods that you
can define to enable e9plicit and i&plicit conversions. It includes
conversion of class to struct or basic data type and struct to class or basic
data type. ,lso- all conversions ðods &ust be declared as static
5C#AND .NET 10CS7612N3T 34
-
8/11/2019 c# and .net VTU
44/49
10//1
UPCASTING AND DOWNCASTING
I&plicit conversion of derived classes to base class is called )pcastin! and E9plicit conversion of base class to derived classes is called Do%ncastin!
class 'ase2
public int nu&4 2 !et5 set5 8
8
class Derived : 'ase
2
public int nu&6 2 !et5 set5 8
8
class Pro!ra&
2
static void ar!s+
2
Derived d4 ? ne% Derived*+5
//Upcasting
'ase b4 ? d45
'ase b6 ? ne% 'ase*+5
//Downcasting
Derived d6 ? *Derived+b65
8
8
C#AND .NET 10CS7612N3T 34
-
8/11/2019 c# and .net VTU
45/49
10//1
8IS8 AND 8AS8 OPERATORS
%henever %e are usin! e9plicit casts- it is al%ays a !ood idea to
%rap the cast inside a trycatch bloc$. C# also
provides is and as operators %hich are helpful in perfor&in! e9plicit
casts in an e9ception safe &anner
The is operator chec$s %hether the type bein! casted fro& is
co&patible to the type bein! casted to and returns a boolean value
C#AND .NET 10CS7612N3T 34
-
8/11/2019 c# and .net VTU
46/49
10//1
EAMPLE
static void ar!s+
2C,SE 4
33 This %ill %or$ fine as o4 is actually an int
ob(ect
o4 ? 45
int i ? *int+o45
33 C,SE 6
33 This %ont %or$ because o6 is not an int so %e need
33 to have an is oprerator before the actual cast ob(ect o6 ? G4G5int (5
if *o6 is int+
2
( ? *int+o65
8
33 C,SE
33 Fe can never $no% %hat is the atual type of 33 an ob(ect at runti&e so its al%ays better to use
33 is operator- re%ritin! the first caseob(ect o ? 45
int $5
if *o is int+
2
$ ? *int+o5
8
8
6C#AND .NET 10CS7612N3T 34
-
8/11/2019 c# and .net VTU
47/49
10//1
Case 4 in the above code snippet %ill thro% an e9ception is o4 is
assi!ned to so&e type that is not int. the other 6 cases are e9ception
safe and %ill only perfor& the cast if the types are co&patible for
castin!
There is one s&all perfor&ance issue in usin! is operator. Case in
above code snippet %ill %or$ fine but it involves accessin! the ob(ect
t%ice. Once for chec$in! the co&patibility i.e. the is operator and
secondly to actually e9tractin! out the value i.e. castin!. can %e not
have soðin! li$e GChec$ the co&patibility and if co&patible
perfor& the castG in one sin!le operation
7C#AND .NET 10CS7612N3T 34
-
8/11/2019 c# and .net VTU
48/49
10//1
9UESTION BAN3
C#AND .NET 10CS7612N3T 34
1.Bre%y e8p9a% t$e to p%99ar! o' OOPS % C#,.;%t$ a e8amp9e) E8p9a% utator! ad Acce!!or!.5.;$at %! t$e ame o' ma!ter ode % Sy!tem object< E8p9a% %t$ ae8amp9e!. .E8p9a% 'our met$od parametermod%=er!) %t$ a e8amp9e.
. ;r%te a C# pro"ram to arra"e ame! % a!ced%" order. T$eame! are obta%ed 'rom t$e commad 9%e ar"umet!.
6.E8p9a% t$e 'uct%o! o' Sy!tem. Object c9a!!. >%?e o?err%dedde=%t%o 'or ToStr%"&( ad E@ua9!&(.
7. ;r%te a pro"ram % C# to read a jo""ed array ad d%!p9ay t$e !umo' a99 t$e e9emet! o' t$ree %er array
. E8p9a% Ca!t%" .;%t$ E8amp9e-. ;$at %! 3$er%tace % c# < E8p9a% %t$ !u%tab9e Pro"ram
10. E8p9a% et$od! Ab!tract %t$ c# Pro"ram
-
8/11/2019 c# and .net VTU
49/49
THAN3 "OU