You are on page 1of 157

C# d n a m m a r g o r P T E N .

i g n I 6 0 S 1 6 7


C# PR G O RAMM N I G N A D .NET

PA T R A

N U IT 1 6 Hou s r
e h T P p o s o l i h hy of .N : T E n U d t s r e a i d n ng the Previous S t a t e f o A f f ai s r , The N . ET
S i t u l o o , n The B i u lding Bl c o k of t e h .NET Platform (CLR,CTS, and CLS , ) T e h R e l o of
t e h . E N T e s a B C a l ss i L braries, What C# B i r ngs to the T , e l b a n A Ov v r e e i w f o N . ET
s e i r a n i B a ( ka Assembli s e ), e h t Role of t e h Co m m on e t n I r t a i d e m e Langu e g a , T e h
e l o R of E N . T T p y e M a t e d a t a , The e l o R f o t e h a s s embly Mani s a f , t Co p m ili g n I C L
to P o f t a l rm Sp c i f i c e I u r t s n c , s n o i t n U d t s r e a d n ing the Common Type S t s y em ,
I t n rin i s c CTS t a D a T p y s e , n U derstanding the C m o mon Lang a u ges Speci i f cati n o ,
U r e d n n a t s ding h t e Comm n o La g n uage u R ntime A r u o t of the .NET a N mespaces,
c n I r a e s n i g Yo r u Namespace Nomen l c a u t re, Deploying the .NET u R n i t me.

N U IT 2 6 Hours
i d l i u B ng C# p A pl c i at o i ns: The e l o R f o h t e Command L e n i Comp e i l r ( e . c s c x ) e ,
d l i u B ing # C p A c i l p ation using csc.exe o W rking wi h t . c s c exe e R sp n o se Fil s e ,
Gener t a ing Bug Reports, Remaini g n g C# Complier Options, T e h Comm n a d n i L e
b e D u r e g g c ( ordbg.ex ) e U i s ng t , e h V a u s i l studio .NET IDE, Ot e h r Key A p s ects of the
VS.NET IDE, C# Prepro e c s s or: Directives, an Interesting Aside: The Sy e t s m .
E i v n n o r me t n Class .

N U IT 3 8 H u o rs
# C n a L u g e g a Fu d n a e m t n als: T e h n A ato y m of c i s a B C# l C ass, a e r C ting o j b t c e : s
r o t c u r t s n o C B , s c i s a The Com o p t i s ion of a # C Application, f e D a l u t as i s gn e m nt an d
Var a i b e l c S ope, The # C e M mber Ini i t alisation Sy t n ax, Ba c i s u p n I t a d n u O tp t u wit h
h t e e l o s n o C , s s a l C U t s r e d n anding e u l a V Typ s e and Reference Typ s e , The Mast r e
N : e d o S s y tem, b O j t c e , T e h S s y tem Data T p y es n a ( d C# Al a i s , ) s e Co r e v n ting Betwe n e
Va u l e Typ s e n a d e R e f e r nce Typ s e : o B xing and b n U i x o g n , Defining P r g o r am
Cons n a t ts, C# Iteration Constructs, C# Cont l o r s Flow n o C st c u r ts, T e h Comp e t e l Set
of C# Oper t a ors, Defining C s u tom Class e M th d o s, U a t s r e d n ting Static e M th d o , s
M o h t e ds P r a a t e m er Modi i f s e , r r A ay Manipulat o i n in C#, S i r t g n Ma i n p i t a l u n o
in , # C # C u n E mera i t o s n , De n i n i f g St c u r u t r s e in C#, D i f e ning u C t s om a N e m sp c a s e .

I N U T 4 6 H u o rs
O t c e j b - r O e i nted Progr m a ming wi h t C#: o F rms Defining f o t e h C# Class, f e D i i n tio n
t e h Defa t l u P l b u ic n I t e c a f r e f o a Type, Re a c ppi g n h t e Pil a l rs of O O P, The First
P r a l l i s: # C s Enca s p ulation Services, Pse d u o- En a c ps l u ation: Cre g n i t a a e R d-Only
, s d l e i F T e h n o c e S d P l i : r a l s # C In n a t i r e h ce Supports, e k eping Fami y l Se r c ets: The
P e t c e t o r d Keyword, N s e ted Type Defini i t ons,The Third Pil a l r: C # s P l o ymorphic
S o p p u r , t a C sting Betwe n e .

PART B



e D pt. f o T I B J S , E S I e g a P 1
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

N U IT 5 6 H u o rs
c x E e i t p ons and Object i L fetim : e O e d to Errors, Bugs, and Exc i t p e ons, The R e l o o f
. E N T Ex p e c tion d n a H i g n , h t e System. Ex p e c t o i n s a B e s s a l C , Throwi g n a e G n i r e c
Ex t p e c i , n o h c t a C i g n Ex e c p i t o , n C R L S e t s y m e v e L l Excep i t ( n o System. System
E t p e c x ion), Custom l p p A ica i t on-L v e el c x E e ( n o i t p S s y tem. S t s y m e Ex , ) n o i t p e c
n i l d n a H g M t l u iple Excep i t on, T e h i m a F ly k c o l B , h t e t s a L Cha c n e e c x E p i t n o
D n y a l a c i m ly t n e d I ifying Ap o i t a c i l p n d n a S m e t s y Le l e v Excep i t on e D bugg n i g
System Exception s U i g n VS. E N T, n U d s r e tan i d ng b O j t c e e m i t e f i L , h t e CIT o f
n e w , T e h a B s c i s of Garbage Collection,, Finalization a T p y e, The Finalization
Pro e c ss, Building n a Ad o H c Destruct o i n M h t e od, Garbage Co l l ection Optim z i ations,
The Sys e t m. GC T e p y .

N U IT 6 6 H u o rs
e t n I f r ac s e n a d C l o lections: Defining n I e t rfaces Using C# Invoking I t n erface Members
at h t e object Level, Ex c r e i i s g n h t e Sha e p s e i H rar h c y, d n U ersta i d n ng E l p x icit
e t n I rf c a e Implementati n o , I t n erfaces s A Poly o m rphic Agent , s Buildi g n n I te f r ace
Hie a r rc i h es, Imple e m nting, m I l p emen i t a t on, In e t r a f ces Using VS .NE , T
n u d r e s a t nd n i g the IC n o e v rt b i le Interf c a e, Building a C s u tom En e m u r t a or ( n E I u e m r b a le
n a d n E u t a r e m or), u B ilding Clo b a e n le
s t c e j b o ) e l b a e n o l C I ( , d l i u B ing Comp a r a ble Objects ( I Compa a r l b e ), E p x o l ri g n
the syst m e . Co c e l l i t n o s Na , e c a p s e m B l i u ding a Cu t s om Container (R t e r f o itting the
Cars Ty e p ) .

N U IT 7 8 H u o rs
Ca a b l l ck I e t n r c a f es, Deleg t a es, a d n Eve t n s, Adv c n a ed c e T n h iques: n U d r e t s a d n ing
a C l k c a b l e t n I r , s e c a f Und n a t s r e d n i g e h t .N T E Delegate Type, Mem s r e b of System.
i t l u M c s a t , e t a g e l e D The Simp t s e l P s o i s ble t a g e l e D e Exa p m le, , B d l i u ing More a
a l E bo e t a r D e l e a g te Exampl , e U d n e t s r a d n ing s A ynchro u o n s Deleg e t a s, U d n e s r t n a di g n
a ( nd s U i g n ) v E ents .
T e h Ad s e c n a v Ke s d r o w y of C#, A a C talog of # C Ke r o w y ds Bui i d l ng a C s u to m
, r e x e d n I A n o i t a i r a V of t e h s r a C n I dex r e In e t r l a n e R p n e s e r tation of T p y e Indexer .
Usi g n C# In e d xer r f om VB .N T E . v O erlo i d a ng ope a r t r o s, The n I t r e nal R e r p e senta i t on
of a o l r e v O d n i g Opera o t rs, e t n i r n i t c a g i w th O e v l r oad p O erator o r f m v O er a o l ded-
O e p r t a r o - Ch l a le g n ed n a L u g g a s e , Cr t a e n i g u C stom C r e v n o si n o u o R tines, n i f e D in g
c i l p m I t i s r e v n o C i n o Routines, T e h In r e t a n l p e R resentat n o i s of u C stoms Con e v rsion
R u o e n i t s

I N U T 8 6 o H u s r
U r e d n st i d n a ng . E N T Assembles: r P oblems w t i h a l C ssic C M O n i B ar , s e i An v O e v r iew
of E N . T Assembly, u B ilding a Si p m le File Test Asse b m ly, A C#. i l C n e t A p p lication,
A Vi u s l a a B sic .NET e i l C nt p A a c i l p i t on, r C o s s Language I h n e i r tance, E p x l i r o ng the
a C r i L brary , s Manife t s , x E plo n i r g h t e a C r i L b a r rys T e p y s, d l i u B i g n t e h M l u ti i f le
m e s s A bl , y U i s ng Asse b m l , y Underst n a din g
P e t a v i r Asse i l b m e , s Pro g n i b for P a v i r te s s A emb i l es (T e h Basics), Pr a v i te Assemblies
XML g i f n o C r u t a ions i F e l s, i b o r P ng r o f r P iva e t Ass b m e lies ( T e h e D t ) s l i a ,
n U d n a t s r e d n i g S e r a h d Assembly, n U d s r e ta d n ing S a h e r d Names, Build n i g a

e D pt. f o T I B J S , E S I e g a P 2
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

e r a h S d Assembly, U e d n rs a t n n i d g D a l e y Signing, Installing/Removing Shared
As e s m l b , y Using a S a h red Assembly

Text k o o B : s
1. An r d ew T : n e s l e o r P o r C# wi h t E N . T 3. , 0 4
th
Edi i t o , n Wiley In i d a, 2009.
Ch r e t p a s: 1 to 11 (up o t pp. 9 6 3 )
2. E. u r u g a l a B samy: P g n i m m a r g o r in , # C 2
d n
Ed i t i on, T a t a M G c raw Hill, 20 4 0 .
(Pro g n i m m a r g Examples . 3 , 7 . 3 1 , 0 5 . 5 , . 6 1, 7 , 2 . 7.4, 7 5 . , , 6 . 7 , 1 . 8 , 2 . 8 8.3, 8 , 5 . . 8 7 ,
8 , 8 . . 9 1, 9 , 2 . . 9 , 3 . 9 4, 10.2, 10 , 4 . 11.2, . 1 1 4, 1 1 . 2 , , 4 . 2 1 12 , 5 . 12.6, . 3 1 1, 13 2 . , 13.3 ,
. 3 1 , 6 14.1, 14.2, 14.4, 15.2, 15 , 3 . 6 1 .1, 16.2, 16.3, 18.3, 18.5.18.6)

f e R ere c n e Book : s
1. Tom Archer: Inside # C , P W P l b u is e h r , s 0 2 1 0 .
2. He b r ert S t d l i h c : # C The Com l p t e e Re e f r n e c , e T a t a Mc r G aw Hill, 2 0 0 4.



































e D pt. f o T I B J S , E S I e g a P 3
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7


t n e t n o C f o e l b a T n e g a P o
T I N U -1 e h T h p o s o l i p y h of N . T E 1- 9 1
1 . 1 i d n a t s r e d n U ng e h t s u o i v e r P e t a t S f o r i a f f A s
2 . 1 e h T T E N . S t u l o i , n o
3 . 1 e h T l i u B i d ng k c o l B of e h t T E N . m r o f t a l P (C , S T C , R L d n a CLS)
4 . 1 e h T e l o R f o E N . e h t T e s a B s s a l C b i L r r a e i
5 . 1 t a h W # C s g n i r B o t the b a T e l
6 . 1 n A w e i v r e v O f o T E N . e i r a n i B s
7 . 1 e h T e l o R f o e h t n o m m o C e t a i d e m r e t n I a L ngu e g a
8 . 1 e h T e l o R T E N . f o T e p y e M t t a d a a
9 . 1 T e h e l o R f o b m e s s A e h t ly n a M i s e f t
0 1 . 1 g n i l i p m o C L I C o t m r o f t a l P c i f i c e p S r t s n I u s n o i t c
1 1 . 1 g n i d n a t s r e d n U e h t n o m m o C e p y T S m e t s y
2 1 . 1 c i s n i r t n I a t a D S T C , s e p y T
3 1 . 1 g n i d n a t s r e d n U e h t n o m m o C a L n s e g a u g n o i t a c i f i c e p S ,
4 1 . 1 g n i d n a t s r e d n U e h t n o m m o C a L n a u g e g e m i t n u R
5 1 . 1 A r u o t f o e h t T E N . s e c a p s e m a N ,


T I N U -2 Bu l i d n i g C# App c i l a o i t ns 1 2 -30
1 . 2 e h T e l o R f o e h t d n a m m o C e n i L r e i l p m o C c ( c s e . x ) e
2 . 2 g n i d l i u B C # n o i t a c i l p p A g n i s u e . c s c xe
3 . 2 g n i k r o W h t i w e x e . c s c e s n o p s e R , s e l i F G g n i t a r e n e Bug
s t r o p e R
4 . 2 e h T d n a m m o C e n i L r e g g u b e D ) e x e . g b d r o c (
5 . 2 g n i s U , e h t l a u s i V t S u o i d T E N . E D I 6
6 . 2 r e h t O y e K s t c e p s A f o e h t T E N . S V E D I
7 . 2 : r o s s e c o r p e r P # C e v i t c e r i D s
8 . 2 n A g n i t s e r e t n I e d i s A : e h T S m e t s y . t n e m n o r i v n E l C s s a .

T I N U -3 e h T # C o r P gra g n i m m e g a u g n a L 1 3 - 6 6
1 . 3 e h T o t a n A my f o a # C c i s a B C a l s s
2 . 3 g n i t a e r C b o j : s t c e r o t c u r t s n o C c i s a B s
3 . 3 e h T s o p m o C t i n o i f o a C# p p A i l t a c i n o
4 . 3 t l u a f e D t n e m n g i s s A d n a e l b a i r a V e p o c S
5 . 3 e h T # C r e b m e M t i n I i n o i t a z i l a S a t n y x
6 . 3 c i s a B t u p n I d n a t u O p t u h t i w e h t C e l o s n o l C s s a
7 . 3 g n i d n a t s r e d n U e u l a V s e p y T d n a e c n e r e f e R T s e p y
8 . 3 e h T r e t s a M o N d : e y S s e t m, j b O e t c
9 . 3 e h T S m e t s y D a t a T A # C d n a ( s e p y i l e s a ) s
0 1 . 3 g n i t r e v n o C n e e w t e B e u l a V T s e p y d n a e c n e r e f e R T : s e p y
o B x g n i d n a o b n U x g n i ,

e D pt. f o T I B J S , E S I e g a P 4
C# d n a r P T E N . m m a r g o i g n I 6 0 S 1 6 7

1 1 . 3 i n i f e D ng m a r g o r P C t n a t s n o s
2 1 . 3 # C n o i t a r e t I s n o C t t c u r s
3 1 . 3 # C s l o r t n o C w o l F C n o t s t c u r s
4 1 . 3 e h T e t e l p m o C S t e f o C
5 1 . 3 s d o h t e M r e t e m a r a P s e i f i d o M
6 1 . 3 r r A a y M a l u p i n a i t n o n i C #
7 1 . 3 g n i r t S n o i t a l u p i n a M in C#
8 1 . 3 # C r e m u n E s n o i t a
9 1 . 3 i n i f e D ng s e r u t c u r t S in C #
0 2 . 3 i n i f e D ng m o t s u C a N m a p s e c s e .
UNIT 4 O O P with C# 67-78
1 . 4 i n i f e D ng f o e h t # C C , s s a l
2 . 4 n o i t i n i f e D e h t t l u a f e D c i l b u P e c a f r e t n I f o a T e p y
3 . 4 i p p a c e R ng i P e h t l l s r a f o O O P ,
4 . 4 e h T t s r i F : s r a l l i P s # C c n E u s p a l t a i n o S s e c i v r e , P o d u e s -
: n o i t a l u s p a c n E g n i t a e r C d a e R - y l n O s d l e i F
e h T 5 . 4 d n o c e S s # C : r a l l i P n I h e c n a t i r e s t r o p p u S
. 4 6 g n i p e e k y l i m a F : s t e r c e S e h T c e t o r P t d e Ke , d r o w y
d e t s e N T e p y D , s n o i t i n i f e
. 4 7 e h T d r i h T : r a l l i P C s # Pol c i h p r o m y S r o p p u t ,
8 . 4 C g n i t s a n e e w t e B .

UNIT 5 E p e c x t o i ns and O e j b ct Li t e f ime 79- 0 9
e d O 1 . 5 o t , s r o r r E Bu , s g d n a Ex , s n o i t p e c
2 . 5 e h T e l o R f o T E N . Ex n o i t p e c i d n a H n , g
e h T 3 . 5 S m e t s y . Ex n o i t p e c e s a B s s a l C ,
4 . 5 g n i w o r h T a c i r e n e G , n o i t p e c x E
5 . 5 g n i h c t a C Ex n o i t p e c ,
6 . 5 C R L Sy m e t s l e v e L n o i t p e c x E (S m e t s y . S s y t m e Ex , ) n o i t p e c
7 . 5 m o t s u C n o i t a c i l p p A - l e v e L Ex n o i t p e c (S e t s y m. m e t s y S
Ex , ) n o i t p e c
8 . 5 g n i l d n a H e l p i t l u M n o i t p e c x E
9 . 5 e h T i n a F ly , k c o l B e h t t s a L a h C n e c Ex n o i t p e c Dyn l a c i m a ly
g n i y f i t n e d I n o i t a c i l p p A d n a S m e t s y e v e L l n o i t p e c x E
g n i g g u b e D m e t s y S
0 1 . 5 Ex n o i t p e c g n i s U VS. , T E N
1 1 . 5 g n i d n a t s r e d n U t c e j b O , e m i t e f i L
2 1 . 5 e h T C f o T I n w e ,
3 1 . 5 e h T s c i s a B f o G e g a b r a n o i t c e l l o C ,
4 1 . 5 n o i t a z i l a n i F a Type, e h T n o i t a z i l a n i F P s e c o r s,
5 1 . 5 g n i d l i u B n a d A c o H n o i t c u r t s e D h t e M , d o
6 1 . 5 e g a b r a G n o i t c e l l o C , s n o i t a z i m i t p O
7 1 . 5 e h T S m e t s y . C G T e p y .



e D pt. f o T I B J S , E S I e g a P 5
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7


UNIT 6 s e c a f r e t n I and s n o i t c e l l o C 1 9 - 2 0 1

1 . 6 g n i n i f e D a f r e t n I c s e g n i s U # C
2 . 6 g n i k o v n I n I t e c a f r e s r e b m e M t a e h t t c e j b o , l e v e L
3 . 6 Ex g n i s i c r e e h t s e p a h S c r a r e i H hy
4 . 6 g n i d n a t s r e d n U t i c i l p x E e c a f r e t n I , n o i t a t n e m e l p m I
5 . 6 s e c a f r e t n I s A o P l c i h p r o m y A , s t n e g
6 . 6 g n i d l i u B e c a f r e t n I s e i h c r a r e i H , n o i t a t n e m e l p m I
7 . 6 s e c a f r e t n I g n i s U S V i d n a t s r e d n u , T E N . ng e h t e l b i t r e v n o C I n I t r e f e c a ,
8 . 6 g n i d l i u B a m o t s u C r o t a r e m u n E ( e m u n E I r e l b a d n a o t a r e m u n E r , )
9 . 6 g n i d l i u B e n o l C b a le s t c e j b o ( , ) e l b a e n o l C I
0 1 . 6 g n i d l i u B e l b a r a p m o C s t c e j b O I a p m o C r e l b a
1 1 . 6 g n i r o l p x E e h t s m e t s y . s n o i t c e l l o C e c a p s e m a N
2 1 . 6 g n i d l i u B a m o t s u C r e n i a t n o C ( g n i t t i f o r t e R the r a C s T ) e p y .


UN T I -7 k c a b l l a C , s e c a f r e t n I g e l e D a , s e t a d n E e v n s t 3 0 1 - 4 1 1
g n i d n a t s r e d n U 1 . 7 k c a b l l a C , s e c a f r e t n I
g n i d n a t s r e d n U 2 . 7 e h t T E N . e t a g e l e D , e p y T
3 . 7 s r e b m e M f o S m e t s y . t s a c i t l u M , e t a g e l e D The t s e l p m i S s s o P i e l b
e t a g e l e D , e l p m a x E g n i d l i u B e r o M a e t a r o b a l E e t a g e l e D e l p m a x E
4 . 7 g n i d n a t s r e d n U As s u o n o r h c n y , s e t a g e l e D
5 . 7 i d n a t s r e d n U ng d n a ( ) g n i s U Ev n e ts
6 . 7 e h T a v d A n s e c Keyw s d r o f o , # C A l a t a C og f o # C y e K w d r o s
7 . 7 g n i d l i u B a m o t s u C In e d x , r e A n o i t a i r a V f o e h t s r a C e d n I x r e t n I e l a n r
n o i t a t n e s e r p e R f o e p y T r e x e d n I
8 . 7 g n i s U # C e d n I x r e r f om B V T E N . . g n i d a o l r e v O , s r o t a r e p o e h T r e t n I n l a
n o i t a t n e s e r p e R f o g n i d a o l r e v O s r o t a r e p O , e h T l a n r e t n I e r p e R s s n o i t a t n e
f o s m o t s u C n o i s r e v n o C R t u o i s e n

UNIT -8 d n U erstanding e h t o F rm t a f o a . E N T Assem l b y 1 1 5-1 0 2

1 . 8 s m e l b o r P h t i w c i s s a l C M O C r a n i B i s e ,
2 . 8 n A w e i v r e v O f o T E N . b m e s s A l , y
3 . 8 g n i d l i u B a e l p m i S i F le t s e T b m e s s A ly
4 . 8 A # C l C i t n e p p A i l t a c i , n o
A 5 . 8 l a u s i V c i s a B T E N . t n e i l C , n o i t a c i l p p A g n i r o l p x E e h t Car a r b i L r s y
t s e f i n a M
6 . 8 i r o l p x E g n e h t r a C a r b i L r s y T e p y s






e D pt. f o T I B J S , E S I e g a P 6








T I N U -1

T I N U -1 e h T o s o l i h p p y h of N . ET
1 . 1 i d n a t s r e d n U ng e h t s u o i v e r P e t a t S f o f A f r i a s
2 . 1 e h T T E N . S u l o i t , n o
3 . 1 e h T i d l i u B ng k c o l B of e h t T E N . m r o f t a l P (C , S T C , R L d n a CLS)
4 . 1 h T e e l o R f o e h t E N . T e s a B s s a l C b i L r i r a e
5 . 1 W t a h # C s g n i r B o t the b a T e l
6 . 1 n A w e i v r e v O f o N . E r a n i B T ies
7 . 1 e h T e l o R f o e h t n o m m o C e t a i d e m r e t n I a L ngu e g a
8 . 1 e h T e l o R T E N . f o T e p y e M t t a d a a
9 . 1 e h T e l o R f o e h t b m e s s A ly n a M i s e f t
0 1 . 1 g n i l i p m o C L I C o t l P m r o f t a c i f i c e p S r t s n I u s n o i t c
1 1 . 1 g n i d n a t s r e d n U e h t n o m m o C e p y T S e t s y m
2 1 . 1 c i s n i r t n I S T C a t a D , s e p y T
3 1 . 1 g n i d n a t s r e d n U e h t n o m m o C a L n s e g a u g n o i t a c i f i c e p S ,
4 1 . 1 g n i d n a t s r e d n U e h t n o m m o C a L n e g a u g e m i t n u R
5 1 . 1 A r u o t f o e h t T E N . e c a p s e m a N s,

















e D pt. f o T I B J S , E S I e g a P 7
C# d n a m m a r g o r P T E N . i g n










g n i c u d o r t n I # C a d n th e
I 6 0 S 6 7 1










. T E N m r o f t a l P
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

1.1 U d n ersta g n i d n the r P v e i u o s t S ate f o Affairs
e r o f e B g n i n i m a x e e h t s c i f i c e p s f o e h t T E N . u v i n e s r e , its p l e h f l u o t n o c s e d i r m o s e of
e h t s e u s s i t a h t mo i t vated h t e genesis of Microso t f s cu r r n e t l p atform. To e g t in e h t
p o r e p r i m nd- e s , t le t s g e b in this t p a h c er h t i w a b i r ef and pa e l n i ss s i h tory e l sson o t
m e r e b m er r u o s t o o r a d n d n u e d n a t s r e h t li i m a t i t n o s of h t e previo s u state of aff i a s r
e t f a ( r a , l l d a t t i m n i g you have a problem is the first s e t p to a w rd fin i d g n a o s lu i t o ) n .
Af e t r c m o l p e i t ng this quick t u o r of l e f i as e w knew it, we u t rn u o r atten i t on to t e h
u n m r e ous e b nef t i s provi d e d by C# and t e h .NET l p atform.
L e f i s A a 2 3 n i W / C I P A r g o r P a r e m m
n o i t i d a r T a y l l g n i k a e p s , de g n i p o l e v o s ftw r a e o f r e h t w o d n i W s a f m l i y of e p o rat n i g
s m e t s y s d e v l o v n i g n i s u e h t C g n i m m a r g o r p g a u g n a l e in conj c n u i t n o i w th t e h
w o d n i W s c i l p p a a n o i t r g o r p a g n i m m i t n erface A ( P ) I . Wh l i e t i is true t a h t u n m r e o s u
p p a c i l a i t s n o e v a h b e e n su c c ess u f l l y cr a e t d e s u n i g i h t s i t me-honor d e p a p o r a h c , few f o
s u w u o d l di a s g e r e t a h t u b l i ding a p p i l ca o i t ns s u i g n the raw API is a o c m l p e x
d n u e n i k a t r g.
e h T t s r i f u o i v b o s b o r p e l m s i h t t a C s i a e v ry te s r e la u g n a e g . C deve o l pers a e r fo d e c r to
n o c t d n e h t i w l a u n a m me y r o m g a n a m e n e m t, g u ly pointer a t i r hmetic, and ug y l
a t n y s c c i t al c s n o t t c u r s . r u F ther o m re, gi e v n t a h t C is a structu d e r l n a g a u ge, t i l c a ks t e h
e b nef t i s pr v o i e d d y b the obj c e t- r o i n e t d e app a o r ch c ( an any n o e a s y spaghe t t i o c de?)
h W en you o c mb n i e the t o h u n a s ds of l g obal c n u f i t ons and data types de i f ned by the
Win 2 3 P A I to an already for i m dable langu g a e, it is l t t i le w n o der that h t ere a e r so m y n a
g g u b y p p a i l ca i t n o s fl a o n i t g ar u o nd today.
e f i L s A a C F M / + + C r g o r P a r e m m
n O e a v st m i o r p e v m n e t e v o r a r w C A / PI develop t n e m is the use of the + C +
pr g o a r i m m ng a l nguage. n I many w y a s, C + + a c n be thou h g t of as an b o j c e t- r o iented
e y a l r on t p o f o C. , s u h T even th u o gh C + + o r p grammers e b nef t i r f om the fam d e
pillars of OO P (encapsulation, inher t i ance, d n a y l o p morphis , ) m t e h y are sti l l at the
c r e m y of h t e pa n i ful as e p cts of the C language (e.g , . ma u n al memory ma g a n eme t n ,
y l g u r e t n i o p a , c i t e m h t i r d n a u y l g s n y t i t c a cal n o c r t s t c u s). D p s e t i e its c l p m o x e t i y,
m n a y + C + frameworks exist to a d y. For ex p m a le, the Micr s o oft F o i t a d n u o n C a l s e s s
) C F M ( s e d i v o r p h t e deve e p o l r w t i h a set of C + + c a l s s es t a h t i c a f li a t te e h t o c nst c u r i t n o
of Wi 2 3 n app i l a c i t n o s. e h T main r l o e f o M C F s i to w p a r a a s ne subs t e of the raw
Wi 2 3 n API b h e n i d a u n m r e b of c a l s s e , s m g a ic ma r c os, and num s u o r e o c de- n e g e a r i t o n
t o o ls (aka z i w ards). s s e l d r a g e R f o e h t l u f p l e h e c n a t s i s s a d e r e f f o e h t y b C F M k r o w e m a r f
s a ( l l e w s a y n a m oth r e + + C - s a b ed win o d wi g n toolki s t ), the fact of the mat e t r is t a h t
+ C + r p ogra i m m ng remains a i d ffi u c lt a d n e r r or-p o r ne e e p x r e i nc , e given its his o t rical
o r ots in C.

L e f i s A a u s i V al Ba i s c 6.0 Programmer
e u D o t a heartfelt e d sire to e j n oy a simp e l r life t s yle, many progra m m e s r ha e v h s if e t d
a w a y f m o r the o w rld of C( ) + + -based f a r me o w s k r to i k nder, ge l t n er lan a u g g s e such s a
l a u s i V B c i s a . 6 0 (VB6). V 6 B is popular e u d to t i s b a t i l i y to u b l i d com l p ex use r
interfac s e , code i l b a r r e i s (e.g , . COM e s rv r e , ) s and data a c c e s s l g o ic w t i h i m nimal u f ss
d n a b t o her. Even o m e r than MF , C VB6 i h d s e t e h complex t i i s e of t e h raw Win32 AP I
e D pt. f o T I B J S , E S I e g a P 8
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

from i v ew using a num e b r of integr t a ed code wizards, intri s n ic a d ta t p y es, class s e , d n a
B V - c i f i c e p s f n u t c s n o i .
e h T r o j a m down l l a f of VB6 (w c i h h has b n e e re i t c fi d e i g ven the advent of i V a u s l c i s a B
. ) T E N s i t a h t t i s i not a l l u f y t c e j b o - e i r o nted l u g n a age; r h t a r e , it s i e j b o ct a a w r . e o F r
x e a e l p m , V 6 B s e o d not a l l ow the prog a r mmer to esta l b ish is-a e r la o i t ns p i h s e b tw n e e
t p y es (i.e , . o n c a l s s ical i h n er t i ance) and h s a no in i r t nsic sup t r o p f r o paramete i r zed
s a l c s construct o i n. Mo r e v o e r , B V 6 do n s e t pr v o i e d the b a il t i y to bu l i d multithr a e ded
p p a i l c t a i s n o unl s s e you are willing to d p o r down to l w o -l e v e l Wi 2 3 n P A I a c lls
h c i h w ( s i co x e l p m at b s e t a d n e g n a d r s u o at wor t s ).

L e f i s A a E E 2 J / a v a J r g o r P a e m m r
r e t n E J a v a . e h T v a J a pr r g o a n i m m g g a u g n a l e is a ( o m l s ) t m o c p l e t e l y e j b o ct or e i n d e t n a d
s a h s t i syn a t c i t c ro t o s in C++. As a m ny f o you e r a a , e r a w s a v a J s e r t n t g hs a e r f r a gr a e ter
n a h t s t i s p u po t r for pla f t orm in e d pen e d n e c . J a v a (as a lan u g a e g ) cle n a s p u many
s n u a o v ry syn a t ctical as e p cts of + C +. a v a J a ( s a l p a o f t r ) m prov d i es pr r g o a e m m rs t i w h a
g r a l e u n mb r e of r p e d e n i f e d s e g a k c a p t a h t c n i a t n o a v r s u o i ty e p e d finitions . s U i g n the e s
y t p s e , a v a J r p o r g a m m e s r e r a a e l b o t u b i d l 1 0 % 0 Pure Ja a v app t a c i l io s n c l p m o ete with
da a b a t se c n n o i t c e v y t i , mes g a s n i g s o p p u t r , web-ena l b ed n o r f t end , s n a d a rich user
t n i erface. h g u o h t l A Ja a v s i a y r e v ele t n a g lan g a u g e, n o e t o p enti l a pr m e l b o s i h t at us g n i
a v a J y l l a c i p y t e m a s n t a h t u o y t s u m e s u v a J a t n o r f - o t - a b ck during t e h de e v lopme t n
c y c le. In eff c e t, Java f o fers l t t i le hope of langu g a e integration, as this g e o s aga n i st h t e
r g ain of Ja a v s pr r a m i y goal (a single pr g o ra i m m ng lan u g age for e e v ry e n ed). I n
, y t i l a e r ho , r e v e w e r e h t e r a s n o i l l i m f o n i l s e of i x e s n i t g d o c e u o t e h t e r in the o w rld tha t
o w u d l i e d ally like to o c m m ingle with ne e w r v a J a c d o e. , y l d a S a v a J s e k a m t s i h a t sk
m e l b o r p a i t c. Pur e J a v a i i s s mpl p p a t o n y r a i r p o t r o f e m g y n a r h p a i a c lly o u n r m r e ically
e v i s n e t n i s n o i t a c i l p p a e s e h , s e s a c u o y y a m f d n i J s a v a n o i t u c e x e p s e d e s e v a e l
some h t ing to be e d sired) . A b t e ter appr h c a o f r o su h c programs would e b to e s u a
lo e w r-level l g a u g n a e ( u s h c s a C++) w e h re appro r p iate. l A as, w i h e l Java do s e provide
a li i m t d e ab i l i ty to a c c ess non-Ja a v AP s I , there s i l t i tle su p p o t r for true s o r c s- a l n u g a e g
o i t a r g e t n i n.

L e f i s A a COM r g o r P a e m m r
T e h Com n e n o p t Object M e d o l (COM) w s a i M r c s o oft s pr v e ious l p p a ica i t o n
e v e d lopment framework. O C M is n a arch t i c e ture t a h t says in effe , t c f I you bu l i d your
class s e in c c a ord n a ce w t i h t e h u r les of C M O , o y u n e d up with a block of u e r s b a le
binary c d o e.
T e h be u a ty of a bi a n ry COM server is h t at t i c n a be c c a e s s ed in a language-
i d n ep t n e d n e mann r e . , s u h T + C + m m a r g o r p e s r n a c l i u b d M O C cl s a ses h t at can be u e s d
by VB6 . e D h p l i r g o r p a m m ers c n a u e s COM classes bu t l i usi g n C, and so forth .
r e v e w o H , s a o y u y a m e b a a w , e r O C M s lang g a u e d n i epe d n ence s i so h w e m at m i l ited.
For example, the e r s i no y a w to deri e v a new C M O cla s s using
n a n i t s i x e g O C M a l c ss ( s a C M O a h s no s p u port for c a l s s l a c i h n i er t i n a ce). Rath r e , u o y
m s u t m e k a use of the more u c m e b r o s me a h s-a rela i t ons p i h to r u e se COM class
t p y es.
A o n t r e h b n e ef t i of COM is t i s loca i t n o -t a r nspa n e r t a n ture. Us g n i c u r t s n o c s t s h c u as
c i l p p a ati n o i n e d tifiers (AppID ) s , stub , s prox e i s, n a d t e h COM n u r m i t e n e i v ronm n e t,

e D pt. f o T I B J S , E S I e g a P 9
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

p o r g a r mme s r a c n v a i o d h t e e e n d to o w rk w t i h r w a s s t e k c o , RPC a c lls, n a d e h t o r w o l -
v e l el de a t ils. For example, consider the f o l l o wi g n
VB6 COM client c d o e:
' s i h T k c o l b f o 6 B V e d o c n a c e t a v i t c a a M O C s s a l c n e t t i r w n i
' y n a M O C - e r a w a , e g a u g n a l h c i h w y a m e b d e t a c o l e r e h w y n a
' n o e h t k r o w t e n g n i d u l c n i ( r u o y l a c o l ) e n i h c a m .
m i D c s a s s a l C M O C y M
t e S c = w e N s s a l C M O C y M ' n o i t a c o L d e v l o s e r g n i s u . D I p p A
k r o W e m o S o D . c
h g u o h t l A M O C n a c be d e r e d i s n o c a y r e v s e c c u s f s l u t c e j b o e d o m l, it is e m e r t x e y l
x e l p m o c e d n u r t e h h d o o (at least n u l i t you ha e v spent m n a y months exploring t i s
plumbi g n e p s ecially if you h p a pen to e b a C + + p o r gra m m e ) r . o T e h lp simp i l fy h t e
e v e d l p o ment f o COM bi a n ries, nu r e m o s u COM- w a r a e r f o w e m a r s k ha e v c m o e in o t
e t s i x e nce. For exam l p e, t e h c A i t ve e T m t a l p e Li r b ary (ATL) pr s e d i v o a e h t o n r set of + + C
, s e s s a l c m e t p e t a l s, d n a c a m r s o o t e s a e e h t crea o i t n of O C M s e p y t . a M y n h t o r e
s e g a u g n a l o s l a e d i h a d o o g t r a p f o e h t M O C e r u t c u r t s a r f n i m o r f w e i v . Ho e v e w r e g a u g n a l ,
t r o p p u s a e n o l s i t o n h g u o n e o t e d i h e h t c y t i x e l p m o of M O C . n e v E n e h w o y u ch o o se a
y l e v i t a l e r m i s ply O C M-a a w re l g n a ua e g such s a VB6, you are still f r o d e c to contend
with fragile regis r t ation n e tries d n a numer u o s deploym n e t-relat d e i s s ues (collectivel y
e t rmed D L L l l e h ).

L e f i s A a s w o d n i W A N D o r P grammer
o T r e h t r u f e t a c i l p m o c , s r e t t a m e r e h t s i a e l t t i l g n i h t d e l l a c e h t t e n r e t n I . r e v O h t e a l t s
l a r e v e s e y r a , s Mc i ro f o s t a h s been d d a n i g more Inter e n t-aw r a e features into t i s fa l i m y of
e p o ra n i t g s s y tems d n a r p odu t c s. Sad , y l b i d l i u ng a b e w ap l p ic i t a on u i s ng O C M-b s a e d
Win s w o d i D str b i uted e t n i rNet p A p n o i t a c i l s e t i h c r A c u t re (DNA) is also u q i e t co l p m ex.
Some of this complexity is due to h t e s m i ple a f ct that Win o d s w DNA re u q r i es h t e s u e
f o nu e m r u o s tec l o n h g o ies and la g n uages A ( SP, HTML, XM , L JavaScri , t p B V Scri t p , and
COM(+), as well as a d a t a c a cess API u s ch as ) O D A . One problem is t a h t m y n a of h t e e s
tec n h ol g o ies are o c mpletely n u rela e t d r f om a syntactic point of view . o F r exa l p m e,
a J v S a c i r pt has a synt x a u m ch i l ke C, whi e l V S B c t p i r is a s s b u et of V 6 B . T e h O C M
r e v r e s s h t at are r c a e ted o t run u e d n r the O C M+ ru i t n me have an en i t rely d f i f r e ent look
d n a e e f l from the S A P pages t a h t invoke th m e . h T e e r s l u t is a i h l h g y c n o f d e s u mishmash
f o s e i g o l o n h c e t .
r e h t r u F m , e r o a d n perh s p a e r o m , t n a t r o p m i e h c a la g a u g n e / d n a or te n h c olo y g a h s its
n w o ty e p t s y s em ( h t t a y a m o o l k t o n h g n i k i l e e h t o n a s r e p y t system). n A int i n
Ja a v S r c i t p s i o n t quite the same as a n Integer in B V 6.

1.2 The .NET S l o u i t on
So much for the br e i f i h sto y r l s e son. T e h bo m o t t n i l e is t a h t i l fe as a Wi d n ow s
p o r g a r mmer h s a be n e u o t gh. h T e .NET Fram o w e rk is a rat e h r radical and bru e t -fo c r e
p p a r a o ch to making u o r l v i s e easier. h T e solution pro o p sed by E N . T is C hange
r e v e yth g n i y r r o s ( , y u o c t n a blame h t e s e m s e g n e r for the mess e g a ). s A you l l i w e e s
g n i r u d e h t re r e d n i a m f o this b o o , k t e h N . ET F a r mework is a co t e l p m l e y new m e d o l for
u b n i d l i g y s stems on the W n i dows fa i m ly of p o r e ati g n s s y tems, s a we l l as o n
num s u o r e n o n - c i M r o s o ft e p o a r g n i t y s ste s m such s a a M c OS X and a v r s u o i Un / x i L u n i x

e D pt. f o T I B J S , E S I e g a P 0 1






















*.dll o t
e r p vious bullet points, t e h E N . T pla o f t rm a h s not i h ng o t o d with O C M ( e b yond t e h
a f ct that both f a r m w e s k r o originat d e from Micros f o t). In fac , t the n o y l way .NET and
O C M e p y t s can intera t c with each t o e h r is using the in e t r e p o r i b a l t i y a l y . r e

1.3 n I tr u d o c g n i the B n i d l i u g B o l s k c of the . E N T
Platfo m r (the CLR, CTS, and CL ) S
w o N t a h t u o y w o n k so e m f o e h t t i f e n e b s d e d i v o r p y b N . E , T t e l s w e i v e r p h t r e e key ( d n a
r r e t n i ) d e t a l e e i t i t n s e h t t a ma e k t i l l a p i s s o l b : e the L C , R C , S T a d n CLS. From a
r p o r g am e m r s po n i t of i v e , w .NET a c n be und r e stood as a new n u r i t e m n e vironme t n
d n a a c m o p e v i s n e h e r e s a b s a l c s i l bra y r . The run- i t me l y a er is p o r p l r e y refe r r ed to as
the c n o m m o a l u g n age t n u r i e m , or CLR. h T e primary role of the CLR is to lo a c t , e load,
and man e g a .NET types on your behalf. The CLR also a t kes care of a u n mber of o l w-
level deta s l i c u s h as memory ma g a n em n e t d n a perfor i m g n c e s ur t i y ch c e ks. n A ot e h r
u b ilding bl c o k f o the .NET plat o f rm is the Comm n o Ty e p y S stem, or CTS. The CTS
n o i t a c i f i c e p s fu y l l e d s s e b i r c a l l e l b i s s o p a t a d ty e p s and programm g n i c n o s t c u r t s
t r o p p u s ed y b t e h runt e m i , spec f i ies w o h the e s e t n i i t es a c n t n i er t c a h t i w ea h c h t o e , r
a d n deta s l i w o h h t ey a e r represe t n ed n i e h t E N . T me d a t a a t fo a m r t (more inf r o a m tion
n o d a t e m a a t t a l er n i s i h t h c a ) r e t p .
r e d n U s a t nd that a i g ven N . T E -a a w re a l n u g g a e i m ght not su o p p rt each and e e v ry
r u t a e f e def n i d e y b the S T C . The Common L n a gu g a e Spe i c i f cation (CLS) is a rela e t d
e p s cif c i a i t on that e d fin s e a s b u e s t of co m m n o types and p o r gra i m m ng c n o structs h t at
a l l .NET progra i m m ng la g n uages a c n agree on. Thus, if y u o build .NET types th t a onl y
x e p e s o C S L -comp i l ant a e f t r u e , s you can re t s s a sured that all .NET-a r a w e lang a u g s e can
c e m u s n o h t em . , y l e s r e v n o C f i u o y e k a m e s u f o a a t a d e p y t r o g n i m m a r g o r p t c u r t s n o c
t a h t is d i s t u o e f o e h t s d n u o b f o h t e CLS, you a c nnot u g a a r n e t e h t at v e y r e N . ET
m a r g o r p - g n i m e g a u g n a l n a c in t c a r e t t i w h r u o y T E N . e d o c y r a r b i l .

e D pt. f o T I B J S , E S I e g a P 11
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

n o i t u b i r t s i d s. o T t e s h t e sta , e g r e h e is a u q k c i r n u d w o n f o o s me r o c e fe t a e r u s pr v o ided
u o c r e t sy of .NET:
l l u F o r e t n i p i l i b a r e ty i wth ex i t s i ng o c de h T : is s i (of c u o rse) a go d o th g n i . s i x E i t g n CO M
n i b a i r s e c n a co i m m n e l g (i.e , . inter p o ) i w th ne e w r E N . T bi a n r e i s and vice e v rsa. o s l A ,
m r o f t a l P o i t a c o v n I n Ser s e c i v (PI e k o v n ) a l l ows u o y o t a c l l C- a b s d e i l b r a r s e i ( n i c g n i d u l
e h t n i y l r e d n u g I P A f o h t e p o r e n i t a g y s s m e t ) r f m o N . ET d o c e.
t e l p m o C e d n a l a t o t e g a u g n a l g e t n i ra o i t n: e k i l n U , M O C T E N . su o p p r s t c o r ss-
e g a u g n a l n a t i r e h n i c , e s o r c s-la e g a u g n e n o i t p e c x i l d n a h ng, and o r c ss- a l n u g ag e
n i g g u b e d g.
A common runti e m e g n n i e shared by l a l .N T E -aware langu g a es: n O e s a pect of this
e n i g n e s i a we l l - e d fi e n d s t e of t p y es that each E N . T-a a w re a l n u g age und r e s a t nds.
A a b se l c ass li r b ary: This library provides s e h lter from the o c l p m ex t i i s e of a r w AP I
l a c ls and r e f f o s a cons t s i ent obje t c model used by a l l . E N T-aw r a e la g n u g a es.
No o m re COM plum i b ng: C I l a F s s a c r o t y, IU w o n k n n, I p s i D a c t h, D I L c d o e, a d n the e i v l
A I R A V NT c m o p i l n a t a d ta typ s e ( S B R T , SA R R A E F AY, and so forth) ha e v no pl c a e in a native
.N T E i b nary.
A r t uly s m i p f i l ied deploym n e t model: Under , T E N . e h t re is no n e e d to r g e is e t r a
bi a n ry u t i n
into t e h system g e r istry . u F rther , e r o m E N . T al w o l s l u m tiple e v r i s o s n of the same
x e ist n i h m r a y n o on a si g n le m c a hine. As you can most i l kely ga h t er m o r f the
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7


T e h Role of the a B e s Cla s s Libra e i r s
n I o i t i d d a n to e h t R L C d n a CT / S C S L s i c e p f c i a s n o i t , h t e . T E N platform pr d i v o s e a s a b e
s s a l c y r a r b i l t a h t s i a l i a v a ble o t a l l N . ET pr g o r n i m m a g la s e g a u g n . Not n o ly s e o d this
ba e s cl s a s b i l rary en a c s p ulate vari u o s p i r i m i t e v s u s ch as th e r a , s d i f e l in u p t/o t u p t u
/ I ( O , ) g c i h p a r al d n e r e , g n i r a d n a r e t n i c n o i t t i w h a v r u o i s ext r e nal r a w d r a h e c i v e d es, u b t
t i o s l a o r p v s e d i o p p u s rt r o f a r e b m u n f o e c i v r e s s d e r i u q e r y b o m st l a e r -worl d
i t a c i l p p a n o s. r o F e l p m a x e , h t e e s a b cl s a s lib a r s e i r e d f n i e p y t s e h t t a a f i c l t i ate b a t a d ase
, s s e c c a a m L M X n l u p i a i t n o , programma i t c c e s ur t i y, d n a the s n o c tr c u o i t n of web-
en b a l d e (as we l l as t d a r i t i a n o l de k s top and c n o sole- a b se ) d fr t n o ends . F o r m a i h gh
leve , l you can visua i l ze the e r la o i t h s n ip e b t e w n e the , R L C T C , S L C , S a d n h t e base
a l c ss b i l rary.
1.4 h W at # C Bri g n s to the Ta l b e
Gi n e v t a h t . E N T is u s h c a a r di a c l de a p r u t re from previ s u o t c e hn o l o i g es, Micr f o s o t
s a h e d vel d e p o a e n w prog a r m i m ng lan e g a u g , C# (p n o r ou c n ed see p r a h s ),
sp c e ifically for this new platform. C# is a r p ogramm g n i la u g n age th t a loo s k ver y
sim l i ar ( u b t o n t i e d nti a c l) to the syntax of Java. , r e v e w o H o t l l a c # C a J a v a r p i -o f f s i
in c a cu a r te. B t o h C# d n a a v a J are e s a b d on the synt c a ti a c l constructs of C++. t s u J s a
a v a J is n i a m ny y a w s a cl a e n d e -up e v rsion of C + + , # C c n a be viewed as a c e l aned- p u
versi n o of Javaafter a , l l they are a l l n i t e h same a f l i m y f o l g a u g n a es .

e h T tr t u h of the t t a m er is h t at many of C#s s a t n y c i t c c n o st u r cts are m d o eled afte r
v r a i u o s s a p c e ts of i V s l a u B c i s a 6 0 . and C + + . r o F l p m a x e e, k i l e B V 6, # C t r o p p u s s h t e
n o i t o n f o l a m r o f e p y t s e i t r e p o r p s a ( s o p p o ed o t l a n o i t i d a r t r e t t e g d n a r e t t e s ) s d o h t e m
d n a e h t y t i l i b a o t c e d l e r a s d o h t e m g n i k a t g n i y r a v r e b m u n f o g r a u e m nts ( a i v
r a p a r e t e m a y a r r s) . i L ke C , + + C# a o l l s w you to overload ope a r tors, as we l l as to c a e r te
st u r ctures, enume a r i t s n o , n a d a c l l ba k c f c n u i t ons (via e d legates). u D e to the fact that
C# is a b y h rid f o numerous l n a guages, the re u s lt is a pr u d o ct that is as s n y tact c i ally
l c eanf i o n t cl a e nert n a h Ja a v , is b a out as s m i ple s a B V 6, and pr e d i v o s j s u t b a out
s a u m h c o p wer and flexibi i l ty as C++ ( t i w hout h t e a o s s cia e t d ugly bi s t ). n I a nutshe , l l
t e h C# l n a g a u ge offers t e h o f l l o i w ng f a e tures (ma y n of i h w h c are h s ar d e by other
.NET-aware program i m g n lan a u g ges):
No pointe s r re u q r i e ! d C# g o r p rams t p y c i ally h e v a no n d e e for direct p t n i o er
m n a i u p la o i t n
t l a ( hough you r a e r f ee to drop o d wn to that level if a s b olutely necessary).
i t a m o t u A c o m e m ry a m n e m e g a nt throu h g a g rb e g a col t c e l i n o . n e v i G thi , s C# s e o d
t o n p u s - t r o p a l e d ete . d r o w y e k
l a m r o F s c i t c a t n y t c u r t s n o c s r o f m u n e e , s n o i t a r s e r u t c u r t s, and cl s a s r p ope e i t r s .
h T e C++- k i l e ab i l i ty to v o erl a o d opera o t rs o f r a custom type, t i w o h ut the compl x e ity
(e.g , . m k a i g n sure to return t * his o t a l l ow c a h in n i g is t o n r u o y b o r p lem).
s A f o # C 2 0 0 , 5 e h t a t i l i b y to b l i u d gene c i r y t p s e and e n e g i r c me b m e s r s u g n i a syn a t x
y r e v m i s i r a l to + C + te l p m ates.
l l u F t r o p p u s r o f t n i e a f r ce- d e s a b p r g o r amm n i g e t c n h i e u q s .
Fu l l r o p p u s t for aspect-ori n e t d e program i m g n A ( OP) t n h c e iques via a t t r b i utes. i h T s
a r b nd f o v e d e o l p t n e m l a o l ws you to s a sign c a r a h c e t ris c i t s to p y t es a d n i e h t r me e b m rs to
r e h t r u f y f i l a u q h t e r i beh o i v a r .

e D pt. f o T I B J S , E S I e g a P 2 1
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

Pe h r aps the most i p m ortant point to und r e s a t nd about the C# language shipped
i w h t t e h Mc i rosoft . T E N plat o f rm is that t i can only prod c u e co e d that can e e x cute
t i w hin t e h .NET r n u m i t e (you could n v e er use C# to u b ild a a n tive COM e s rver r o a
unm a n a ged Win32 API a p p l c i a i t on). f f O icially s e p aking, the e t rm s u d e to desc i r be the
d o c e targe i t ng the E N . T run i t me is d e g a n a m c e d o . e h T b n i ary u t i n h t at c n o t n i a s t e h
g a n a m ed c d o e s i term d e n a s a s m e bly e r o m ( s l i a t e d on assemb i l es in j s u t a bit).
C n o e v rsely, o c de that o n n a c t be dir c e l t y host d e by t e h N . T E r n u i t me is termed
n u m d e g a n a o c de.

1.5 A i d d t n o i al .NET-Aw r a e Pr g o ra m m ing La u g n ages
Unde s r t d n a that C# is o n t t e h o y l n lan g a u g e a t rget n i g the E N . T l p atf r o m. h W en e h t
. T E N l p a m r o f t a w s i f r t s e r vea e l d to h t e e g ne a r l pu l b ic during the 2 0 0 0 Mc i rosoft
s e f o r P s a n o i l v e D el p o e s r e f n o C rence (PDC , ) se e v ral ven o d rs annou e c n d t e h y e w re u b sy
bu l i di g n . T E N -a r a w e v n o i s r e s of their e r s e p ct e v i co p m i s r e l . t A h t e m i t e f o this wr t i ing,
z o d ens of di f f er n e t a l n u g a e g s have undergone .NET en g i l htenm n e t. In ad i d tion to the
e v i f a l nguages that s i h p with Visual Studio 2005 (C#, J#, s i V u l a Basic N . ET, a M na d e g
s n o i s n e t x E r o f , + + C and i r c S J t p E N . T , ) t e h re a e r E N . T o c m e l i p s r for S t l l a m l a , k O C BOL ,
d n a c s a P l a t ( o a n me a fe ) w . h g u o h t l A t i h s o b ok foc s e s u a ( m l t s o ) e i s u l c x e v ly on , # C Ta e l b
1-1 s t s i l a nu r e b m f o .N T E -enab e l d o r p gramming lang e g a u s and w e r e h to e l a n r m r o e
t u o b a t m e h o d ( e t o n t a h t e s e h t URLs a e r u s bj t c e to h c a g n e).
Ta e l b 1-1. A Samp i l ng of . T E N -A e r a w r P ogr m a i mng Langu g a es
. E N T Language e W b i L nk g n i n a e M in f i L e
htt / : p / w w w o . b n o r e . h t e z c . / h o o r e b n. e n t
htt / : p / w w w u . s . a f a a m . f i / l f d / s c f d /b s o i /
htt / : p / w w w n . e b o c t o c . l om r o F e s o h t d e t s e r e t n i n i O C B L O .NET.
htt / : p / w w w e . i l e f f . o c m o F r t o h se i e t n res e t d in Eiffel .NET.
htt / : p / w w w d . a a m a t n r . o/ f d r o th
htt / : p / w w w s . i r e v l f s o r t c . m o / f / 1 1 tn 5 9 /
t f n f _ 5 9 t r o n a r _ 5 9 _ f _ r o n i w o d s a . s w p
htt / : p / w w w v . mx- e n t o c . m

a w a e b e s a e l P r e t t a h T b a l 1 e - 1 i o n s t x e h u a stive . N f o t s i l a n i a t n i a m s e t i s b e w s u o r e m u
compi e l r , s n o e of h w c i h o w u d l be
htt / : p / w w w d . o t e n t p e w o r d e .co a l / m ng e g a u s p s a . x ( g a ain, the e a x ct
URL is u s bject to h c a ) e g n . I encour g a e you to vis t i this pag , e as you e r a u s re to find
many . E N T langua e g s o w rth inves i t g t a ing L ( I P S .NE , T anyone?).

e f i L n i a l i t l u M ang e g a u d l r o W
As v e d e o l pers fi s r t c m o e to d n u s r e a t nd t e h l g n a u g a e- n g a os i t c na r u t e of E N . T,
num r e ous u q es i t s n o arise. The o m st preval n e t of e h t se questions would have to be, If
l l a T E N . a l n e g a u g s o c mpi e l o d wn to g a n a m ed , e d o c h w y o d e w n d e e o m e r n a h t n o e
? r e l i p m o c e r e h T e r a a e b m u n r of ways to n a s e w r t i h s q t s e u i n o . , t s r i F e w s r e m m a r g o r p
e r a a y r e v c i t r a p u r a l t o l h w en t i o c e m s to u o r c i o h c e of r g o r p m a i m ng g a u g n a l e l e s y m ( f
) d e d u l c n i . So e m of s u f e r p r e lang a u g s e f l l u of se i m co o l ns and u c l r y r b e k c a , s t t i w h s a few
e g a u g n a l s d r o w y e k s a e l b i s s o p . t O h r e s e y o j n a e g a u g n a l t a h t r e f f o s e r o m human-
e l b a d a e r c i t c a t n y s o t k s n e s ( uch s a s i V a u l c i s a B N . ) T E . l l i t S ot e h rs may w n a t to

e D pt. f o T I B J S , E S I e g a P 3 1
p e m o H a e g o f r i t c A ve e b O r n o .NET.
m f c . p r a h s _ a / l m t h _ c c m Homepage for # A
o F r h t ose in e r e t sted in Fo t r h .NE . T
o F r th s o e in r e t e e t s d in o F rtran E N . T.
Yes, v e en a m S l t l alk .NET is availa l b e.
.NET-awar e
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

le g a r e v e h t e r i ma r f n i a e m sk l l i s wh l i e mo g n i v to t e h N . T E l p a r o f t m (via CO O B L N . E ) T .
, w o N e b t s e n o h . f I t f o s o r c i M e r e w o t u b i d l a i s ngle o fficial .NET la g n u g a e h t at s a w
e d r e v i d m o r f e h t B S A IC fa i m ly of lan u g a e g s, can you r a e l y l say a l l r g o r p amm r e s
o w uld be p a h py t i w h t i h s choice? Or, if the only official .NET language was based o n
Fortran syntax, imagine l a l the fol s k u o t
e r e h t o h w d l u o w e r o n g i T E N . r e h t e g o t l a . e s u a c e B e h t T E N . u r n e m i t t ' n d l u o c care s e l s
h c i h w e g a u g n a l a w s used to build a block of m n a ag d e cod , e .NET pr g o a r m m ers c n a
t s ay u r t e to their s n y tactic prefe e r n e c s, and sha e r the compiled assembl e i s am n o g
a e t mma e t s, departments, and ex e t rnal o a g r ni a z i t o s n (r a g e rdl s e s of which N . T E
l u g n a age ot s r e h o o h c se to e s u ). n A o r e h t x e c t n e l l e u d o r p y b t c of int g e a r g n i t a v r u o i s
. T E N s e g a u g n a l o t n i a e l g n i s i n u f d e i a w t f o s re o s luti n o is the simple a f ct that a l l
r g o r p a i m m ng languag s e a h ve their w o n sets f o streng h t s and we k a -nesses . r o F
x e a , e l p m s e m o m m a r g o r p n i g la g n uages o f f er excellent t n i rins c i s p p u ort r o f
a d e c n a v d h t a m ema l a c i t o r p c s e s g n i . s r e h t O r e f f o r o i r e p u s o p p u s rt for f n a n i c l a i
, s n o i t a l u c l a c l a c i g o l , s n o i t a l u c l a c n o i t c a r e t n i h t i w r f n i a m a e m o c t u p m e , s r and so h t r o f .
h W en you take h t e s r t eng h t s of a part c i u a l r pr a r g o m m n i g u g n a l a e g a d n n e h t o c n i r e t a r o p
e h t s t i f e n e b d e d i v o r p y b e h t T E N . , m r o f t a l p y d o b y r e v e s n i w . Of cou , e s r in reality h t e
a h c n e c s are qu t i e good th t a you i w ll spend m c u h of y u o r t m i e bu l i ding o s t f a w e r g n i s u
r u o y E N . T g a u g n a l e of c e c i o h . Ho e w , r e v once you learn h t e syntax of n o e E N . T
lang a u g , e it is e v ry easy to mas e t r anoth r e . This is al o s u q ite e b neficial, es e p cially to
t e h co s n ult n a s t of t e h o w rld. If your l n a guage of cho c i e a h ppe s n to be C#, but you are
c a l p ed at a c e i l nt s e t i h t at s a h c t i m m o ted to Visual a B sic E N . T, o y u sh u o ld be able t o
s r a p e h t e x e i t s n i g co e d body m l a ost n i s a t nt y l ( n o h s e t ) ! h w e l i still n i t n o c u g n i o t
e g a r e v e l h t e . E N T Frame o w rk. E h g u o n a s id.

1.6 An O e v rview of .NET e s s A m e i l b s
Re r a g d s e l s of wh c i h E N . T lang a u ge you h c s o o e o t r p ogram with, n u derstand that
s e d p t i e the fact h t at .NET binaries take t e h sa e m file extensi n o as COM e s rvers and
un a m nag d e Win32 bin r a e i s . e P h r a s p
o m st m i por , t n a t T E N . a n i b r s e i o d t o n i a t n o c n t a l p form-sp c e if c i inst u r c o i t ns, u b t rather
l p o f t a rm-agn i t s o c int e m r e diate langu g a e (IL) and t p y e e m tadata. u g i F e r 1-2 h s ows the
i b g picture of h t e story h t us f r a .













e r u g i F 1-2. l A l T E N . - e r a w a s r e l i p m o c e t i m L I s n o i t c u r t s n i d n a me a t d a t a .


e D pt. f o T I B J S , E S I e g a P 4 1
(*. l d l or x e . * e , ) e h t y h v a e absolutely no internal










d n u b e l d o t n i an a s s e b m ly. You l i w l exa n i m e u n m r e ous e d ta l i s of .NET
a e s s mb i l es in Howe e v , r to fac t i l i ate t e h di u c s ssi n o of the T E N . run m i t e envir n o m n e , t
u o y do e e n d to d n u e t s r and some a b sic pr e p o rti s e of this new file form t a . As
e m n i t o e n d, an a s s embly c n o ta n i s L I C code, which is conceptually i m i s lar to J v a a
by e t co e d in that t i is t o n o c mpiled o t pla o f t rm-sp c e i c i f n i struc i t n o s un l i t absol t u e y l
necessary. Ty i p ca l l y, absolute y l e c e n s a s r y is t e h p i o nt at which a o l b ck of CIL
instr c u tions s ( u h c as a method imple-
) n o i t a t n e m s i f e r erenc d e o f r u e s by h t e N . ET r i t n u e m .
In d d a i t i on to CIL n i str c u i t n o s, a e s s mb i l es al o s contain me a t a d a t t a h t d s e cri s e b in
vivid e d ta l i h t e c r a h a i r e t c s c i t s of e e v ry t y e p l v i ing w t i hin the bi a n ry. r o F , e l p m a x e i f
Sp s t r o Car, e h t t p y e t a d a t e m a descr s e b i e d a t l i s s c u h as
Spo s t r Cars e s a b , s s a l c i h w ch in e t f r c a s e are l p m i emented by Sp s t r o Car (if a y n ), s a well
s a a full desc i r pt o i n of ea h c mem e b r su o p p rted y b the S r o p t a C s r type.
s i a r d ama c i t p m i o r veme t n to COM type metadata . As you may al e r ady know, COM
binari s e a e r y t pical y l d c s e ri d e b usi g n an as c o s iat d e t p y e l b i rary (which is lit l t e mo e r
t n a h a b a n i ry vers o i n of In e t rf c a e e D fin t i ion La g n u g a e [I L D ] co e d ). h T e problems with
COM t p y e n i forma i t on r a e t a h t t i is n t o u g arant e e d to e b p e r n e s t a d n t e h fact t a h t I L D
o c de a h s no way to u c o d m n e t the e t x ernally referen d e c serv r e s h t at are r u q e ired for
e h t corr c e t p o era o i t n f o h t e u c rr n e t C M O server . In cont a r st, .N T E metadata is
a w l ays pr s e ent and is automati a c l y l generated by a given . T E N -aware compi e l r.
i F a n lly, n i d d a o i t i n to CIL d n a type meta a d ta,a e s s mb i l s e themsel s e v are also des r c ib d e
s u i g n d a t e m a a t , which s i o f f a i c i lly te d e m r a a m ni s e f t. T e h mani s e f t c t n o a n i s
n o i t a m r o f n i t u o b a e h t e r r u c t n version of t e h as e s l b m y, u c t l ure informa i t n o ( d e s u for
loca i l zi g n stri g n and m i age re o s urces), d n a a i l st f o a l l extern l a ly re e f ren e c d
s a s m e b s e i l h t t a are r u q e ired for proper ex c e ut o i n. Yo u l l ex m a n i e a v r u o i s to s l o h t at
n a c be s u ed o t e n i m a x e n a e s s a m y l b s t e p y , s me d a t a , a t a d n manife t s inf r o a m ti n o over
the c u o rse f o t e h next w e f chapters.

1.7 S l g n i e- i F le a d n M l u tif l i e Assem i l b es

n I a gr a e t u n m r e b of ca s e s , there is a s m i ple one-to-one c r o res d n o p en e c e b tween a
and the bi a n ry file ( . * l d l or *. x e e). Th , s u f i y u o are b i u l i d g n a N . T E
*.dll, t i is f a s e o t cosi e d r that t e h i b nary and h t e assembly are o e n d n a h t e sa e m .
, e s i w e k i L f i o y u a e r bu l i d g n i an e x e c a t u b e l desk
e b r r e f e r ed o t s a e h t ass m e y l b s t i elf .
e T chni a c lly spea i k ng, if an asse b m ly is o c p m o d e s of a sin l g e
have a g n i s le- i f le m e s s a bly. i S n e l g -f e l i s s a e b m l e i s contain all t e h e n c s e
m t e a d a ta, d n a ass c o iat d e m n a ifest in an autonomous, s n i gle, we l l -def n i d e p c a a k ge.

e D pt. f o T I B J S , E S I e g a P 5 1
u o y e v a h a c s a l s m a n ed
. T E N metada a t
.NET as e s mbly
p o t a c i l p p a , n o i t e h t x e . * e n a c s m i ply
w o h e , r e v this s i not o c mp y l e t e l c a cura e t .
. * dll or . * exe module, you
sary CI , L
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

t o N e e r e h T s i e n o t n i o p o t e b e d a m g n i d r a g e r e h t n o i t a i v e r b b a I L . Du i r g n the devel p o e m nt of . , T E N
e h t i f f o -
l a i c m r e t r o f L I s a w t f o s o r c i M e m r e t n i di e t a a l g n uage M ( L I S ) . Ho r e v e w h t i w e h t l a n i f e s a e l e r f o , T E N . t e h
m r e t s a w d e g n a h c o t n o m m o c i d e m r e t n i ate language C ( L I ). , s u h T s a u o y d a e r t . e h T E N t i l era u t re ,
d n a t s r e d n u h t at , L I , L I S M d n a L I C e r a l l a g n i b i r c s e d e h t e m a s y t i t n e t c a x e . n I g n i p e e k h t i w h t e cu r r n e t
o l o n i m r e t g , y I l l i w e s u e h t r b b a i v e a n o i t L I C h t rou h g u o t h t is t x e t .
n e h W a *. l d l or e . * xe has e e b n r c a e ted using a N . ET-aw r a e o c mpil r e , t e h re u s i t l g n
m u d o le i s
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

e l i f i t l u M s e i l b m e s s a , on h t e o r e h t han , d e r a c o p m o e s d of nu u o r e m s N . ET n i b r a s e i , a e ch
f o c i h w h is e t rmed a l u d o m e. Wh n e u b l i ding a mu i t l f l i e a e s s mbly, o e n of the e s
m u d o les (termed t e h a m i r p ry o m d l u e) u m st o c ntain h t e s a e s mbly a m ni e f st (and
s o p sibly L I C n i s s n o i t c u r t d n a me t a d a t a f r o var s u o i y t pes). e h T r e h t o t a l e r ed mo e l u d s
t n o c a n i a e l u d o m v e l el , t s e f i n a m I C L, and t p y e t e m a a t a d . s A o y u g i m ht susp c e t, h t e
m i r p ary mo l u d e o d c n e m u s t the set of requir d e sec d n o ary u d o m les wi i h t n h t e
s s a e l b m y manifest. S , o why wo l u d y u o choose to create a mu t l ifile s s a embly? W e h n
o y u a p rt i t i on an s a sembly n i o t discrete m u d o e l , s u o y d n e up w t i h a o m re fl x e ible
p e d l y o m n e t p o i t on. For e a x e l p m , f i a u e s r is e r f r e en i c g n a remote ass m e b y l a h t t d e e n s
o t be o l n w o d a d e d o t n o s i h r o r e h c a m h n i e, e h t t n u r ime i w ll only do l n w d a o the
e r q r i u d e modules. , e r o f e r e h T u o y e r a e e r f o t t c u r t s n o c r u o y y l b m e s s a n i h c u s a y a w
less freq e u l t n y r q e ui d e r y t pes (su h c as a t p y e nam d e Ha i r D d r v r o f e R e m e t t a r) r a e kep t
in a se a p
n I n o c t s a r , t if a l l your t s e p y w r e e pl e c a d in a sing e l -f l i e s a e s mbly, t e h e d n e s u r may
d n e p u downl d a o i g n a large h c unk of data that is not r a e lly need d e (which is
o i v b o usly a waste of i t me). , s u h T s a you can e s e, an assembly is re l l a y a logi l a c
r g ouping f o n o e or more re t a l ed mod l u es that are int n e ded to e b in i t i a y l l deployed
and vers o i e n d as a sin l g e n u it .


. 1 8 The Role of the Common Int r e m d e ia e t Language

Now t a h t o y u have a e b t t er feel for .NET s a s b m e li s e , lets x e amine h t e o r le f o the
common intermediate langu g a e (CIL) in a bit mo e r e d tail. CIL is a l n a g a u ge that sits
o b a ve a y n pa t r icul r a p t a l r o f m-s e p ci i f c instruction e s t. R g e ardle s s of whi h c N . ET-
aware lan a u g ge y u o ch o o se, the assoc t a i ed c p m o i r e l emits CIL instruc s n o i t . r o F
x e a e l p m , t e h fo l l owing C# co e d models a trivial a c lcul t a or. o D nt co e c n rn u o y r e s lf
with h t e ex c a t n y s tax for now, u b t o d no c i t e the format of the
C l a c cl s a s:
/ / s c . c l a C
g n i s u ; m e t s y S
e c a p s e m a n e l p m a x E r o t a l u c l a C
{
/ / s i h T s s a l c s n i a t n o c e h t s ' p p a t n e y r . t n i o p
c i l b u p s s a l c
{
c i t a t s d i o v ) ( n i a M
{
c l a C c = w e n ; ) ( c l a C
t n i s n a 84);
0 1 " ( e n i L e t i r W . e l o s n o C + 4 8 s i , " . } 0 { ; ) s n a

/ / t i a W r o f r e s u o t s s e r p e h t r e t n E y e k e r o f e b g n i t t u h s . n w o d
Con l o s e e R . d a L ( e n i ; )
}
}

e D pt. f o T I B J S , E S I e g a P 6 1
t a h t
e t a r d n a t s - e n o l a e l u d o m .
Add() method in the
p p A c l a C
= , 0 1 ( d d A . c
C# d n a T E N . m m a r g o r P i g n I 6 0 S 1 6 7


/ / e h T # C . r o t a l u c l a c
c i l b u p s s a l c
{
c i l b u p t n i d A d t n i ( , x t n i ) y
{ n r u t e r x + ; y }
}
}
O c n e h t e C# c l i p m o r e ( s c x e . c e) comp l i s e h t is o s ur e c co e d f l i e, you end up i w h t a
si g n le-f l i e * x e . e s s a m e y l b t a h t t n o c a n i s a i n a m e f t s , CIL n i r t s c u t o i s n , d n a me d a t a a t
g n i b i r c s e d each as c e p t of the C l a c and C l a cApp cl s s a es. r o F example, if you were o t
e p o n h t is assembly us n i g l i dasm.e e x ( a x e mined a l t t i l e lat r e n i h t is a h c t p er), you
w u o ld f n i d t a h t t e h A d d () me o h t d s i repre e s nted using
. p d o h t e m c i l b u g i s y b e d i h e c n a t s n i 2 3 t n i 2 3 t n i ( d d A , x 2 3 t n i ) y l i c d e g a n a m
{
/ / e d o C e z i s 8 ) 8 x 0 (
.m s x a t k c a 2
. t i n i s l a c o l ] 0 [ ( 2 3 t n i ) 0 0 0 0 $ 1 $ S C
: 0 0 0 0 _ L I 1 . g r a d l
: 1 0 0 0 _ L I 2 . g r a d l
: 2 0 0 0 _ L I d d a
: 3 0 0 0 _ L I 0 . c o l t s
IL_ 0 0 0 4: . r b s 6 0 0 0 _ L I
: 6 0 0 0 _ L I 0 . c o l d l
: 7 0 0 0 _ L I e r t
} / / d n e f o d o h t e m d d A : : c l a C
Don t w r o y r if you r a e unable to make h d a e s or tails f o the r u s e l n i t g CIL for thi s
methodw l l i descr b i e h t e a b s c i s of the C L I pr g o ra i m m ng a l nguage. T e h po n i t o t
e t a r t n e c n o c n o is h t at the C# compiler e i m ts CIL, not platform-spe i c fic n i s r t uctions .
, w o N l l a c e r t a h t s i h t s i e u r t f o l l a T E N . - e r a w a s r e l i p m o c . To l l i ustrat , e a u s s me you
cre t a d e this same a c i l p p i t a n o i s u g n i V su l a s a B ic N . T E ( B V .NET , ) r t a e h r h t an # C
' b v . c l a C
s t r o p m I m e t s y S

e c a p s e m a N e l p m a x E r o t a l u c l a C
' A VB T E N . ' e l u d o M ' s i a s s a l c t a h t y l n o s n i a t n o c
' c i t a t s s r e b m e m .
e l u d o M p p A c l a C
b u S ) ( n i a M
m i D s n a r e g e t n I
m i D c s A w e N c l a C
s n a = , 0 1 ( d d A . c ) 4 8
0 1 " ( e n i L e t i r W . e l o s n o C + 4 8 s i , " . } 0 { ) s n a
Con l o s e e R . d a L ( e n i )
d n E b u S
d n E e l u d o M


e D pt. f o T I B J S , E S I P e g a 7 1
c l a C
L I C s h c u as t e h fo i w o l l g n :
s A
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

s s a l C c l a C
c i l b u P n o i t c n u F Add l a V y B ( x s A , r e g e t n I l a V y B y s A ) r e g e t n I s A r e g e t n I
n r u t e R x + y
d n E n o i t c n u F
d n E s s a l C
d n E e c a p s e m a N
f I o y u exa i m e n t e h I C L o f r t e h Ad ( d ) me , d o h t o y u fi d n s i m i lar ins r t u t c ions (s i l g t h ly
e w t aked by
h t e VB . E N T com i p e l r):
. c i l b u p d o h t e m 2 3 t n i ( d d A 2 3 t n i e c n a t s n i , x 2 3 t n i ) y l i c d e g a n a m
{
/ / e d o C e z i s 9 ) 9 x 0 (
.m s x a t k c a 2
. s l a c o l t i n i ] 0 [ ( 2 3 t n i ) d d A
: 0 0 0 0 _ L I p o n
: 1 0 0 0 _ L I 1 . g r a d l
: 2 0 0 0 _ L I 2 . g r a d l
: 3 0 0 0 _ L I f v o . d d a
: 4 0 0 0 _ L I c o l t s 0 .
IL_ 0 0 0 5: . r b s 7 0 0 0 _ L I
: 7 0 0 0 _ L I 0 . c o l d l
: 8 0 0 0 _ L I t e r
} / / d n e f o d o h t e m d d A : : c l a C

n e B efits of I C L
t A h t s i p , t n i o y u o i m g t h e b o w nderi g n c a x e y l t w a h t is a g i e n d by c i l i p m o g n o s urce
co e d into CIL ra h t er h t an i d re y l t c to a s e p c i f i c n i struc n o i t e s t. O e n be i f e n t s i
la u g n age n i te r g ati n o . s A you have alr a e dy s e e n, ea h c T E N . -aw e r a compiler pr s e c u d o
a e n rly i e d n i t cal CIL i s n tr c u tio s n . e h T r r o f e e, a l l n a l guages e r a l b a e to n i teract wi h t in a
we l l -defined binary a e r na. , e r o m r e h t r u F n e v i g t a h t L I C s i m r o f t a l p - , c i t s o n g a e h t T E N .
k r o w e m a r F f l e s t i s i m r o f t a l p - , c i t s o n g a g n i d i v o r p e h t e m a s s t i f e n e b J a v a s r e p o l e v e d e v a h
n w o r g d e m o t s u c c a to , . e . i ( a s e l g n i e d o c e s a b g n i n n u r n o s u o r e m u n g n i t a r e p o ) s m e t s y s .
n I fac , t r e h t e s i an t n i e l a n o i t a n r sta d n ard for t e h C# la u g n a , e g d n a a a l rge subset f o the
.NET platform n a d p m i lem n e ta i t s n o already x e ist for m y n a non-Wi o d n ws e p o i t a r n g
y s stems ( r o m e i a t e d ls t a h t e conc u l s o i n of h t s i t p a h c r e ). n I t s a r t n o c o t , a v a J , r e v e w o h
. T E N s w o l l a u o y o t d l i u b s n o i t a c i l p p a g n i s u r u o y a u g n a l e g f o e c i o h c .

o C mp i l i ng CIL to l P a f t orm- e p S cific In r t s uc i t ons
e u D o t e h t t c a f t a h t as m e s b s e i l con n i a t L I C u r t s n i c , s n o i t r e h t a r h t an a l p tfo m r -s c e p i i f c
s n o i t c u r t s n i , L I C e d o c u m st be m o c p l i ed on t e h f y l e b fo e r e s u . The ent t i y that o c m i p e l s
CIL code i t n o e m aningful CPU t s n i r c u t n o i s is e t rmed a u j t s - n i -t m i e (JIT ) c m o piler,
c i h w h e m i t e m o s s s e o g y b e h t r f ie l d n y e m a n of t i J ter. e h T N . ET u r ntime n e vir m n o ent
s e g a r e v e l a I J T r e l i p m o c r o f e h c a CPU t r a t e g i g n h t e runti a e , e m ch opti z i m ed for h t e
g n i y l r e d n u l p a r o f t m. For a x e mple, if you e r a u b ilding a . E N T app i l cation that is to e b
p e d l y o ed to a a h ndheld e d vice s ( uch as a Pocket PC), the c r o re o p s d n ing i J tt r e s i we l l
equipped to r n u within o l w-memory v n e iro m n ent. On e h t r e h t o ha d n , if y u o a e r
de g n i y o l p u o y r s a e s b m ly to a ba k c -end server ( h w ere memory is seldom an i e u s s ) ,

e D pt. f o T I B J S , E S I e g a P 8 1
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

t e h Jitt r e w l i l e b opt m i i d e z to f n u cti n o in a high- e m o m ry e v n i m n o r ent . n I this way,
v e d elop r e s n a c write a single d o b y f o e d o c t a h t n a c be i f f e c n e i t y l J T I -co p m i e l d and
e x e c d e t u on mac i h e n s t i w h f f i d erent architec u t s e r . u F r , e r o m r e h t as a n e v i g r e t t i J
s e l i p m o c L I C s n o i t c u r t s n i o t n i g n i d n o p s e r r o c e n i h c a m , e d o c t i l l i w e h c a c e h t re s t l u s n i
me o m ry in a mann r e suit d e o t t e h ta t e g r p o r e g n i t a y s stem. n I s i h t , y a w f i a l l a c s i e d a m o t
a met d o h nam d e r P n i tD c o m u n e t() , t e h C L I i s n t c u r tion s e r a o c p m il d e into platform-
e p s cif c i n i s u r t c s n o i t n o h t e i f r t s c o v n i ation a d n re e n i a t d in me o m y r for e t a l r use.
, e r o f e r e h T e h t t x e n e m i t i r P n D t u c o m ( t n e ) a c s i ll , d e t e h r e i d e e n o n s t o r o c e mpil e t e h CIL.

. 1 9 h T e R l o e of .NET Type Metadata
n I d d a i t i on to C L I ins r t c u i t n o s, a .NET as e s l b m y o c t n ai s n f , l l u o c mplete, d n a c c a u a r te
met a d a ta, i h w ch s e b i r c s e d h c a e a d n eve y r ty e p (c a l s , s s c u r t t e r u , en m u eratio , n a d n s o
) h t r o f n i f e d ed n i h t e i b nary, as we l l as t e h mem s r e b of c a e h y t e p (pr p o er i t es, met d o h s,
ev n e ts, n a d o s on). Th n a k u f l y l , it is always t e h j b o of t e h o c mpil r e n ( ot t e h
p o r g a r mmer) o t e t i m t e h latest d n a gr a e e t st y t pe me a t - data . c e B a e s u . E N T met d a a a t is
o s i w cke l d y met c i ulous, s a semblies are o c mpletely self- e d scribing en t i t e i sso m c u h
, o s in a f ct, h t at T E N . bina i r es h v a e no need to be regist r e ed into h t e system reg t s i ry. T o
l l i ustrate h t e format of .NET t e p y met a d a t , a le t s ta e k a l k o o at the metadata t a h t ha s
d e t a r e n e g r o f the d A d() me h t d o of the C# C l a c class you a x e m e n i d r p v e i s u o ly ( h t e
met a d a ta g n e e a r ted r o f h t e B V .NET ve s r ion of the A ( d d ) me h t od is s i m i a l r):
f e D e p y T 2 # ) 3 0 0 0 0 0 2 0 (
--- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - ---
y T p N f e D e m a : C u c l a o t a l rE m a x p a C . e l lc (0 0 0 2 0 3 0 0 )
Flags ] c i l b u P [ ] t u o y a L o t u A [ ] s s a l C [
A [ n l C i s s s a ] [ e B i F e r o f e n I d l it] 1 0 0 ( 0 0 0 1 0 )
s d n e t x E : 1 0 0 0 0 0 1 0 ] f e R e p y T [ t c e j b O . m e t s y S
d o h t e M 1 # ) 3 0 0 0 0 0 6 0 (
--- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - ---
d d A 0 0 6 0 ( 0 0 0 3)
[H B e d i y g i S ] R [ e S e s u o l ] t 0 0 0 ( 0 0 0 6 8 )
V R A : 0 9 0 2 0 0 0 0 x 0
s g a l F l p m I : ] L I [ ] d e g a n a M [ ) 0 0 0 0 0 0 0 0 (
Cal n C l v n t n :
s i h T s a h
: e p y T n r u t e R
2 s t n e m u g r A
t n e m u g r A : 1 #
t n e m u g r A : 2 #
2 s r e t e m a r a P
) 1 ( n e k o T m a r a P : ) 1 0 0 0 0 0 8 0 ( e m a N : x : s g a l f ] e n o n [ ) 0 0 0 0 0 0 0 0 (
) 2 ( m a r a P n e k o T : ) 2 0 0 0 0 0 8 0 ( e m a N : y : s g a l f ] e n o n [ ) 0 0 0 0 0 0 0 0 (

e M tadata is u d e s y b u n merous asp c e ts of the N . ET ru m i t n e v n e ir n o m n e , t s a we l l as by
s u o i r a v o l e v e d pm n e t t s l o o . o F r m a x e p , e l h t e I t n ell n e S i se e f u t a re pr d i v o ed y b i V sual
o i d u t S



e D pt. f o T I B J S , E S I a P e g 9 1
:
Met d o h N e m a :
Flags P [ u c i l b ] :
[ A F E D U T L ]

4 I
I4
I4
be n e
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

0 2 05 is m d a e p s o sible by e r ading an a s s e l b m y s met d a ata at desi n g i t me . e M t a d a ta is
also s u d e by var o i us ob e j ct browsing u i t l t i ies, debugging tools, and the C# compi e l r
t i self. To be s , e r u metadata is the o b k c a b ne of u n m o r e us N . ET t h c e nol g o ies i c n lu i d ng
remo n i t g, re l f c e i t n o , late n i b d , g n i L M X b e w e s r , s e c i v a d n ob c e j t e s ri l a iz o i t a n.

1.10 h T e Role of the s A s m e bly Ma i n fest
t s a L t u b t o n a e l st, remember t a h t a E N . T s a s m e b y l l a so co a t n ins m a d a t e ta t t a h
s e d c s e b i r h t e s a e s mbly i s t elf (tec n h c i ally t r e med a f i n a m est) . Amo g n h t o er e d tail , s t e h
m n a ifest o d cuments all x e te a n r l assemb i l s e r u q e i d e r by the curr n e t as e s l b m y o t
u f c n o i t n c r o c e r , y l t the a s s e y l b m s ve s r ion u n mber, copyright n i formati n o , and so
t r o f h. e k i L p y t e me , a t a d a t t i s i l a ways h t e o j b of h t e comp l i er to g n e erate t e h
as e s mblys ma i n fest. Here are s m o e rel n a v e t details of the
CSh p r a C c l a l u a . r o t exe e f i n a m s : t
. n r e t x e y l b m e s s a b i l r o c s m
{
. n e k o t y e k c i l b u p = 7 B ( A 7 C 5 6 5 9 1 4 3 0 E 9 8 )
. r e v 0 : 0 : 0 : 2
}
. S C y l b m e s s a r o t a l u c l a C p r a h
{
. h s a h m h t i r o g l a 4 0 0 8 0 0 0 0 x 0
. r e v 0 : 0 : 0 : 0
}
. e x e . r o t a l u c l a C p r a h S C e l u d o m
. 0 0 0 0 0 4 0 0 x 0 e s a b e g a m i
. 3 0 0 0 0 0 0 0 x 0 m e t s y s b u s
. e l i f t n e m n g i l a 2 1 5
. 1 0 0 0 0 0 0 0 x 0 s g a l f r o c
In a n t u e h s , l l this mani e f st d c o m u e t n s t e h i l st of x e ternal as e s mb i l es r u q e ired y b
CSh p r a C c l a l u a . r o t exe a i v ( the . y l b m e s s a r e t x e n d e r i ct e v i ) as l l e w as a v riou s
a h c racte t s i r ics of the a s s em y l b
1.11 U d n ersta n i d n g the Common Type System
A g e v i n a s s embly m y a co t n ain a y n m u n e b r of distinct t y e p s . In the r o w d l of .NET,
t y e p is i s y l p m a i r e n e g c te m r d e s u o t r e f e r o t a b m e m er r f om the s t e a l c { s , s
r u t c u r t s e, t n i e f r a , e c e m u n e a r tion, l e d eg t a e}. W e h n you bu l i d solut o i ns s u ing a N . ET-
aware l n a g a u g , e you will m s o t l k i ely interact i w th ea h c of these types. r o F examp e l ,
your as e s mbly m y a define a single class that m i plements s m o e nu b m er of interfaces .
Pe h r a s p n o e of h t e t n i e f r ace o h t e m ds ta e k s an en m u e a r tion p y t e as an inp t u
pa e m a r e t r d n a t e r r u ns a t s r c u t e r u to t e h l a c l r e . c e R a l l h t at h t e Com o m n y T pe y S stem
C ( TS) s i a formal specification that docu e m nts how ty e p s m s u t be d f e i d e n in e d r o r to
e b o h sted y b t e h CLR. y T pic l a ly, the only individua s l who are d p e e ly conc r e ned h t i w
t e h i n n er wor i k g n s of t e h T C S are t e s o h bu l i di g n t o o ls n a / d or o c mp l i e s r that t e g r a t h t e
. T E N p t a l form. t I is m i o p r a t n , t o h we r e v , for all . T E N p o r gra m m ers to lea n r about o h w

e D pt. f o T I B J S , E S I e g a P 0 2
s t i e f l (v n o i s r e b m u n e , r d o m u e l na e m , d n a o s on).
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

to k r o w t i w h h t e f v i e t s e p y d f e d e n i y b h t e C S T in h t e r i e g a u g n a l of c i o h c e. r e H e is a
f e i r b w e i v r e v o .
S T C s s a l C Typ s e
Every .NET-awa e r language suppo t r s, at the very least, t e h notion of a c a l ss type, whic h
s i the corn r e st n o e of object-orient d e p o r gram n i m g (OOP). A c a l s s m y a be o c m o p se d
f o a y n r e b m u n f o me r e b m s (su h c as p o r p ertie , s m t e h s d o , and e t n e v s) and da a t p i o nt s
d l e i f ( s). n I , # C c a l s e s s a e r de a l c r d e g n i s u
/ / A # C s s a l c e p y t .
c i l b u p s s a l c
{
c i l b u p t n i t n i ( d d A , x t n i ) y
{ n r u t e r x + ; y }
}

Ta e l b 1-2. S T C C s s a l h C t s i r e t c a r a ics
s s a l C t c a r a h C e i r s c i t g n i n a e M n i Li e f
s I e h t s s a l c d e l a e s r o ? t o n S a e l d e cl s a ses a c nnot func i t on as a ba e s
c a l s s to oth r e classes .
Do s e h t e c a l s s l p m i em n e t y n a in e t rfa s e c ? An interface is a e l l o c c n o i t of ab t s t c a r
me b m e s r t a h t p d i v o r e a o c n r t act be e w t en
e h t e j b o ct n a d o j b e t c r e s u . h T e

S T C w o l l a s a s a l c s to m i plem n e t any number of int r e faces.
Is t e h class abst c a r t or o c ncrete? b A s r t act s e s s a l c t o n n a c e b r i d e l t c y e r c ate , d u b t a e r
t n i e d e d n o t defi e n com o m n e b h v a iors for der v i d e y t pes. o C n e r c te classes can e b
creat d e i d rec y l t . What is t e h v is i b i t i l y of t s i h cla s s ? Each cl s a s mu t s be co f n i u g red
t i w h a visi i l i b ty attri u b te. a B s , y l l a c i th s i t i a r t e n i f e d s f i h t e l c s a s y a m be us d e y b
l a n r e t x e
s s a e s e i l b m , or only r f om t i w hin the de i f ning s a sembly (e.g., a p i r vate hel e p r c s a l s).
S T C e r u t c u r t S T s e p y
e h T t p e c n o c f o a e r u t c u r t s s i o s l a z i l a m r o f d e r e d n u e h t S T C . If y u o ha e v a C backgroun , d
u o y d l u o h s e b d e s a e l p o t t a h t w o n k r e s u e s e h t - d e n i f e d s e p y t ) s T D U ( e v a h d e v i v r u s n i e h t
d l r o w f o T E N . h g u o h t l a ( y e h t e v a h e b a t i b y l t n e r e f f i d r e d n u e h t ) d o o h . y l p m i S , t u p a
r t s uc u t re n a c e b t h g u o h t f o s a a l t h g i e w t h g i s s a l c e p y t g n i v a h e u l a v - d e s a b s c i t n a m e s . F r o
e r o m s l i a t e d n o e h t s e i t e l t b u s f o , s e r u t c u r t s e e s r e t p a h C 3. , y l l a c i p y T s e r u t c u r t s e r a t s e b
t i u s ed f r o mode g n i l g o e metric a d n mat e h ma c i t al data, and are created n i
c u r t s t keywor : d
/ / A # C r u t c u r t s e . e p y t
t c u r t s t n i o P
{
/ / s e r u t c u r t S n a c n i a t n o c . s d l e i f
c i l b u p t n i , s o P x

s e r u t c u r t S / / n a c n i a t n o c d e z i r e t e m a r a p . s r o t c u r t s n o c
c i l b u p t n i ( t n i o P , x t n i ) y
s o P x { = ; x s o P y = } ; y

e D pt. f o T I B J S , E S I e g a P 1 2
h t e cl s a s key o w r : d
c l a C
# C s u ing t e h
y o P s;
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

/ / u t c u r t S s e r y a m e n i f e d . s d o h t e m
c i l b u p d i o v ) ( y a l p s i D
{
, } 0 { ( " ( e n i L e t i r W . e l o s n o C , " } 1 { , s o P x ; ) s o P y
}
S T C e c a f r e t n I s e p y T
s e c a f r e t n I e r a n t o hi g n more h t n a a named collection of abs r t a t c member d i f e n t i i n o s,
which m y a be sup o p r d e t ( , . e . i m i ple e m nted) y b a given cl s a s or st u r e r u t c . l n U ike CO , M
a o c m m n o s a b e int r e face such as k n U I n n w o . n I , # C
e c a f r e t n i s e p y t e r a d e n i f e d g n i s u e h t a f r e t n i ce y e k - r o w d, for examp e l :
/ / A # C e c a f r e t n i e p y t .
c i l b u p e c a f r e t n i w a r D I
{
d i o v ; ) ( w a r D
}
n O h t e r i own, in e t rfac s e are of t t i l le u e s . How e v e , r w n e h a cla s s or str c u ture
m i pleme t n s a g v i n e i t n er a f ce in t i s n u ique way, you are a l b e to u q e r est ac e c ss to t e h
s p u p i l d e fu i t c n a n o t i l y i s u ng n a n i t c a f r e e refe c n e r e in a o p y l morphic a m nn r e .

CTS n E um r e ation Types
Enumera i t ons are a d n a h y r g o r p m a g n i m t c u r t s n o c t a h t a l l o s w y u o o t gro p u
e u l a v / e m a n s r i a p . r o F example, as u s me you are cr a e i t g n a vi e d o-game p a i l p a c i t n o t a h t
a l l w o s h t e player to sel c e t one of h t ree a h c racter ca e t o g ries ( z i W ard, Fi h g ter, or Thief).
Rather t n a h keeping track of raw numeri a c l
d l u o c b d l i u a t s u c om enu a r e m tion using t e h u n e m keyword:
/ / A # C n o i t a r e m u n e e p y t .
m u n e c i l b u p e p y T r e t c a r a h C
{
d r a z i W = , 0 0 1
r e t h g i F = , 0 0 2
f e i h T = 0 0 3
}
By d a f e u , t l h t e s r o t age used to o h ld each item is a 32-b t i n i e t ge ; r h w o e r e v , it is o p s i s bl e
to a t l er this s o t a r e g slot if e e n d be e ( g . ., when pr g o ra i m m g n r o f a low-memory device
su h c s a a o P ck t e C P ). l A o s , t e h S T C deman s d that enumerat d e types e d ri e v r f m o a
o c mm n o e s a b class, s y S t E . m e num . As o y u wi l l see in a h C p r e t 3, this a b se c a l ss defines a
nu e b m r of inte e r sti g n membe s r h t at a l l ow u o y to e t x r c a t, mani u p l t a e, a d n n a r t s o f rm
e h t d n u erl g n i y na e m /va u l e r i a p s r p ogramm a c i t a l y l .
CTS g e l e D a e t Ty e p s
Dele a g s e t r a e h t e . E N T equiva e l nt of a t e p y -sa e f C-style function o p in e t r. h T e key
a .N T E delegate is a s s a l c h t at deri s e v fr m o
Sys m e t . l u M i t c D t s a e g e l ate , rather th n a a simple o p i t n er to a raw e m mory ad r d e s s . n I
, # C delega e t s r a e e d clar d e using the d g e l e ate e k yword:
/ / s i h T # C e t a g e l e d e p y t n a c t n i o p ' ' o t y n a d o h t e m
/ / g n i n r u t e r n a r e g e t n i a d n g n i k a t o w t s r e g e t n i s a . t u p n i
c i l b u p e t a g e l e d t n i t n i ( p O y r a n i B , x t n i ; ) y

e D pt. f o T I B J S , E S I e g a P 2 2
. E N T i t n e f r c a es o d not d r e e v i
s e u l a v o t t n e s e r p e r h c a e , y t i l i b i s s o p u o y
n e r e f f i d ce is h t a t
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

s e t a g e l e D e r a l u f e s u n e h w u o y h s i w o t e d i v o r p a way o f r e n o n e t y t i to o f rwa d r a c l l a o t
r e h t o n a , y t i t n e d n a i v o r p e d e h t n o i t a d n u o f r o f e h t T E N . t n e v e e r u t c e t i h c r a . t a g e l e d s e
e v a h c i s n i r t n i o p p u s rt r o f g n i t s a c i t l u m , . e . i ( g n i d r a w r o f a t s e u q e r o t e l p i t l u m ) s t n e i p i c e r
d n a
s u o n o r h c n y s a d o h t e m s n o i t a c o v n i .
S T C T s r e b m e M e p y
Now t a h t u o y e v a h r p eviewed ea h c f o the y t pes forma i l d e z y b the C S T , r a e i l ze h t at
m t s o y t e p s take n a y num e b r of me e b m rs. F m r o a l l y speaki g n , a ty e p member s i
d e n i a r t s n o c y b e h t set n o c { s r o t c u r t , , r e z i l a n i f c i t a t s r t s n o c u t c o , r e n s d e t t p y e, ope t a r o , r
, d o h t e m p o r p e , y t r r e x e d n i , , d l e i f d a e r y l n o f d l e i , constant, v e n e t}. The T C S e d f n i e s
a v r o i us d a orn n e m ts that a m y be associa e t d i w th a i g ven mem e b r. r o F x e ampl , e e c a h
me e b m r has a g e v i n i v i s bility trait (e.g., p b u lic, pr v i ate, pro e t cted, and so for h t ) . Some
mem e b rs may be d c e a l red s a s b a tr c a t to enforce a o p lymor h p ic v a h e b ior n o e d riv d e
t p y es as well s a a u t r i v l o t de n i f e a c n a ned (b t u ov r e ri a d b ) e l im l p em n e a t t n o i . Als , o
t s o m s r e b m e m may be n o c figured as sta i t c b ( o d n u at t e h cl s s a leve ) l or i s n tan e c
(bound at t e h o j b c e t le e v l). e h T n o c st u r ction f o
t p y e mem e b s r is a x e mi d e n over t e h o c u e s r of the n x e t v e s e a r l a h c t p ers.
c i s n i r t n I S T C t a D a Ty s e p
e h T l a n i f t c e p s a f o e h t S T C o t e b a e r a w of for h t e ti e m n i e b g s i h t t a it e t s b a i l s e h s a well
e d f d e n i set of co e r da a t t p y es. t l A h g u o h a given language typ c i ally a h s a unique
r o w y e k d u d e s to e d cla e r an intrinsic CTS data t p y e, a l l language k y e o w rds u i t l mately
s s a m e y l b na e m d c s m or d . b i l ll . r e d i s n o C l b a T e
1- , 3 wh h c i c o d u s t n e m w o h y e k S T C da a t y t pes are p x e e r d e s s n i s u o i r a v . E N T
u g n a l a e g s .

Ta e l b 1-3. e h T r t n I insic CTS D a t a Types
a t a D S T C T e p y B V N . ET e K yword # C e K yword d e g a n a M s n o i s n e t x E o f r + + C K w y e d r o
Sys m e t . t y B e y B te t y b e d e n g i s n u r a h c
Sys m e t . y B S te S y B te sb t y e d e n g i s r a h c
Sys m e t . t n I 16 S o h rt sh r o t t r o h s
Sys m e t . t n I 32 I t n e r e g n i t n i t or g n o l
Sys m e t . t n I 64 Long long 4 6 t n i _ _
Sys m e t . n I U t16 h S U ort u r o h s t d e n g i s n u t r o h s
Sys m e t . n I U t32 n I U t r e g e n i u t d e n g i s n u n i t or d e n g i s n u g n o l
Sys m e t . n I U t64 o L U ng ul n o g d e n g i s n u 4 6 t n i _ _
Sys m e t . n i S gle n i S gle o l f at t a o l F
Sys m e t . u o D ble u o D ble u o d ble e l b u o D
Sys m e t . j b O ect j b O ect o j b ect O t c e j b ^
Sys m e t . a h C r h C ar a h c r t _ r a h c w
Sys m e t . r t S ing r t S ing s n i r t g S g n i r t ^
Sys m e t . c e D a m i l e D ci a m l dec a m i l l a m i c e D
Sys m e t . o o B a e l n o B ol a e n o b ol l o o B

1. 2 1 U e d n s r ta d n i g n the Co m m on Language Sp c e ifi a c o i t n
As o y u e r a a , e r a w f i d f t n e r e s e g a u g n a l x e p s s e r the s m a e o r p g a r i m m g n t s n o c r s t c u in
, e u q i n u e g a u g n a l c i f i c e p s s m r e t . r o F e l p m a x e , n i C# u o y e t o n e d s g n i r t n e t a c n o c a n o i t

e D pt. f o T I B J S , E S I e g a P 3 2
v l o s e r e to the a s me ty e p e d fined in an
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

g n i s u e h t s u l p r o t a r e p o , ) + ( e l i h w in B V .N T E u o y ty i p c l l a y a m ke use of t e h am e p r a s d n
(&). n e v E e h w n w t o i d s c n i t t la s e g a u g n p x e s e r s t e h same pr g o ramma i t c idiom (e.g , . a
u f o i t c n n t i w h no retu n r value , ) t e h ch n a ces are v r e y d o o g that h t e s x a t n y w l i l a p p ear
e t i u q t n e r e f f i d n o e h t : e c a f r u s
' B V T E N . d o h t e m g n i n r u t e r g n i h t o n .
c i l b u P b u S ) ( d o h t e M y M
' e m o S g n i t s e r e t n i e d o c . . .
d n E b u S

/ / # C d o h t e m g n i n r u t e r . g n i h t o n
c i l b u p d i o v ) ( d o h t e M y M
{
/ / e m o S g n i t s e r e t n i e d o c . . .
}
As u o y a h ve a e r l a y d seen, these i m o n r s n y tactic a v riatio s n r a e in o c s n q e uential in t e h
. T E N , e m i t n u r g n e v i t t a h h t e s e r pe v i t c e p m o c i e l rs ( b v . c x e e r o x e . c s c e, in
this ca e s ) em t i a simi a l r e s t of CIL i s n r t u i t c n o s. Ho e w v r e , l n a gua e g s n a c l a so d f i fer i w th
g e r a d r o t h t e r i ov r e a l l e l vel of functio a n i l ty. r o F , e l p m a x e a T E N . e g a u g n a l y a m r o y a m
t o n e v a h a d r o w y e k o t t n e s e r p e r d e n g i s n u , a t a d d n a y a m r o y a m t o n t r o p p u s r e t n i o p
e p y t s. n e v i G e s e h t e l b i s s o p , s n o i t a i r a v t i d l u o w e b l a e d i o t e v a h a e n i l e s a b o t h c i h w a l l
. T E N - e r a w a s e g a u g n a l e r a d e t c e p x e o t m r o f n o c . h T e Co m m n o Langua e g Sp c e ifica i t n o
( S L C ) is a e s t of r l u es that e d scr b i e in v d i v i deta l i t e h i m i n l a m a d n com l p e e t set f o
e f a s e r u t a v i g en .NET-awa e r c i p m o ler must su t r o p p to r p o u d e c code that n a c be
h s o ted by the C R L , wh l i e at the a s me time be d e s s e c c a in a unif r o m mann r e by all
l n a gu e g a s h t at t g r a et the N . ET platform . In m n a y w y a s, the L C S a c n be viewed as a
u s bs t e of the full f n u ctiona i l y t defin d e y b the CTS .
The CLS is i t l u mately a set of ru e l s that compi e l r bu l i d r e s must conform to, if they
intend their products to c n u f i t n o sea l m e s s ly n i h t i w h t e .NET n u ive s r e. E c a h rule is
s s a igned a s m i p e l name (e.g , . CLS Rule 6) and describ s e how i h t s rule affects h t ose
w o h bu l i d t e h c l i p m o ers as we l l as those who (in some w y a ) inte c a r t h t i w them. The
e m r c e d a l cr e m of the C S L s i the i m t h g y Rule : 1
u R e l 1: CLS ru e l s p a ply n o ly to h t s o e a p rts of a type that are expo e s d ou i s t de h t e
e d fini g n
l b m e s s a y. v i G en this rule, o y u can (correctly) n i fer h t at the remaining ru e l s of the CL S
do n t o app y l to t e h o l g c i used to u b i d l e h t n i n r e o w r g n i k s of a N . ET p y t e. The n o y l
a e p s cts of a t p y e t a h t m s u t con o f rm o t t e h CLS a e r the me b m er de t i n i f i n o s
e h t mselv s e . e . i ( , nami g n conve t n i n o , s parameters, and e r u t n r p y t es). h T e
m i pleme t n a o i t n o l gic for a mem e b r m y a e s u n a y num r e b of o n n- L C S t c e hniques , as
the outs d i e o w rld n o w t k o n w h t e di r e f f e c n e.
s i t o n S L C - t n a i l p m o c , s a e h t para t e m e s r a d n t e r u n r va u l s e make use of uns g i ned a d ta
c i h w ( h is not a requ r i e e m nt of h t e L C S : )
c i l b u p s s a l c c l a C
{
/ / d e s o p x E d e n g i s n u a t a d s i t o n S L C ! t n a i l p m o c
c i l b u p g n o l u d d A ( g n o l u , x g n o l u ) y
{ n r u t e r x + } ; y

e D pt. f o T I B J S , E S I e g a P 4 2
y e s e of h t e
To l i lus a r t t , e h t e fo l l o i w ng d A d() me d o h t
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

}
, r e v e w o H f i u o y e r e w o t m i s p y l e k a m e s u f o u n g i s n ed a t a d e t n i rn l a ly s a f l o l s w o :
c i l b u p s s a l c c l a C
{
c i l b u p t n i t n i ( d d A , x t n i ) y
{
/ / s A s i h t g n o l u e l b a i r a v s i y l n o d e s u , y l l a n r e t n i
/ / e w e r a l l i t s S L C t n a i l p m o c .
g n o l u ; p m e t
. . .
n r u t e r x + ; y
}
}
u o y e v a h l l i t s d e m r o f n o c to h t e r l u es of h t e C S L , d n a a c n t s e r a s s ur d e h t at all . T E N
l b a e to i v n oke h t e d A d() m h t e od. f O , e s r u o c n i n o i t i d d a o t e l u R , 1 e h t
S L C s e n i f e d u o r e m u n s r e h t o s e l u r . r o F exampl , e e h t C S L e d c s ri s e b how a given
l g n a u e g a u m st r p e s e r ent t x e t strin s g , how e u n e m i t a r n o s shou d l be e r p e r sented
in e t rna y l l ( h t e b s a e y t pe us d e for e g a r o t s , ) h w o to d f e n i e a t s tic e m m e b r , s d n a so for h t .
, y l i k c u L u o y t n o d e v a h o t o c t i m m e s e h t s e l u r o t y r o m e m o t e b a t n e i c i f o r p T E N .
e v e d l e p o r. , n i a g A y b d n a , e g r a l n a m i t n i a e t g n i d n a t s r e d n u f o e h t S T C d n a L C S
s n o i t a c i f i c e p s s i y l n o f o t s e r e t n i o t r e l i p m o c / l o o t s r e d l i u b .

n E s r u ing CLS Com l p ian e c
As y u o i w l l see over t e h cou s r e of this book, C# d e o s def n i e a numb r e of pr g o ra i m m ng
n o c structs that are o n t S L C - o c mpliant. The o o g d new , s howev r e , is that you ca n
n i struct h t e # C compiler to h c eck u o y r d o c e for S L C o c p m lia e c n s u i g n a n i s g e l E N . T
t t a r t u b i e:
/ / l l e T e h t # C r e l i p m o c o t c e h c k r o f S L C . e c n a i l p m o c
a [ s b m e s y l : y S t s S L C . m e C l p m o i t ( t n a r e u )]

1.13 h T e Assem l b y/Nam s e pace/Type Dis n i t ct n o i
h c a E f o s u r e d n u s s d n a t h t e impo t r a c n e of o c e d i l braries. The i o p nt of b i l a r i r es s c u h s a
, C F M , E E 2 J and ATL is to gi e v deve o l pers a we l l -defined e s t of exis i t g n d o c e o t
leverage n i their p a i l p ca o i t s n . , r e v e w o H e h t # C e g a u g n a l s e o d t o n e m o c h t i w a
e g a u g n a l - c i f i c e p s e d o c y r a r b i l . t a R h , r e C# de s r e p o l e v e v e l r g a e the la g n ua e g -ne t u r l a
. E N T l b i raries. To e k ep l l a h t e e p y t s wi i h t n t e h base c a l s s l b i a r s e i r w l l e orga e z i n d, h t e
. T E N p m r o f t a l k a m s e e n e t x s e v i e s u f o h t e na c a p s e m e co p e c n t. o T clarif , y Fi u g re 1-4
sh s w o a s r c een s o h t f o the Vi u s al o i d u t S 2005 O e j b t c w o r B er u i t l t i y. T s i h tool all w o s you
o t x e a e n i m e h t s s a e s e i l b m f e r ere d e c n y b r u o y t n e r r u c p j o r e , t c t e h a n mes s e c a p i w thin
a a p rticu a l r s a e s mbl , y the t p y es i w thin a gi e v n na s e m pace, and the memb r e s of a
h t at l d . b i l r o c s m l co t n ains a m y n di e f f r n e t names c a p es, ea h c with its
own e s mantical y l rela e t d y t pes.






e D pt. f o T I B J S , E S I e g a P 5 2
u g n a l a e g s r a e
e p s cific ty e p . e t o N
C# d n a r P T E N . m m a r g o i g n I 6 0 S 1 6 7


























e r u g i F 1-4. A single a s s m e bly can a h ve n a y numb r e of a n e m a p s c s e .

1.14 h T e A s s emb / y l Namespa e c /Ty e p D s i ti t c n o i n
h c a E f o s u r e d n u s s d n a t h t e impo t r a c n e of o c e d i l braries. The i o p nt of b i l a r i r es s c u h s a
, C F M , E E 2 J and ATL is to gi e v deve o l pers a we l l -defined e s t of exis i t g n d o c e o t
leverage n i their p a i l p ca o i t s n . , r e v e w o H e h t # C e g a u g n a l s e o d t o n e m o c h t i w a
e g a u g n a l - c i f i c e p s e d o c y r a r b i l . t a R h , r e C# de s r e p o l e v e v e l r g a e the la g n ua e g -ne t u r l a
. E N T l b i raries. To e k ep l l a h t e e p y t s wi i h t n t e h base c a l s s l b i a r s e i r l l e w orga e z i n d, h t e
. T E N p m r o f t a l k a m s e e n e t x s e v i e s u f o h t e na c a p s e m e co p e c n t. m i S p y l pu , t a
a n m s e pa e c is a gr p u o ing of relat d e typ s e n o c tai d e n in an as e s mbly. r o F x e amp , e l
Sys m e t .IO na e c a p s e m n i a t n o c s l i f e I/O l e r ated t e p y s, the Sy m e t s . a D ta namespa e c
s e n i f e d c i s a b e s a b a t a d , s e p y t d n a o s n o . t I s i y r e v t n a t r o p m i o t t n i o p t u o t a h t a e l g n i s
s s a embl y s ( uc h as o c s m r d . b i l ll) n a c n i a t n o c y n a b m u n er f o na e m spa , s e c a e ch of
i h w ch can n i a t n o c any nu b m er of ty s e p .
Ta e l b 1-4. A Sa g n i l p m of T E N . Namespaces
. E N T a N m s e p e c a g n i n a e M n i Life
Sys e t m i Wt i h n s y S tem you fi d n numero s u s u e u f l types dea i l g n t i w h


e D pt. f o T I B J S , E S I e g a P 6 2
h t e
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

c i s n i r t n i , a t a d h t a m l a c i t a m e , s n o i t a t u p m o c m o d n a r
r e b m u n e g n r e a o i t n, v n e i n o r m n e t a v riables, d n a gar a b ge
o c e l l ct o i n, s a we l l s a a nu b m er of c m o mon y l us d e
e c x e p s n o i t n a d at r t ibu e t s.
Sys m e t . l o C e l c o i t ns e s e h T na e m s s e c a p n i f e d e a m u n ber of o t s ck co t n a n i er
e j b o cts
( r r A a s i L y t, Q e u ue, a d n so forth), as we l l as b s a e

and interfac s e t a h t a l l ow y u o to b l i u d custo i m e z d
co l l c e o i t ns. As of .NET 2.0, t e h collec i t on t p y s e

be n e e t x e d n d e i w th g e n e ric ca a p i b l . s e i t i
e s e h T s e c a p s e m a n a e r d e s u r o f t n i erac i t ng with a d a t a b s s e


1.15 e D o l p ying the .NET R n u time

t I sho l u d come as no surpr s i e t a h t E N . T assemblies can be ex c e uted only on a a m chine
th t a s a h h t e . T E N F a r mework ins a t l e l d. s A an individ a u l h w o bu l i ds .NET softwa , e r this
h s o l u d nev r e be n a s i s , e u s a u o y r t n e m p o l e v e d a m c n i h e w l i l e b pr e p o r y l n o c f u g i e r d at
e h t e m i t u o y t s n i a l l e h t y l e e r f e l b a l i a v a . T E N k r o w e m a r F 0 . 2 K D S (as l l e w as
l a i c r e m m o c T E N . t n e m p o l e v e d e m n o r i v n e nts h c u s s a i V su l a t S u o i d 2 0 0 5). , r e v e w o H f i
u o y y o l p e d an y l b m e s s a o t a co r e t u p m h t at s e o d t o n e v a h T E N . i s n l a t e l d, it wi l l fail o t
n u r . F r o this rea o s n, Mi r c osoft p o r d i v es a set p u p c a kage nameddo e n t tf e . x xe t a h t a c n
be r f e e y l p i h s ped and installed along with u o y r o t s u c m softw e r a . s i h T n i stall t a ion
m a r g o r p s i n i c e d u l d t i w h the .NET Framework 2.0 D S K, and t i s i also free y l
d w o l n oad b a le from Mc i rosoft. e c n O e n t o d t x e . x f e s i , d e l l a t s n i e h t t e g r a t e n i h c a m l l i w
w o n n i a t n o c e h t T E N . e s a b s s a l c , s e i r a r b i l T E N . e m i t n u r ( o c s m r . e e dll , ) a d n a d d i n o i t al
. T E N e r u t c u r t s a r f n i ( h c u s s a e h t C A G ).

1.16 h T e Plat o f rm- d n I ependent Nat r u e f o .NET

To close this a h c pter, a w o l l me to r b ief y l o c mment on t e h platform-i d n e n e p d n e t a n t r u e
of t e h .N T E l p a r o f t m. To t e h u s rpri e s of most e d velope s r , .NET as e s mb i l es a c n be
e d vel d e p o and u c e x e t d e on n o n - i M r c osoft op r e g n i t a systems ( a M c S O X, e m u n r u o s
x u n i L d n o i t u b i r t s i s, , S O e B d n a , D S B e e r F o t a n me a few). o T d n u e s r t n a d how this i s
po i s s bl , e o y u n d e e to o c me to terms to e y t o n a ther b b a re i v ation in the E N . T u i n verse:
L C I C ( o m m on a L ng a u ge n I fra t s ruc r u t ) e . Wh n e Mc i rosoft re a e l sed t e h C#
g o r p i m m a r ng a l n u g g a e and the . T E N platfor , m t i also craft d e a set of formal
u c o d m n e ts t a h t descri e b d the n y s tax d n a sem n a i t s c of e h t C# d n a CIL lang a u g s e , t e h
. T E N assemb y l format, core .NET na e m space , s and the e m chanics of a hypothet c i al
. E N T runtime en i g ne k ( nown as the V r i l a u t e x E cu i t n o S s y te , m or VE ) S . Be t t er e y , t th s e e
mi t t d e to Ecma In e t n r a l a n o i t s a o f f l a i c i in e t rna n o i t al s d r a d n a t s
( a m c e . w w w / / : p t t h - r o . l a n o i t a n r e t n i g ). e t n i f o s n o i t a c i f i c e p s e h T r a t s e r e
ECMA-334: h T e C# La u g n age Spe i c fica i t on

e D pt. f o T I B J S , E S I e g a P 7 2
Sys m e t . l o C e l c n o i t s e G . n r e ic
ty e p s
have

Sys m e t . t a D a
i s u ng
o d u c e m nts have been b u s
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

: s n o i t s e u Q d e d n e m m o c e R
1. Ex n i a l p e h t g n i l i u b k c o l b f o T E N . fr k r o w e m a
2. g n i r B t u o e h t t n a t r o p m i e r e f f i d n s e c b n e e w t e i s ngle d n a e l i f i t l u m s e i l b m e s s a
3. ? s e c a p s e m a n e r a t a h W d n a t s i L n i a l p x e h t e e s o p r u p f o t a t s a e l e v i f s e c a p s e m a n .
4. t a h W s i T E N . ? h t i W a t a e n m a r g a i d ex n i a l p e h t t n a t r o p m i u b i g n i d l s k c o l b of T E N .
m r o f t a l p .












































e D pt. f o T I B J S , E S I e g a P 8 2
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7


g n i d l i u B # C s n o i t a c i l p p A
T I N U -2

1 . 2 e h T e l o R f o e h t d n a m m o C e n i L r e i l p m o C c ( c s e . x ) e
2 . 2 g n i d l i u B C # n o i t a c i l p p A g n i s u e . c s c xe
3 . 2 g n i k r o W h t i w e x e . c s c e s n o p s e R , s e l i F G g n i t a r e n e Bug
s t r o p e R
4 . 2 e h T d n a m m o C e n i L r e g g u b e D ) e x e . g b d r o c (
5 . 2 g n i s U , e h t l a u s i V t S u o i d T E N . E D I 6
6 . 2 r e h t O Key s t c e p s A f o e h t T E N . S V D I E
7 . 2 : r o s s e c o r p e r P # C s e v i t c e r i D
8 . 2 n A g n i t s e r e t n I e d i s A : e h T S m e t s y . m n o r i v n E e t n l C s s a .

































e D pt. f o T I B J S , E S I e g a P 9 2
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

2.1 e h T C# Com a m d n -Li e n Comp l i er c ( sc.exe)
r e h T e e r a a b m u n er f o e u q i n h c e t s you a m y u e s to o c mp l i e C# so c r u e co e d . n I ad i t i d n o
to i V a u s l t S u i d o 2005 s a ( well as va i r o s u h t ird-party .NET I E D s , ) you a e r a l b e to cre e t a
e h t # C d n a m m o c - e n i l m o c p , r e l i s c c e . xe h w ( e e r csc s d n a t s r o f C-
S l i p m o C p r a h er). s i h T l o o t s i d e d u l c n i h t i w h t e T E N . k r o w e m a r F 0 . 2 K D S . e l i h W t i s i tr e u
t a h t u o y y a m r e v e n e d i c e d o t d l i u b a e g r a l - e l a c s app i l a c i t on usi g n t e h command- n i l e
o c mp l i e , r t i is m i portant to un e d rstand the ba i s cs of how to
h n a d. I n a c k n i h t f o a w e f s n o s a e r u o y d l u o h s t e g a p i r g n o e h t : s s e c o r p
h T e m s o t obvious re s a on is h t e s m i ple a f ct that you i m ght o n t h v a e a copy of V u s i a l
u t S dio 2005.
u o Y p n a l to k a m e use f o au m o t a d e t bu l i d to s l o c u s h s a MS u B ild or A N nt.
o Y u w n a t to e d e e p n u o y r understanding of C#. When y u o use r g aph c i al IDEs to bu l i d
ca i t , s n o u o y are m i t l u ately instruc i t ng s c c x e . e h w o to manip l u t a e your C# input
An e h t o r n c i e
y b -product of o w rki g n i w h t c e . c s xe n i e h t raw s i th t a you b c e ome t a h t m h c u more
o c mfort b a le m n a i u p lati g n other o c mm d n a - i l ne t o o ls incl d e d u w t i h the N . ET
Frame o w rk 2.0 SDK. As y u o wi l l see h t ro g u o h ut t i h s book, a num e b r of m i o p rt n a t
s e i t i l i t u e r a c a c s e sible only m o r f t e h co m m a d n e n i l .
o C nfiguring e h t C# Co m m a d n -Li e n C l i p m o r e
Before y u o n a c be i g n o t ma e k e s u of t e h C# comm d n a - i l e n o c mpiler, y u o e e n d to
deve e m p o l nt e n i h c a m s e z i n g o c e r h t e i x e s c n e t e f o x e . c s c e. f I r u o y
e n i h c a m s i t o n d e r u g i f n o c , y l t c e r r o c u o y e r a d e c r o f o t y f i c e p s h t e l l u f h t a p o t h t e
i d rec o t ry containing s c c e . xe b f e ore you c n a o c mp l i e your C# f l i es.
o T p i u q e r u o y de t n e m p o l e v n i h c a m e to e l i p m o c *.cs s e l i f m o r f y n a , y r o t c e r i d w o l l o f
e s e h t
d f i fer sli h g tly):
1. R g i ht-c i l k c t e h My Com u p t r e icon and e s lect Pr p o er i t es f o r m the p o p -up menu.
2. e l e S ct e h t d A v n a ced tab and click the En i v ron e m nt Variables bu t t on.
3. e l b u o D - k c i l c e h t P t a h e l b a i r a v m o r f e h t Sy m e t s e l b a i r a V s i l st o b x.
4. A d d h t e fol o l i w ng line to the end of t e h cur n e r t Pa h t va u l e (n t o e e c a h value in th e
Path a v r a i ble is e s parated by a se i m colon):
: C \ d n i W s w o \ i M c s o r o . t f T E N \F w e m a r ork\ 2 v . 5 . 0 0 5 1 2
Of o c u s r e, o y ur entry m y a d e e n to be adjusted a b s d e n o o y ur curre t n v r e sion d n a
loca i t n o of the .NET Framework 2.0 S K D s ( o e b su e r to do a sani y t e h c ck s u in g
Wi w o d n s Ex l p orer). O c n e y u o ha e v p u dat d e h t e a P th varia l b e, o y u may t e k a a test run
y b c o l sing any o c mma d n wi d n ows open in h t e a b ckg o r und o t ( c m o mit t e h setti g n s , )
d n a n e h t p o e i n ng a new c m o a m nd wind w o d n a ent r e i g n
c s c ? /
f I you set things up co r r e t c y l , you shou d l e s e a list of o t p ions sup o p rted by h t e C#
c m o pi e l r.

o C n g n i r u g i f Ad l a n o i t i d N . T E C m o a m nd- n i L e T o o s l
e r o f e B u o y n i g e b o t in t a g i t s e v e . c s c exe, d a d t e h follo i w ng add t i i n o l a Path a v ri b a le to
t e h System
: ) y r o t c e r i d

e D pt. f o T I B J S , E S I e g a P 0 3
.N T E a s s embli s e using
co e l i p m r u o y c . * s s e l i f b y
ap l p i
fi e l s. n I s i h t i l h g , t t i s f i d e ying o t e s e h w at takes p a l ce b h e n i d the sc n e es.
ensu e r h t at o y ur
steps ( h w ich as u s me a i Wndows XP i s n t l l a a i t o ; n i Wndows T N /2000 steps will
s e l b a i r a V t s i l o b x , n i a g a ( e p f r orm a t i n a s y h c eck o t e s n ure a a v lid
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

: C \ s e l i F m a r g o r P \ f o s o r c i M l a u s i V t o i d u t S 8\ K D S \ 0 . 2 v \ n i B
c e R a l l h t at th s i r i d e r o t c y contains addi l a n o i t o c mmand- n i l e o t o s l th t a r a e o c m m y l n o
d e s u u d r g n i N . ET development. W t i h h t ese t o w pa h t s established, o y u should now e b
e l b a o t run n a y N . ET uti t i l y f o r m any co m m and win w o d . f I u o y i w sh to n o c fi m r i h t s
w e n n i t t e s g, c s o l e any p o en c m m o and i w ndows, op n e a e n w co n a m m d n i w do , w n a d
n e ter t e h fo n i w o l l g command to view t e h p o o i t s n of
l i t u c a g ? /

2.2 u B l i ding C# Applicat o i ns Using csc.exe
Now t a h t y r u o de e v l p o m n e t m c a hi e n r c e n g o izes cs x e . c e, e h t t x e n o g al is o t b i u d l a
m i s p e l n i s g e l f l i e s a e s mbly nam d e Te p A t s p.exe g n i s u e h t # C com a m nd-li e n comp l i er
d n a No p e t ad. Fir t s , o y u
l l o f o i w g n :
/ / A e l p m i s # C n o i t a c i l p p a .
g n i s u ; m e t s y S

s s a l c p p A t s e T
{
c i l b u p c i t a t s d i o v ) ( n i a M
{
e l o s n o C ! g n i t s e T " ( e n i L e t i r W . , 1 , 2 ; ) " 3
}
}
On e c o y u a h ve finis d e h , save t e h file in a conveni n e t loca i t on e ( .g., C:\CscE a x mp ) e l
s e T t c . p p A s. , w o N s t e l t e g o t w o n k e h t e r o c s n o i t p o f o e h t # C r e l i p m o c . h T e first
point of e t n i rest is o t u e d n rstand how to s e p ci y f h t e name n a d type of as e s mbly to
e r c a e t . e ( g , . a l o s n o c e n o i t a c i l p p a na d e m
b i L h t a M . l d l, a W n i d w o s o F rms ap c i l p at o i n named W y M i . p p A n exe, a d n so for h t ). E h c a
o p s i s b l i ity is re r p e e s t n ed by a c e p s i c i f g a l f pa e s s d n i to x e . c s c e s a a c m m o a d n - e n i l
r a p a r e t e m ( e s e Tab e l 2-2).
Ta e l b 2-2. Outp t u -centric p O tio s n f o the C# o C i p m ler
o i t p O n g n i n a e M n i Li e f
u o / t This o i t p on is u e s d to c e p s i y f t e h name of t e h s a sembly to e b
created. y B
defa , t l u t e h assembly a n me is t e h a s me as t e h a n me of h t e in i t i al
n i put s c . * l i f e i ( n the c e s a of a . * l d l) or t e h name of t e h t e p y
Ma ( n i ) me h t d o (in t e h ca e s f o n a . * exe . )
/ta e g r t x e : e s i h T n o i t p o s d l i u b n a e u c e x t l b a e c s n o ole a p p a c i l tion. T s i h s i the
f e d a l u t fi e l ou t u p t t p y e, d n a t u h s a m y be i m o e t t d w e h n b i u ldin g
i h t s
i t a c i l p p a on t p y e.
/ta e g r t i l : r b ary s i h T op o i t n d l i u b s a si e l g n -f l i e *. l d l l b m e s s a y.
/ta e g r t o m : l u d e i h T s op i t on bui d l s a d o m u e l . Modules are elem n e ts of i t l u m f l i e
a e s s m i l b es f ( l l u y c s e d r b i ed n i t p a h C r e 1 1 ).

e D pt. f o T I B J S , E S I e g a P 1 3
e h t C A G , y t i l i t u u c a g ti e . l xe :
ne d e some source c d o e . Op n e Note d a p d n a ent r e th e
as
MyS x e . l l e h e, a e d o c b i l r a r y e m a n d
conta n i n i g t e h p m a r g o r s
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

/ta e g r t i w : x e n e t l A hough you are fr e e o t u b l i W d i d n ows-b s a ed app i l a c i t s n o .

o T e l i p m o c Te A t s p c . p s o t n i a o c nsole a p p l c i a i t on a n med e T s e . p p A t xe, ch n a ge to t e h
contain n i g u o y r s c r u o e code file and enter the fo l l o i w g n o c m m and set (note
th t a com a m nd- i l ne fla s g m s u t come before h t e a n me of the input f e l i s, o n t after):
c s c / e x e : t e g r a t T A t s e p . p cs
e r e H I d i d t o n y l t i c i l p x e y f i c e p s n a u o / t , g a l f e r o f e r e h t e h t e l b a t u c e x e l l i w e b d e m a n
x e . p p A t s e T e,
n e v i g h t at e T s p A t p s i the s s a l c n i n i f e d g h t e g o r p r s m a e t n ry n i o p t ( h t e ( n i a M ) meth d o ).
Also be aware that m s o t of t e h C# co l i p m er flags p u s port n a b b a r a i v e ted e v rsi n o , s h c u
as /t a r t r e h than / g r a t et u o y ( n a c i v ew all a b b e r i v a n o i t s by ent r e ing c s c /? t a t e h
d n a m m o c : ) t p m o r p c s c / e x e : t e T st . p p A cs r o m r e h t r u F e, e v i g n th t a the t / e : xe fl g a is
t e h e d fault u o tput us d e by the C# compi e l r, you could also o c mpile Te p A t s p c . s s m i p y l
y b g n i p y t c s c s c . p p A t s e T e T s p p A t . x e e a c n now e b run f o r m t e h command i l e n .
f e R e e r ncing Ext a n r e l As e s mb i l es
N x e t u , p le t s ex i m a ne how to co l i p m e an p p a li a c i t n o that m e k a s s u e of t s e p y defined
in a e s a p a r te . E N T s a s y l b m e . e p S a n i k g of which, just in case you are wonde i r ng how
your re e f r n e ce o t the S e t s y m C . n o s l o e t p y e, reca l l m o r f
a h C pter 1 that c s m o b i l r . l d l is l l a c i t a m o t u a y
i ( f f r o s m o e t n a r ge reason you i w sh to i d b a s le thi s
/no d t s lib a l f g). o T u l l i st a r te h t e process of re r e f c n e i g n e t x er a n l as e s mb i l s e , l t e s update
the s e T t p A p n o i t a c i l p p a o t d y a l p s i a W s w o d n i m r o F s m s e g a s e x o b . Open you r
Tes p A t p c . s l i f e a d n mo i d fy t i s a l l o f o : s w
g n i s u ; m e t s y S
/ / d d A s i h t !
s u i g n m e t s y S . d n i W . s w o F s m r o ;

s s a l c p p A t s e T
{
c i l b u p c i t a t s d i o v ) ( n i a M
{
! g n i t s e T " ( e n i L e t i r W . e l o s n o C , 1 , 2 ; ) " 3

/ / d d A ! s i h t
Mes g a s e x o B S . h " ( w o H l l e o . . .");
}
}
No i t ce t e h reference to t e h S e t s y m n i W . d s w o o F . rms nam p s e ace via the C# i s u ng keywo d r
i ( ntrod e c u d in C p a h t r e 1). e R c l l a that wh n e you ex l p ici l t y list h t e namesp c a es u e s d
t i w hin a g v i n e c . * s
dir c e tory
the # C c p m o i r e l un e d rstood
d e c n e r e f e r dur n i g h t e o c mp l i t a o i n o r p c s e s
, r o i v a h e b o y u a m y s i c e p fy h t e
f e l i , you a o v id the ne d e to m k a e u e s f o f l u ly qualified a n mes
(which can lead to ha d n cramp ) s . t A the c m o m d n a lin , e you m s u t i o f n rm
e . c s c xe i h w ch as e s mbly c t n o a n i s t e h us d e nam s e paces. n e v i G th t a u o y e v a h ma e d
e s u of e h t e M s o B e g a s x c s a l s, you must s e p cify the Sy . m e t s W w o d n i s. r o F m l d . s l assembly
s u i g n the e r / f c n e r e e g a l f (which can e b abbreviat d e o t /r):
c s c / : r s y S m e t n i W . d s w o o F . m r . s l l d e t t s a c . p p s

e D pt. f o T I B J S , E S I e g a P 2 3
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7


g n i l i p m o C e l p i t l u M e c r u o S s e l i F h t i w e x e . c s c
e h T t n e r r u c c n i a n r a n o i t f o the t s e T A e . p p xe p a i l p a c i t n o w s a cr a e t d e s u i g n a si g n le
c . * s e c r u o s e d o c file. Wh l i e it is perfectly p r e missi l b e to have all of your .NET types
f e d ined in a sing e l . * cs , e l i f o m st p o r e j cts are o c m d e s o p of m l u i t ple c . * s l i f s e to p e e k
r u o y e d o c e s a b a bit more fle l b i x e . Assu e m y u o v a h e auth r o ed an a d d i i t o a n l a l c ss
t n o c a d e n i n i a e n w i f e l nam d e l l e H o . g s M cs:
/ / e h T e g a s s e M o l l e H s s a l c
g n i s u ; m e t s y S
s u i g n m e t s y S . d n i W . s w o F s m r o ;

s s a l c e g a s s e M o l l e H
{
c i l b u p d i o v ) ( k a e p S
{
Mes g a s e x o B S . h " ( w o H l l e o . . .");
}
}
, w o N upd e t a your initial T A t s e pp class to m e k a u e s of this new type, n a d m o c me t n o t u
t e h pre
g n i s u ; m e t s y S

/ / t ' n o D d e e n s i h t e r o m y n a .
/ / g n i s u ; s m r o F . s w o d n i W . m e t s y S

s s a l c p p A t s e T
{
c i l b u p c i t a t s d i o v ) ( n i a M
{
! g n i t s e T " ( e n i L e t i r W . e l o s n o C , 1 , 2 ; ) " 3

/ / t ' n o D d e e n s i h t e r o m y n a . r e h t i e
/ / e M s e g a s x o B S . H " ( w o h e l l o . . . ; ) "

/ / e s i c r e x E e h t e g a s s e M o l l e H ! s s a l c
h e g a s s e M o l l e H = w e n ; ) ( e g a s s e M o l l e H
h.S a e p k ) ( ;
}
}
u o Y n a c comp e l i o y ur # C l i f es by i t s i l g n each input i f le x e l p c i l t i y :
c s c l l d . s m r o F . s w o d n i W . m e t s y S : r / s c . p p a t s e t s c . g s m o l l e h
s A an alte n r a i t ve, t e h C# o c mpiler al o l ws you to k a m e use of the wi c d l ard c a h racter (*)
o t o f n i m r c s c . x e e to in l c ude l a l *.cs l i f es contai e n d in h t e pro e j ct dir c e tory as p t r a of
t e h u c rre t n build:
c s c l l d . s m r o F . s w o d n i W . m e t s y S : r / s c . *


e D pt. f o T I B J S , E S I e g a P 3 3
vious Wi w o d n s Forms log : c i
C# d n a a r g o r P T E N . m m i g n I 6 0 S 1 6 7

h W en u o y n u r h t e r g o r p am a , n i a g h t e out u p t s i d i e c i t n al. h T e only i d e f f r n e ce bet e w en
the two app i l a c i t s n o is h t e f c a t t t a h t e h curre t n o l gic has been sp t i l am g n o mu i t l ple
l i f es.

R f e e e r ncing Multiple Ex e t r l a n s s A emb i l es
On a related note, h w at if y u o n d e e to reference num o r e us e e t x r a n l as e s mb i l s e s u i g n
csc x e . e? Sim
e p s c f i y multiple extern l a assemblies for the curr n e t a x e mpl , e but o s me p m a s le a s u ge
l l o f ows:
c s c / : r s y S m e t n i W . do s w . o F m r s l l d . ;S e t s y m r D . a g n i w . l l d s c . *

2.3 W r o king i w th cs . c exe Response Files
As you g i m ht gu s e s, if y u o e w re to build a complex C# applica i t on at the o c a m m nd
r p o t p m , your life would e b full of a p in as o y u type n i the l f g a s h t at p s ecify n e m u o r us
. * cs input f l i es. o T help e l s e s n y u o r typing u b rden, the C #
c m o pi e l r honors the use of re o p s nse files. C# res s n o p e f l i es co t n ain a l l the n i st c u r i t n o s
to be used r u d i g n t e h c l i p m o a i t n o f o your current
n i a . * rsp r ( es ) e s n o p extens o i n . s A m u s e t a h t u o y ha e v cr a e ted a res o p s n e file named
p p A t s e T . s r p t a h t contai s n the o f l l ow n i g ar u g ments ( s a o y u n a c see, o c mm n e ts a e r
d e t o n e d h t i w e h t # characte ) r :
# s i h T s i e h t e s n o p s e r e l i f
# r o f e h t e x e . p p A t s e T p p a
# f o r e t p a h C 2.

# l a n r e t x E y l b m e s s a . s e c n e r e f e r
n i W . m e t s y S : r / s w o d . r o F s m l l d .

# t u p t u o d n a s e l i f o t e l i p m o c g n i s u ( d r a c d l i w . ) x a t n y s
e x e : t e g r a t / e x e . p p A t s e T : t u o / s c . *
No , w as u s i m ng th s i file s i a s ved in the same d r i e t c ory as the C# s u o c r e code fi e l s o t be
y u o are able to build u o y r n e i t re p a plica i t on as follows (note the use of th e
@ ) l o b m y s : c s c T @ e A t s pp s r . p
f I the n e e d should arise, y u o are also able to e p s ci y f multiple *. s r p l i f es as input ( . e g , .
e S @ o c F d n . e l i s r p d r i h T @ F . e l i r p s ). If o y u t e k a th s i p a proach, do
c r e l i p m o s e s s e c o r p e h t c a m m o nd opti n o s as they a e r e c n oun r e t ed!
m n e ts n i a later *.rsp f e l i n a c ov r e ride p o tions in a
s l A o t o n e h t at s g a l f l s i ted p x e li i c l t y on h t e c n a m m o d l e n i
e r o f e b a r n o p s e se f e l i wi l l be v o r e d i r
t n e r e
c s c e x e . p p A l o o C y M : t u o /
the name of the a s s embly would s l i t l be e T s A t p e . p xe r e h t a r ( n a h t MyC A l o o p x e . p e ),
given the
/ou T : t e A t s p p . x e e f a l g s i l ted in the t s e T A r . p p sp re p s e s n o f l i e. Ho e w ver, if you i l st flags
after
p y l i l st each s s a e l b m y using a semi o c lon-de i m i l t d e list. Y o t d e e n t n o d u o
e r fe e r nced assem l b i s e n a d
bu l i d. y B v n o c e o i t n n, s e h t e i f e l s d n e
co p m iled,
c s c p s r . e l i F t s r i F @
e b aware t a h t t e h
r e h T e e r o f , c m o mand-l e n i a u g r
r p evious r s e p n o se e l i f .
e d n by t e h specified r . * sp e l i f . , s u h T f i o y u e w e r o t
p s r . p p A t s e T @
a e s n o p s e r l i f e, the g a l f i w ll e v o r r e d i sett s g n i in h t e res s n o p e f l i e.


e D pt. f o T I B J S , E S I e g a P 4 3
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

e h T t l u a f e D e s n o p s e R e l i F ) p s r . c s c (

T e h final o p t n i o t e b m e d a e r g r a di g n r p s e onse f l i es is t a h t t e h C# o c mp l i r e has n a
e s n o p s e r f e l i (cs r . c sp , ) whi h c is o l c e t a d in h t e same dire t c ory as
T E N . t f o s o r c i M \ k r o w e m a r F \ . 0 . 2 v 5 5 1 2 0 ) . f I u o y were o t
n e p o i h t s l i f e n i s u g , d a p e t o N u o y will f n i d
alre y d a e e b n c e p s ified s u n i g t e h r / : a l f g. Wh n e u o y a e r bu l i d n i g your # C pr g o a r m s
g n i s u c e . c s xe, this i f e l will be automatic l a ly refe n e r ced, ev n e w e h n you p p u s y l a
custom s r . * p l i f e. v i G en h t e pr s e ence of the d f e ault resp s n o e file, the n e r r u c t
Tes p A t p x e . e p a i l p a c i t n o could e b su c c essfu l l y comp l i ed using t e h f l l o o i w g n comm n a d
e s t (as Sys m e t . n i W do . s w F s m r o .dll s i refer n e ced w t i hin c c s . s r p : )
c s c e x e . p p A t s e T : t u o / s c . *
n I h t e e e v t n h t at you i w sh o t i d sa l b e e h t au m o t a i t c readi g n f o csc r . sp, you ca n
sp c e ify e h t o n / co i f n g : n o i t p o
c s c p s r . p p A t s e T @ g i f n o c o n /
y l s u o i v b O , h t e # C d n a m m o c - e n i l r e l i p m o c s a h a m ny r e h t o s n o i t p o a h t t a c n e b us d e to
l o r t n o c o h w t e h re u s l i t ng T E N . s a sembly is to be ge e n rat d e . f I you wish to lea n r more
func i t onal t i y of cs e . c xe, k o o l p u my a i t r e l c t t i led o W i k r ng t i w h
e h t C# 2 0 . Com n a m d Li e n o C mp l i r e o i l n e n at ht / : p t / n d s m . r c i m o o s ft o c . m.

2 4 . The Com a m nd-Line e D bugger (cord g b .exe)
Bef r o e movi g n on to our x e amin t a i n o f o bu l i ding C# l p p a i t a c i n o s s u ing TextPad, I
would like to brief y l point u o t that the N . T E Framewo k r 2.0 SDK o d es h s ip w t i h a
named cor x e . g b d e . This t o o l p o r vides do e z ns of p o i t n o s
t a h t a l l ow u o y to e d b g u y r u o s s a emb y l . You m y a view t e h m by specify n i g the /? fla : g
g b d r o c ? /
Ta e l b 2-3 do u c me s t n o s me ( u b t certainly n t o l a l) of h t e fla s g recogn z i d e by
cor g b d . x e e (with
e h t a t l e n r a e v i t h t r o h s a d n t a t o n i ) n o e c n o u o y e v a h t n e e d e r a e d bug i g ng e s ssion.

Ta e l b 2-3. A l u f d n a H f o Us f e l u cor . g b d exe C m m o n a d-L n i e Flags
Fl g a g n i n a e M in Li e f
b[r k a e ] t e S r o y a l p s i d t n e r r u c s t n i o p k a e r b .
del e t e [ ] Rem e v o o e n or more bre o p k a t n i s.
ex[ t i ] t i x E h t e r e g g u b e d .
o [ g ] i t n o C n e u g n i g g u b e d e h t r r u c e t n s s e c o r p l i t n u hi t t i g n e n xt
e r b a n i o p k t.
o[ut] t S ep t u o f o e h t cur e r nt fu c n t o i n.
p[r t n i ] Pr t n i l l a loa e d d a v riab s e l (local, a g r umen , s t etc.).
si St p e n i to the e n t x line.
so S p e t e v o r h t e next l e n i .

s A I assume th t a mo t s of you w l l i h c o s o e to m e k a s u e of t e h s i V u l a St i d u o 2 0 0 5
, r e g I l l i w not e h t o b r to o c m n e m t on e h c a flag of o c r g b d .exe. e v e w o H r ,


e D pt. f o T I B J S , E S I e g a P 5 3
a s s ociat d e de u a f t l
csc e . xe f l e s t i (e g . ., C:\W w o d n i s\
t a h t numerous . E N T as e s i l b m s e ha e v
deta l i s g e r ardi g n the
co m m a d n -li e n deb g g u er
t n i g e ra e t d e d u b g
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

r o f e s o h t f o u o y o h w a e r e t n i r s e ted, the f l l o o i w g n c e s o i t n pre n e s ts a i m nimal wa k l -
h t rough of t e h basic o r p cess f o u b e d gging at t e h c m m o a d n n i l e.

g n i g g u b e D t a e h t d n a m m o C i L n e
e B f r o e you can debug u o y r appl c i ation using co g b d r .exe, the fi t s r step is to ge e n ra e t
u b e d g n i g g y s mbols for y u o r u c r r n e t ap l p ic i t a on by s e p cifying h t e d / e u b g g a l f f o
csc x e . e. o F r ex m a ple, to ge e n rate e d bugg n i g da a t for T t s e A e . p p xe, ent r e t e h o f l l owi g n
o c mm d n a e s : t
c s c p s r . p p a t s e t @ g u b e d /
This g n e erates a e n w l i f e nam d e (in this case) testapp.pdb . If y u o do o n t have n a
s a c o s iated . * p b d file, it is s l l i t pos b i s le o t ma e k e s u f o co d r b . g exe ; e w o h v r e , o y u will
o n t e b able to view yo r u




te p a t s p e . xe



d i v ed by your I E D . f I u o y e r q i u re mo e r informatio , n lo k o up
cor g b d . x e e n i the .NET r F amework 2.0

2.5 U i s ng h T e, V s i u l a i d u t S o .NET ID E
e l i h W d a p e t o N s i e n i f r o f g n i t a e r c m i s ple N . T E pr g o r m a , s it o s r e f f n i h t o n g n i h t e wa y
t i v i t c u d o r p y. It would e b i e d al to u a thor *.cs fil s e usi g n n a e t i d or t a h t
w d r o o c lori g n , o c de sni p p ets, d n a integr t a o i n t i w h a C#
o c mp l i er. As luck w u o d l have i , t u s ch a tool d e o s x e i : t s TextPad. d a P t x e T s i n a r o t i d e
u o y n a c e s u o t a r o h t u d n a c e l i p m o c e d o for nu e m r u o s p o r grammi g n l g a u g n a s e ,
g n i d u l c n i # C . e h T chief advant g a e of this p o r u d ct is the fact that it s i e v ry s p m i le to u e s
a d n v o r p id s e just h g u o n e be l l s and i h w s l t s e to a h n e n e c u o y r d o c ing effor s t .
, d a P t x e T e t a g i v a n o t htt / / : p w e t . w w x d a p t .com a d n d l n w o oad the c r u rent e v rs o i n
at t e h time f o this wr t i ing). O e c n y u o have insta l l ed t e h pr u d o ct, u o y w l i l a h ve a
e r u t a e f - f o n o i s r e v e t e l p m o c T t x e P h ; d a o e v e w r h t , i o o t s l f t o n s i r e e ware. u o y l i t n U
s a h c r u p e a e l g n i s - r e s u e s n e c i l r o f ( r a ound US 0 3 $ .00 at the time of i h t s wr i t i , ) g n y u o
l l i w e b pres n e t d e h t i w a f rien l d y re i m e d n r each m i t e o y u n u r the applica o i t n.
d a P t x e T s i t o n d e p p i u q e o t d n a t s r e d n u # C s d r o w y e k r o k r o w h t i w x e . c s c e t u o f o e h t x o b .
o T o d o s , u o y l l i w e n d e o t n i sta l l n a add l a n o i t i add-on. e t a g i v a N o t
d d a / m o c . d a p t x e t . w w w / / : p t t h - l m t h . g 2 a n y s / s n o
d n a d a o l n w o d h s c arp i z . 8 p g n i s u e h t # C 5 0 0 2 k n i l o i t p o n. This add-on t k a es into
c c a ou t n h t e new
d e t i m i l o t # C 1 . 1 ). Once o y u h v a e un p i z ped cs r a h p8 z . ip, e c a l p a y p o c of h t e e a r t x c d e t
Samp e l s sub i d rec o t ry of the T x e tPad n i sta l l ation . e ( g.,


e g a P 6 3
# C so c r u e co e d during the pr e c o ss (which is t p y ically no f n u
h w a s t o e v e r, un e l ss you wish to complicate t t a m ers by reading C L I co e d ).
O e c n you ha e v e g nerat d e a p . * db f , e l i n e p o a s s e n o i s t i w h o c r b d x e . g e y b sp c e if i y ng
your .NET a s s emb y l as a comm n a d-l e n i arg m u e t n t ( he *.pdb e l i f will be d e d a o l
c i t a m o t u a l a y l : )
e x e . g b d r o c

n e h W u o y h s i w o t q i u t de g n i g g u b wi h t b d r o c g e x e . , si p m ly type exit r o ( e h t r o h s t d n a h
ex). g A a , n i un e l ss you are a c m m o a d n -line ju k n ie, I s a sume y u o l i w l opt for the
r g aphical de u b g e g r pro
SDK doc m u e t n a i t n o .
f o de e p o l e v r
supports (at a i m nimum) e k y
o T n i a t b o
(4.7.3
e k y s d r o w d e c u d o r t n i h t i w # C 5 0 0 2 n i ( t s a r t n o c o t e h t # C , k n i l c i h w h s i
csh p r a 8 y s . n f l i e in t e h


e D pt. f o I B J S , E S I T
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

: C \Pr g o a r m Files\TextPad 4\Sa p m les). , t x e N la n u ch T x e tP d a a d n pe o f r rm e h t foll i w o ng
a t sks using h t e New c o D u t n e m Wiza d r .
1. Ac i t vate the C n o figure w e N D m u c o e t n s s a l C e m nu opt n o i .
2. r e t n E h t e na e m C# . 2 0 in the Document class name d e t i box.
3. n I e h t t x e n t s e , p enter c . * s in h t e Cl s a s members ed t i box.
4. l l a n i F y, a n e b e l x a t n y s h g i l h g i h t g n i , c s o o h e s c h r a 8 p . y s n f o r m the drop-down i l st
x o b , d n a
close the wi a z d r . u o Y n a c w o n k a e w t s d a P t x e T # C o p p u s rt using e h t o D c m u ent
l C s a ses n d o e ac s e c sible from the Con i f gure.
Co f n iguring t e h c . * s Fi e l l i F ter
The e n xt c n o fig r u ati n o deta l i is to c e r a e t a f r e t l i for # C source code fi e l s displayed b y
the Open and a S ve dialog : s e x o b
1. Ac i t vate the C n o figure Prefere c n es menu p o i t on d n a sel c e t File Name t l i F e s r from
t e h
e r t e view control.
2. C i l ck t e h New b t t u on, d n a e t n er C# into the Descrip o i t n field and . * cs into the Wi d l
cards te t x box.
3.
o o H g n i k Into c s c .e e x
The a l st major o c nf g i ura i t n o e d ta l i to n o c tend t i w h is o t a s s ociate csc. x e e h t i w
compile your C# i f l s e . h T e i f r t s way to do so s i using the To l o s
n u R u n e m n o i t p o . e H re you a e r s e r p n e ted i w th a dialog box h t at a l l ows you to p s e i c y f
t e h name of the tool o t run and any e n c s e sary c m o a m nd- i l ne l f a s g . T e l i p m o c o
Tex a P t d s e T . t cs t n i o a .NET c n o sole-ba e s d executable, fo l l ow t e h se
1. e t n E r h t e l u f l path to cs x e . c e into the m m o C and e t xt b x o (e.g.,
: C \Wn i dows\Microsoft.NET\
Framew k r o \ 2 v .0. 5 1 2 0 5 \ e x e . c s c ).
2. E e t n r t e h command- n i l e p o i t ons u o y i w h s to sp c e ify t i w hin t e h a P a r meters text x o b
g . e ( . ,
x e . p p A y m : t u o / e s c . * ). e R l l a c that you a c n p s ecify a u c stom e r sponse file to s m i plify
ma r e t t s
e ( .g., @ I y m n . t u p rsp)
3. r e t n E e h t y r o t c e r i d g n i n i a t n o c e h t t u p n i s e l i f a i v e h t l a i t i n I r e d l o f t x e t x o b
: C ( \T d a P t x e T ) e l p m a x e s i h t n i p p A t s e .
4. f I y u o wish Te t x Pad to p a c ture the compi e l r u o tp t u di c e r l t y ( a r ther than w t i hin a
s p e arate
comm d n a i w d n ow), e s l c e t t e h Capture Output che k c o b x .
t A i h t s po , t n i o y u n a c h t i e r e run u o y r pr g o r m a by u o d ble- i l c ck n i g the e x e c l b a t u e us n i g
w o d n i W s
s A c o s ia i t ng Run o C m n a m ds w t i h M n e u t I ems
P t x e T ad l a so al s w o l u o y o t c e t a e r u c s m o t u n e m t i e s m t t a h re s e r p e t n pre e d f n i ed u r n
m m o c a d n s. Le t s e r c a e t a cus m o t t i em un e d r the o T o s l menu a n med C ompile C#
n o C s e l o h t at l l i w c m o l i p e all # C f l i s e n i h t e u c e r r t n i d e r o t c ry:
1. e t a v i t c A e h t e r u g i f n o C s e c n e r e f e r P u n e m n o i t p o n a d t c e l e s s l o o T m o r f e h t e r t e i v ew
l o r t n o c .
2. Us n i g h t e Add bu t t on, se e l ct Pro r g am and e p s ci y f the u f ll pa h t to s c c.exe.
e D pt. f o T I B J S , E S I e g a P 7 3
e v o M r u o y w e n r e t l i f o t e h t o t p f o e h t t s i l g n i s u h t e e v o M p U but n o t a d n c c i l k OK.
d a P t x e T o s u o y n a c
: s p e t s
Ex r o l p er or l e v e rage e h t Tools.
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

3. f I u o y , h s i w m a n e r e . c s c exe o t a e r o m e d s v i t p i r c e b a l el ( m o C p l i e # C ) y b c i l k c g n i e h t
l o o t
name and then clicki g n K O .
4. , y l l a n i F e t a v i t c a e h t u g i f n o C e r
c e l e s t
e l i p m o C # C m o r f e h t s l o o T e d o n , d n a i c e p s fy c . * s as h t e sole v u l a e in h t e P r a a t e m e s r
i f eld.
g n i r u D h t e r e m m u s f o , 4 0 0 2 o r c i M soft introduc d e a br d n a -new e n i l of I E D s h t t a f l a l
n o i t a n f o s s e r p x E s t c u d o r p ( m / / : p t t h s i m . n d c o s o r f e / m o c . t x s s e r p ) . T o
da , e t r e h t e are s x i membe s r f o the Ex r p ess family:
i V s l a u b e W e v e D l p o r e 2 0 0 5 Ex s e r p s: A t h g i e w t h g i l o t ol r o f u b i i d l ng n y d m a ic b e w i s tes
a d n
X L M w b e ser s e c i v using S A P.N T E 2.0
i V sual a B sic 0 0 2 5 Express: A s d e n i l m a e r t pr r g o a g n i m m o t ol e d i al for o n i v ce
m m a r g o r p s r e o h w want to e l arn o h w to build p a p c i l a i t ons using t e h e s u r-friendly synta x
of Vis a u l B s a ic .NET i V s l a u # C 5 0 0 2 x E p e r s s , i V s l a u C++ 0 2 05 x E r p , s s e n a d i V sual J#
0 2 05 x E press: Targeted D I Es for s u t dents a d n en h t usiasts who wish to learn the
d n u f am n e t s l a of o c m u p ter science in th i e r sy x a t n of i o h c ce
S L Q S e v r e 0 2 r 5 0 x E p e r ss: n A y r t n e - l e v e l e s a b a t a d t n e m e g a n a m m e t s y s d e r a e g d r a w o t
, s t s i y b b o h , s t s a i s u h t n e d n a t n e d u t s s r e p o l e v e d

. 2 6 Ot e h r Key Asp c e s t f o the VS.NET I E D
l a u s i V i d u t S o 0 0 2 5 i g v s e s u h t e ability to s e d i n g s a l c ses v u s i l a ly t u b ( this ca b a p ili y t is
t o n e d u l c n i d in l a u s i V # C 2005 Exp s e r ) s . e h T s s a l C r e n g i s e D t i l i t u y w o l l a s u o y to i v ew
d n a y f i d o m e h t i t a l e r o s p i h s n f o e h t e p y t s (c , s e s s a l t n i r e e c a f s, structu , s e r enum a r e t o i n , s
d n a l e d e e t a g s) n i r u o y r p o t c e j . g n i s U s i h t , l o o t u o y e r a e l b a o t y l l a u s i v d d a r o ( ) e v o m e r
s r e b m e m o t r o ( ) m o r f a e p y t d n a e v a h r u o y s n o i t a c i f i d o m d e t c e l f e r n i e h t g n i d n o p s e r r o c
# C fi e l . s A l l e w , s a you m i d o fy a gi e v n # C l i f e, chang s e a e r e r fle t c ed n i the class
diag a r m.
To o w rk with this aspe t c of Visual Studio 200 , 5 the fi t s r t s ep is to inse t r a new clas s
diagram f l i e. e r e h T e r a n a m y s y a w to do o s , one of c i h w h s i o t c c i l k h t e V e i w Cla s s
m a r g a i D bu o t t n c o l ated n o S l o u i t on Ex r o l p e s r right side
2 7 . # C Preprocessor i D rectives
i L ke a m ny t o her lan u g g a es n i the C fa i m ly, C# supports the use of v r a ious y s mbols that
l l a ow you to a r e t n i ct h t i w t e h m o c l i p a i t on pr e c o ss. e B f r o e examin n i g va i r ous C#
p e r p roc s e s r o i d r c e i t ves, le s t get our te m r n i ol y g o co r r c e t. h T e t r e m # C p e r pr s e c o sor
e v i t c e r i d s i t o n y l e r i t n e c a cur e t a . n I e r a , y t i l t s i h m r e t is e s u d l n o y for n o c s t s i e c n y w t i h
e h t C d n a + + C g o r p r m a m g n i la u g n a s e g . In C , # h t ere is no s p e a e t a r r p p e e c o r ssing t s ep.
, r e h t a R g n i s s e c o r p e r p e v i t c e r i d s a e r c o r p e d e s s s a a p t r of e h t le i x cal a y l a n sis ph e s a o f
t e h c m o p l i er .
n I y n a , e s a c e h t x a t n y s of h t e # C p e r c o r p e o s s r i d e r s e v i t c is e v ry s m i a l i r o t a h t t of h t e
r e h t o m e m s r e b f o e h t C fa , y l i m n i t a h t e h t s e v i t c e r i d e r a s y a w l a d e x i f e r p h t i w e h t d n u o p
n g i s ) # ( . e l b a T 9-4 n i f e d s e e m o s f o e h t e r o m y l n o m m o c d e s u s e v i t c e r i d t l u s n o c ( e h t T E N .
k r o w e m a r F 0 . 2 K D S n o i t a t n e m u c o d r o f e t e l p m o c ) s l i a t e d .
Ta e l b 9-4. Co o m m n C# Pr p e r c o essor i D r c e ti e v s
e r i D ctives g n i n a e M n i Li e f

e D pt. f o T I B J S , E S I e g a P 8 3
s e c n e r e f e r P me u n o n o i t p once a i a g n, b t u t s i h i t e m
e d n u r h t e e d s g i
C# d n a m m a r g o r P T E N . i g n 0 I 6 S 1 6 7

#re o i g n, #e r d n eg o i n Us d e to mark sections of collapsible s u o c r e code
#de n i f e, #u e d n f d e s U o t e n i f e d a d n d n u e n i f e co a n o i t i d n l o c mp l i a i t on y s mb l o s
i # f, i l e # f, #else, e # n i d f U d e s to con i t i d a n o l l y k s ip sec i t s n o of o s urce co e d (b e s a d
n o
specifi d e com i p l t a o i n symb l o s )
g n i y f i c e p S s n o i g e R e d o C
s p a h r e P e m o s of e h t m t s o u e s u f l of a l l p e r pr o s s e c o r e r i d c v i t s e r a e # e r g o i n n a d
e # n r d egi n o . Using
a y l d n e i r f l a u t x e t r e k r a m . Use of g e r io s n a c n help
e k ep lengthy c . * s f e l i s m e r o g a n a m e b a le. F m a x e r o
s e p y t t c u r t s n o c , s r o r e h t o n a r o f e p y t , s e i t r e p o r p d n a o s : h t r o f
s s a l c r a C
{
e t a v i r p g n i r t s ; e m a N t e p
e t a v i r p t n i ; p S r r u c

n o i g e r # s r o t c u r t s n o C
c i l b u p ) ( r a C
. . .
c i l b u p r a C t n i ( r a C , p S r r u c g n i r t s ) e m a N t e p
{ . . .}
n o i g e r d n e #

n o i g e r # s e i t r e p o r P
c i l b u p t n i d e e p S
{ . . . }
b u p c i l g n i r t s e m a N
{ . . .}
n o i g e r d n e #
}

2.8 An I r e t n s e ting A i s : e d e h T S s y te . m n E i v ronm n e t
Cla s s
t e L s exa i m ne the Sys e t m E . v n i m n o r ent class in g e r ater e d tail. i h T s cl s a s allows you t o
obtain a num
c i t a t s b m e m e s r . o T u l l i s r t ate i h t s c s s s a l s e n l u f e s u , s u d p ate
u o y r a M i ( n ) m t e d o h i w th t e h o f l l o i w ng l i g o : c
c i l b u p c i t a t s t n i ] [ g n i r t s ( n i a M ) s g r a
{
. . .
/ / S O g n i n n u r s i h t ? p p a
t n e r r u C " ( e n i L e t i r W . e l o s n o C : S O } 0 { , " ; ) n o i s r e V S O . t n e m n o r i v n E

/ / y r o t c e r i D g n i n i a t n o c s i h t ? p p a
t n e r r u C " ( e n i L e t i r W . e l o s n o C : y r o t c e r i D } 0 { , "
Env o r i n n e m . t C e r r u n i D t r c e o t r ) y ;

e D pt. f o T I B J S , E S I e g a P 9 3
{ }
t e h se t , s g a y u o are b a le to p s ecify a l b k c o of code t a h t m y a be
hi e d d n fr m o view d n a i n e d tifi d e by
, e l p you c u o ld create e n o o i g e r n for a
ber of e d tails r g e ardi g n t e h opera i t g n s y s tem curr n e l t y hos i t g n your .NET
p p a c i l a i t n o s u i g n various
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

/ / t s i L e h t s e v i r d n o s i h t . e n i h c a m
] [ g n i r t s s e v i r d = e v i r D l a c i g o L t e G . t n e m n o r i v n E s ; ) (
t n i ( r o f i = ; 0 i < ; h t g n e L . s e v i r d ) + + i
e v i r D " ( e n i L e t i r W . e l o s n o C { } 0 : } 1 { , " , i ; ) ] i [ s e v i r d

/ / h c i h W n o i s r e v f o e h t T E N . m r o f t a l p s i g n i n n u r s i h t ? p p a
g n i t u c e x E " ( e n i L e t i r W . e l o s n o C n o i s r e v f o : T E N . } 0 { , "
Env o r i n n e m . t V i s r e o ) n ;
. . .
}
The y S s . m e t E o r i v n n e m nt e p y t defines e m b m s r e t o her h t an tho e s p e r t n e s ed n i h t e
e r p o i v us
l p m a x e e. T b a le 3-1 do u c m n e ts o s me d d a i t i o a n l pr p o er i t s e of int s e r e ; t o h w e v e r, e b su e r
to check u o t the .NET Framework 2.0 D S K documentation for f l u l d a t e i s l .

Ta e l b 3-1. S t c e l e Pr p o er i t s e of Sy e t s m n E . vi m n o r ent
Pr p o erty n a e M i g n n i e f i L
Mac n i h e m a N e Ge s t the name of the c r u rent machine
New n i L e t e G s h t e n i l w e n e sym l o b r o f e h t c r r u e t n env r i n o e m n t
Pro s e c s C r o n u o t Re n r u t s h t e nu b m er of o r p cesso s r n o e h t cu r r ent m c a h n i e
Sys m e t D e r i t c ory Returns t e h fu l l a p th to t e h system dir c e to y r
Use m a N r e s n r u t e R e h t na e m f o e h t e y t i t n t a h t sta t r ed t s i h ap l p c i i t a n o

























e D pt. f o T I B J S , E S I e g a P 0 4
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

d e d n e m m o c e R : s n o i t s e u Q
1. n i l a p x E h t i w a t a e n m a r g a i d e h t w o l f k r o w t a h t a t k s e e c a l p b n e e w t e o y ur so e c r u o c d , e a
e v i g n T E N . r e l i p m o c n a d e h t T E N . ex n o i t u c e en e n i g .
2. w o H T E N . k r o w e m a r f t n e r e f f i d m o r f r e h t o g n i m m a r g o r p s t n e m n o r i v n e e k i l
a v a j , 6 B V , + + C , M O C ? c t e
3. f e i r B ly s s u c s i d e h t a t s te f o f a f s r i a t a h t t n e v e u l a ly d e l o t e h t T E N . m r o f t a l p . t a h W s i t e h
. T E N l o s n o i t u d n a t a h w # C s g n i r b t t e h t b a l s e ?
4. t a h W e r a e h t c i s a b d l i u b i g n s k c o l b f o . T E N f t a l p o m r ? Ex n i a l p e h t n o m m o c t e p y s m e t s y
n i e d ta l i .










































e D pt. f o T I B J S , E S I e g a P 1 4
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

T I N U -3
e h T # C n i m m a r g o r P g L n a u g age
1 . 3 e h T o t a n A m a f o y # C c i s a B l C s s a
2 . 3 g n i t a e r C b o j : s t c e c u r t s n o C t r o B c i s a s
3 . 3 e h T n o i t i s o p m o C f o a C# a c i l p p A t n o i
4 . 3 t l u a f e D s s A i t n e m n g a d n r a V i b a le S o c pe
5 . 3 e h T # C r e b m e M n o i t a z i l a i t i n I S x a t n y
6 . 3 c i s a B t u p n I d n a O t u p t u h t i w e h t e l o s n o C C s s a l
7 . 3 g n i d n a t s r e d n U l a V ue T s e p y d n a e r e f e R n e c T s e p y
8 . 3 e h T r e t s a M o N d : e y S s , m e t t c e j b O
9 . 3 e h T S m e t s y a t a D T s e p y d n a ( # C ) s e s a i l A
0 1 . 3 g n i t r e v n o C n e e w t e B e u l a V T s e p y d n a e c n e r e f e R T : s e p y g n i x o B d n a
, g n i x o b n U
1 1 . 3 i n i f e D ng m a r g o r P C s n o t n a ts
2 1 . 3 # C n o i t a r e t I s t c u r t s n o C
3 1 . 3 # C C o r t n o ls Fl w o c u r t s n o C t s
4 1 . 3 e h T e t e l p m o C Se t f o C
5 1 . 3 s d o h t e M r e t e m a r a P M d o i e i f s
6 1 . 3 r r A ay M n o i t a l u p i n a n i C #
7 1 . 3 g n i r t S u p i n a M la i t n o n i # C
8 1 . 3 # C u n E m a r e i t n o s
9 1 . 3 i n i f e D ng s e r u t c u r t S in # C
0 2 . 3 i n i f e D ng m o t s u C e c a p s e m a N s .























e D pt. f o T I B J S , E S I e g a P 2 4
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7


e h T 1 . 3 An y m o t a f o a l p m i S e C# r P g o m a r
# C demands t a h t a l l o r p gram l g o ic is n o c tai d e n w t i hin a t p y e defin i t i on (r a c e l l from
h C p a ter 1 t a h t type is a term refe r r n i g to a e m b m er of the e s t {c a l s s , interf e c a ,
structu , e r u n e mera o i t n, l e d e a g te}). Un e k i l in , ) + + ( C n i C# t i is n t o p s o s e l b i to r c eate
l a b o l g n o i t c n u f s or l g o l a b po t n i s of data. n I s t i simp t s e l f r o m, a C# pr g o a r m c n a e b
n e t t i r w as fol w o l : s
/ / y B , n o i t n e v n o c # C s e l i f d n e h t i w a s c . * e l i f . n o i s n e t x e
g n i s u ; m e t s y S

s s a l c s s a l C o l l e H
{
c i l b u p c i t a t s t n i ] [ g n i r t s ( n i a M ) s g r a
{
o C n e l o s r W . e t i n i L e( e H " l o l W r o d l ! ) " ;
Con l o s e e R . d a L ( e n i ; )
n r u t e r ; 0
}
}
, e r e H a i f e d nit o i n is cre t a ed for a cl s a s p y t e (He C o l l l s a s) a h t t su o p p t r s a n i s gle meth d o
named i a M ( n ). Every x e ecutable C# applica i t on must contain a cl s a s e d fini g n a
( n i a M ) m t e h d o , h w ich s i u d e s to s g i nify the n e try o p t n i of h t e p p a i l a c o i t n. As you can
e s e, t i h s sig a n ture of a M i ( n ) s i d e n r o d a t i w h the u p b i l c d n a sta i t c d r o w y e k s. e t a L r in
this c , r e t p a h y u o wi l l be suppli d e with a formal defi
U t n l i h t n e , d n u erstand h t at b u p i l c mem s r e b are c c a e s s ible from other t p y e , s wh l i e
sta i t c me b m ers are o c s ped at t e h class level (rather t a h n h t e object level) d n a can t u h s
be inv d e k o w t i u o h t the e e n d to f r i st r c eate a new cl s a s i s n ta e c n .
s n o i t a i r a V n o the a M in ) ( Me h t d o
The r p v e io s u i e t rati n o of a M i ( n ) was e d fined to take a single parameter (an arr y a of
strings) n a d ret r u n an integer data y t pe. i h T s is n t o h t e l n o y o p s b i s le form of a M in(),
t c u r t s n o c r u o y s n o i t a c i l p p a y r t n e t n i o p g n i s u y n a f o e h t
g n i w o l l o f s e r u t a n g i s g n i m u s s a ( t i s i d e n i a t n o c n i h t i w a C# cla s s r o s u r t c r u t e d i f e n n o i t i ):
/ / o N n r u t e r , e p y t y a r r a f o s g n i r t s s a t n e m u g r a .
c i l b u p t a t s c i d i o v ] [ g n i r t s ( n i a M
{
}

/ / o N n r u t e r , e p y t o n . s t n e m u g r a
c i l b u p c i t a t s d i o v ) ( n i a M
{
}

/ / r e g e t n I n r u t e r , e p y t o n . s t n e m u g r a
c i l b u p c i t a t s t n i ) ( n i a M
{
}

e D pt. f o T I B J S , E S I e g a P 3 4
n i t i on f o b u p c i l d n a sta i t c.
ho e w e v r. t I is e p rm s i sible to
g r a ) s
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7




g n i s s e c o r P d n a m m o C - e n i L u g r A m t n e s

s A su e m a h t t o y u now wish to up a d te He l C o l l ass to pro e c ss p s o s b i le com d n a m -l e n i
paramet r e s:
/ / s i h T , e m i t k c e h c f i u o y e v a h n e e b t n e s y n a d n a m m o c - e n i l . s t n e m u g r a
g n i s u ; m e t s y S

s s a l c { s s a l C o l l e H
c i l b u p c i t a t s t n i ] [ g n i r t s ( n i a M ) s g r a
{
L e t i r W . e l o s n o C d n a m m o C * * * * * " ( e n i e n i l s g r a ; ) " * * * * *
t n i ( r o f i = ; 0 i < ; h t g n e L . s g r a ) + + i
: g r A " ( e n i L e t i r W . e l o s n o C } 0 { , " ; ) ] i [ s g r a
. . .
}
}
e H re, y u o are h c ecking to see if t e h r a ray of t s rings conta n i s s m o e num e b r of item s
Length pr p o r e ty f o System.Array (as u o y ll e e s lat r e in th s i p a h c , r e t a l l C#
r a rays c a t a u y l l a i l as the Syst m e .Array e p y t , a d n h t erefore h v a e a c m o m n o set o f
m b m e ers). As u o y o l op r e v o h c a e t i em n i t e h r a ray, t i s va u l e is pr t n i d e o t the o c ns l o e
o d n i w w. p p u S l g n i y e h t s t n e m u g r a at h t e c m o a m nd line is e u q a l l y s a m i s ple .
a e t l rna i t ve to t e h st n a da d r o f r o l o , p o y u may i e t rate over in o c ming string r r a ays u i s g n
t e h C# r o f c a e h e k y o w rd. This bit of sy t n ax is u f l l y e p x lai d e n later n i s i h t h c apter, u b t
here is some a s mple us e g a :
/ / e c i t o N u o y e v a h o n d e e n o t e h c k c e h t
'fo a e r c ' h .
c i l b u p c i t a t s t n i ] [ g n i r t s ( n i a M ) s g r a
{
. . .
g n i r t s ( h c a e r o f s n i ) s g r a
: g r A " ( e n i L e t i r W . e l o s n o C } 0 { , " ; ) s
. . .
}
Fina l l y, you are l a so able to a c c e s s command-line a u g r ments usi g n the static
o C t e G d n a m m i L e n ( s g r A ) me d o h t f o e h t s y S t . m e E r i v n on n e m t p y t e. The return value of
str g n i s. e h T r i f st d n i ex identifies the r u c rent dir c e tory
i a t n o c n g n i e h t a i l p p cation itse f l , i h w le the a m e r ini g n l e eme t n s n i e h t a y a r r con a t in
e h t l a u d i v i d n i o c mma d n - e n i l a t n e m u g r s n e h w ( g n i s u s i h t c e t h i n q , e u you no lo g n er
d e e n o t de e n i f the M i a n() met d o h s a t k a ing a string arr y a pa a r meter)

c i l b u p c i t a t s t n i ] [ g n i r t s ( n i a M ) s g r a
{
. . .

e D pt. f o T I B J S , E S I e g a P 4 4
n i s u g th e
As an
e z i s f o e h t y a r r a n e h w g n i s u
this method is an r a ray of
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

g n i s u t s y S e E . m v n . t n e m n o r i
= En L d n a m m o C t e G . t n e m n o r i v A e n i r ) ( s g ;
r W . e l o s n o C h t a P " ( e n i L e t i o t s i h t p p a : s i , " } 0 { ; ) ] 0 [ s g r A e h t
. . .
}

g n i t a e r C 2 . 3 t c e j b O s C : ons s c i s a B r o t c u r t
w o N t a h t u o y e v a h e h t r e l o of M n i a () d n u er your e b , t l le t s move n o to t e h t p o ic of
tion . l l A o e j b ct-ori n e e t d (OO) langu g a es a m ke a clear dist n i c i t n o
be e w t en cla e s s s and objects. A s s a l c is a defini i t n o o ( r, f i you w , l l i a blu n i r p e t) for a
r e s u - d e n i f e d e p y t ( D U T). n A j b o c e t is simply a e t rm e d scr n i b i g a gi n e v inst n a ce f o a
par i t u c lar class in memory. In , # C t e h e n w d r o w y e k s i e h t de
ob e j ct. e k i l n U r e h t o O O s e g a u g n a l h c u s ( as + + C ), it is o n t o p s s ible to al e t a c o l a class
t p y e on the stack; the e r fore, if u o y attempt o t use a class vari b a le that h s a o n t been
w e n -e , d u o y e r a d e u s s i a c e l i p m o - e m i t e r o r r . h T s u t e h f l l o o i w ng C# o c e d s i e l l i gal:
g n i s u ; m e t s y S
l c s s a s s a l C o l l e H

c i t a t s t n i ] [ g n i r t s ( n i a M ) s g r a

e s U f o d e n g i s s a n u l a c o l . ' w e n ' e s u t s u M ! e l b a i r a v
s s a l C o l l e H ; 1 c
c1. m o S e t e M o h d ) ( ;
. . .
}
}
To u l l i st a r te the pro e p r procedu e r s o f r b o ject c a e r i t n o , b o s r e ve h t e l l o f o i w ng u a d p te :
g n i s u ; m e t s y S

s s a l c o l l e H s s a l C

c i t a t s t n i ] [ g n i r t s ( n i a M ) s g r a

u o Y n a c e r a l c e d d n a e t a e r c a w e n t c e j b o n i a e l g n i s e n i l . . .
s s a l C o l l e H 1 c = w e n ; ) ( s s a l C o l l e H

/ / . . . r o k a e r b n o i t a r a l c e d d n a n o i t a e r c o t n i o w t . s e n i l
s s a l C o l l e H ; 2 c
2 c




d n a c a i u q n i r g su f f ici n e t o m e m ry from the n a m aged heap. Here, you h e v a
a l l ocat d e o w t b o j c e ts f o the l l e H o s a l C s s a l c s e p y t . n a t s r e d n U d h t t a # C ob c e j t a v r s e l b a i
e r a e r y l l a a e c n e r e f e r to the b o j c e t in memory, not the ac u t al ob e j ct it l e s f. h T s u , in this

e D pt. f o T I B J S , E S I e g a P 5 4
/ / t e G s t n e m u g r a
s g r A e h t ] [ g n i r t s
b o ject construc
a f cto way to c e r ate an
r E r r o !
{
c i l b u p
{
/ /
{
c i l b u p
{
/ /
= w e n ; ) ( s s a l C o l l e H
. . .
}
}
h T e new d r o w y e k is n i e g r a h c f o c l u c l a a n i t g t e h c r o rect e b m u n r of b s e t y for e h t e p s cified
t c e j b o
C# d n a P T E N . m m a r g o r i g n

i l t h g , c1 a d n c2 h c a e re e f re e c n a u q i n ue H l l e o s a l C s
a e h p.

. 3 3 The Compo i s tion f o a C#
e h T s u o i v e r p l C o l l e H ass b o jects v a h e e e b n c n o structed u i s ng the defa l u t o c s n tructor ,
n o i t never ta e k s ar m u g en s t . r e v E y C# c s a l s s i u a tomat c i a l l y provided
t i w h a e r f e d f e au t l con t s r c u , r o t whi h c you may e r d i f e ne if e n d e e b . e h T f e d a l u t
r o t c u r t s n o c e s n u s e r t a h t l l a me r e b m a t a d is s t e to an a p p o r priate default value ( i h t s
r o i v a h e b is true o f r l a l c t s n o ruc o t ) s r . t s a r t n o C s i h t o t , + + C e r e h w i n i n u -
data p i o nts to garbage s ( m o eti s e m h t e li l t t e th s g n i e m an a lo ) t . , y l l a c i p y T s e s s a l c
o r p v e d i l a n o i t i d d a o c n s r o t c u r t s d n o y e b e h t def t l u a . n I g n i o d , o s u o y e h t e d i v o r p t c e j b o
r e s u h t i w a s m i p e l y a w o t i t i n i a e z i l e h t e t a t s f o n a t c e j b o t a e h t e m i t f o r c e n o i t a . k i L e in
Java a d n C+ , + in # C co t s n ruc o t rs e r a na d e m ident c i ally to the c a l ss they are
d i v e a re n r u t t o n ( e u l a v e n e v i o v d). He e r s i e h t
l C o l l e H ass t e p y c n o e again, i w th a cu t s om o c n r t s uct , r o
d n a a point of pu l b ic s r t ing data:
/ / , s s a l C o l l e H h t i w s r o t c u r t s n o c .
g n i s u ; m e t s y S

s s a l c s s a l C o l l e H
{
/ / A t n i o p f o e t a t s . a t a d
/ / t l u a f e D . r o t c u r t s n o c
c i l b u p l C o l l e H a ) ( s s
{ t l u a f e D " ( e n i L e t i r W . e l o s n o C r o t c ; ) " ! d e l l a c }

/ / s i h T m o t s u c r o t c u r t s n o c s n g i s s a e t a t s a t a d
/ / o t a r e s u - d e i l p p u s e u l a v .
c i l b u p s s a l C o l l e H g n i r t s ( ) g s m
{
W . e l o s n o C r e t i e n i L ( u C " m o t s c o t r c ; ) " ! d e l l a
e g a s s e M r e s u
}

/ / m a r g o r P y r t n e t n i o p .
c i l b u p c i t a t s t n i ] [ g n i r t s ( n i a M ) s g r a

l l a C t l u a f e d . r o t c u r t s n o c
s s a l C o l l e H 1 c = w e n ; ) ( s s a l C o l l e H
e u l a V " ( e n i L e t i r W . e l o s n o C f o : e g a s s e M r e s u } 0 { \ , " n ) e g a s s e M r e s u . 1 c ;

/ / l l a C d e z i r e t e m a r a p . r o t c u r t s n o c
s s a l C o l l e H ; 2 c
2 c = w e n , g n i t s e T " ( s s a l C o l l e H , 1 , 2 ; ) " 3
e u l a V " ( e n i L e t i r W . e l o s n o C f o : e g a s s e M r e s u , " } 0 { ; ) e g a s s e M r e s u . 2 c
Con l o s e e R . d a L ( e n i ; )

e D pt. f o T I B J S , E S I e g a P 6 4
I 6 0 S 1 6 7

t c e j b o a t a c o l l ed n o e h t ma g a n e d
c i h w h by de i f ni
z i l a i t ed state
n o c struct n i g, and they v e n r e pro
a e r d n i f e ed f e d au t l co r t s n u t c , r o
= ; g s m
{
//
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

n r u t e r ; 0
}
}


. 3 4 a f e D ult A s s ign e m nt n a d vari l b a e o c s pe
, y l t n e r r u C h t e He l C o l l s a s e p y t s a h n e e b c n o s d e t c u r t o t pe o f r rm o w t du s e i t . r i F st, the
l c ass d i f e nes the entry o p int of t e h p a l p i a c i t n o (t e h a M i ( n ) e m h t d o ). Seco d n ,
l C o l l e H ass maint i a ns a poi t n of fiel d
d n a g , d o o t i y a m e s em a bit str e g n a l a ( h g u o h t n y s
Mai ( n ) method e r c ates an i s n n a t e c of t e h very class n i whi h c t i
s s a l c s s a l C o l l e H
{
. . .
c i l b u p c i t a t s t n i ] [ g n i r t s ( n i a M ) s g r a
{
s s a l C o l l e H 1 c = w e n ; ) ( s s a l C o l l e H
. . .
}
}
a M ny f o y m n i t i ial e a x mp e l s take this app o r ach, u j st to e k p e focused on l l i ustrating the
hand. w o H e , r e v a e r o m na r u t al design would be to refac o t r the cu r r ent
l C o l l e H ass p y t e into w t o dis i t n t c l c asse : s Hel s a l C o l s a d n l e H l p A o p. Wh n e you bu l i d # C
c i l p p a a n o i t s, t i becomes quite co o m m n o t have one t p y e fun i t c oning as the applica i t on
ob e j ct (the t p y e that d i f e nes h t e Ma ( n i ) e m t ) d o h d n a u o r e m u n s t o h r e ty e p s h t a t
t u t i t s n o c e h t e a a c i l p p tion at la e g r In OO r a p lan , e c this is termed the se a p a r t o i n of
conce n r s. n I a n t u shell, h t is des g i n n i r p ci l p e stat s e t a h t a class u o h s d l be r p s e onsible
for t e h least a o m u t n of o w rk. , s u h T we could r n e e i g neer h t e
o f l g n i w o l e c i t o n ( t a h t a w e n me b m r e na d e m e M t n i r P s e g a s () s a h n e e b d e d d a o t h t e
Hel C o l l s a s : ) e p y t
s s a l c s s a l C o l l e H
{
c i l b u p g n i r t s ; e g a s s e M r e s u

c i l b u p l l e H l C o a ) ( s s
t l u a f e D " ( e n i L e t i r W . e l o s n o C { r o t c ; ) " ! d e l l a c }

c i l b u p g n i r t s ( s s a l C o l l e H ) g s m
{
W . e l o s n o C r e t i e n i L ( u C " m o t s c o t r c ; ) " ! d e l l a
e g a s s e M r e s u
}

c i l b u p d i o v ) ( e g a s s e M t n i r P
{
e g a s s e M " ( e n i L e t i r W . e l o s n o C : s i , " } 0 { ; ) e g a s s e M r e s u

e D pt. f o T I B J S , E S I e g a P 47
a t a d d n a a e f w co s n t u r ctors. W l i h e th s i s i a l l w l e l
a t c i t c y l l a well-for e m d) that the static
was def n i ed:
a t sk at
c r r u e t n pr r g o am into th e
= ; g s m
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

}
}

s s a l c p p A o l l e H
{
c i l b u p c i t a t s t n i ] [ g n i r t s ( n i a M ) s g r a
{
s s a l C o l l e H 1 c = w e n y e H " ( s s a l C o l l e H e r e h t . . " . ; )
c1. i r P n e M t s s a ) ( e g ;
. . .
}
}
3.5 C# Member I t n a z i l a i t o i n Sy t n a x
a M ny f o the examp e l p a l p ications r c eated o e v r t e h course of h t e e s first few chap e t rs
e s u of e h t s y S t C . m e o s n l o e l c ass. l i h W e a e l o s n o c s u er t n i erface ( U C ) I is
r o b e w , I U g n i t c i r t s e r e h t y l r a e s e l p m a x e o t a I U C l l i w
w o l l a s u o t p e e k d e s u c o f n o e h t s t p e c n o c r e d n u exa i m a n i t n o , a r ther t n a h d a e n i l g t i w h
As s t i n m a e i p m l e i , s e h t l o s n o C e s a l c s
s e t a l u s p a c n e , t u p n i , t u p t u o d n a r o r r e m a e r t s l u p i n a m a n o i t s o f r con e l o s - a b e s d
n o i t a c i l p p a s. h t i W t e h e s a e l e r f o T E N . , 0 . 2 e h t C l o s n o e e p y t s a h n e e b d e c n a h n e h t i w
l a n o i t i d d a y t i l a n o i t c n u f . a T ble 3-2 lists o s me (but not a l l ) new m m e e b rs of in e t rest.
e l b a T 3-2. t c e l e S .N T E 2.0S c e p i i f c e M mbers of S t s y e C . m n o s l o e
r e b m e M g n i n a e M n i Lif e
Bac r g k o d n u o C lor Th s e e r p p o erties set the ba g k c r f / d n u o oregr n u o d colors for
h t e
r r u c ent
For r g e o d n u o C lor o t u put. They m y a e b a s s igned any me b m r e of the
r o l o C e l o s n o C
n e u o i t a r e m n.
Buf r e f H g i e ht e s e h T s e i t r e p o r p l o r t n o c e h t h t d i w / t h g i e h f o e h t s e l o s n o c
r e f f u b
. a e r a
h t d i W r e f f u B
Cle ( r a ) This method cle r a s t e h buffer n a d co o s n le di p s l y a ar a e .
Title This pr p o er y t sets t e h l t i t e of t e h u c rr n e t console.
Win w o d H g i e ht e s e h T t r e p o r p i s e c l o r t n o h t e i d me i s n ons of the c n o o s e l i n
n o i t a l e r
t o
Win w o d W t d i h e h t es d e h s i l b a t r e f f u b .
p o T w o d n i W
t f e L w o d n i W

6 . 3 c i s a B t u p n I d n a t u p t u O h t i w e h t e l o s n o C s s a l C
n I d d a i t i on to t e h me b m ers in Table 3-2, the Co o s n le e p y t de s e n i f a t e s of m t e h d o s o t
e r u t p a c i p n ut
k a m e extens v i e
t o n s a en i t c g n i as a Wi s w o d n
t e h c m o pl x e i t i s e of u b l i ding GU s I .
n a d o p t u u , t a l l of whi h c r a e defi d e n as sta i t c n a d are theref r o e a c l l d e at

e D pt. f o T I B J S , E S I e g a P 8 4
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

t e h class l v e el. As u o y ha e v seen, r W i n i L e t ( e ) pum s p a text string (i c n l d u i g n a carri e g a
return) to t e h u o tp t u str a e m. h T e t i r W e() h t e m od pu p m s text to the u o tp t u str a e m
h t i w t u o a c r r a iage re r u t n. R d a e i L n ( e ) al o l ws you to receive info m r a o i t n from the i u p n t
s r t a e m p u un l i t t e h a c rriage return, while e R a ( d ) is e s u d to ca r u t p e a e l g n i s
o T l i lustrate s a b ic I/O usi g n the Co s n ole class, n o c sid r e the
m o r p pts the s u er for s m o e bi s t of n i f i t a m r o on d n a
e h c e o s ea h c item to the standa d r p t u o ut stream.
e r u g i F 3-5 s s w o h a e t st r n u .

/ / e k a M e s u f o e h t e l o s n o C s s a l c o t m r o f r e p c i s a b . O / I
c i t a t s d i o v ] [ g n i r t s ( n i a M ) s g r a
{
/ /

= ; ) ( e n i L d a e R . e l o s n o C
l e H " ( e n i L e t i r W . e l o s n o C , o l } 0 { , " ; ) s

r e t n E " ( e t i r W . e l o s n o C r u o y : e g a ; ) "
s = ; ) ( e n i L d a e R . e l o s n o C
u o Y " ( e n i L e t i r W . e l o s n o C e r a } 0 { s r a e y , " d l o ; ) s
}
t s r e d n U 7 . 3 a d n ing V l a e u Ty e p s a d n r e f e R ence Ty s e p
L k i e y n a program i m ng language, C# defin s e a num e b r of k y e r o w s d that represent
basic a d a t y t e p s u s ch as wh l o e u n b m e , s r ch r e t c a r a t a d a, a o l f ting-po t n i numb , s r e a d n
o B o n a e l s e u l a v . f I you come r f om a C++ bac g k rou d n , you l l i w e b h p a y p to k o n w th t a
th s e e t n i ri s n ic ty e p s r a e fixed consta t n s in t e h univer e s , m n a e i g n that w e h n you r c eate
an int g e er a d ta p , t n i o a l l T E N . -aw r a e lang a u ges underst n a d h t e f x i ed nature of this
typ , e d n a a l l agree on the r n a ge t i is c b a p a le of h n a i l d g n .
y l l a c i f i c e p S , g n i k a e p s a T E N . a t a d e p y t y a m be l a v ue- a b sed or e c n e r e f e r - e s a b d. V e u l a -
wh c i h n i clude all nu e m rical data y t pes ( n i t, o l f t a , et . c ), as we l l as
d e t a c o l l a n o e h t t s ack. n e v i G th s i , d i o t c a f e u l a v s e p y t
n a c e b y l k c i u q d e v o m e r m o r f y r o m e m e c n o y e h t l l a f t u o f o the d f e i i n ng s o c p : e
/ / s r e g e t n I e r a e u l a v ! s e p y t
c i l b u p d i o v ) ( d o h t e M e m o S
{
t n i i = ; 0
Con l o s e r W . t i e e n i L ( ) i ;
d e p p o p f f o e h t k c a t s ! e r e h
h W en you a s s ign n o e value t p y e to anoth r e , a member-by-me b m er copy is c a hi d e v e by
de a f ult. n I e t rms of nume i r cal or o B olean da a t t p y e , s the only member to co y p is h t e
va u l e of t e h v a i r a ble i e s t f l :
/ / i n g i s s A g n o w t c i s n i r t n i e u l a v s e p y t s t l u s e r n i
/ / o w t t n e d n e p e d n i s e l b a i r a v n o e h t . k c a t s
c i l b u p d i o v ) ( d o h t e M e m o S
{
t n i i = ; 9 9

e D pt. f o T I B J S , E S I e g a P 9 4
character
from the p n i ut st e r am.
o f llowing a M i ( n ) m h t e od, wh c i h
o h c E e m o s . s t a t s
r e t n E " ( e t i r W . e l o s n o C r u o y : e m a n ; ) "
g n i r t s s
, s e p y t d e s a b
u n e merations and st u r ctures, r a e
} / / ' i ' s i
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

t n i j = ; i

/ / r e t f A e h t g n i w o l l o f , t n e m n g i s s a ' i ' s i l l i t s . 9 9
j 8 2 3 7 ;
}
e l i h W e h t s u o i v e r p x e a l p m e s i no r o j a m w e n s , h s a l f d n a t s r e d n u t a h t T E N . s e r u t c u r t s d n a (
r e m u n e a n o i t s, wh c i h are e a x m n i ed la e t r in this chap e t r) are also val e u types. r t S c u u t res,
m i s p y l u p , t o r p vi e d a way to ach e i ve the bare-bones e b nef t i s of ob e j ct orien i t a t on (i. . e ,
c n e a l u s p a i t n o ) h w ile having the f e ficiency of stack- l l a c o ated data. i L e k a c a l s s ,
struc u t res a c n t e k a c s n o t c u r tors (pr v o ided they a h ve ar u g m n e ts) and n i f e d e any
l l A s r t uct s e r u a e r implic t i ly de i r v d e from a l c a s s n m a d e
. m e t s y S V e u l a p y T e. , y l l a n o i t c n u F e h t y l n o u p r e s o p f o S e t s y m u l a V . eType o t s i e d i r r e v o
e h t l a u t r i v s d o h t e m d e n i f e d y b O . m e t s y S b c e j t d e b i r c s e d ( in u j st a mo n e m t) to h n o or
va e u l - s a b ed, r e v s s u e c n e r e f e r - , d e s a b i t n a m e s cs. n I , t c a f e h t e c n a t s n i h t e m
byS m e t s y . e u l a V T p y e e r a i c i t n e d al to o h t se of t c e j b O . m e t s y S :
s e r u t c u r t S / / d n a s n o i t a r e m u n e d n e t x e . e p y T e u l a V . m e t s y S
c i l b u p t c a r t s b a s s a l c e p y T e u l a V : t c e j b o
{
c i l b u p l a u t r i v l o o b t c e j b o ( s l a u q E ; ) j b o
c i l b u p l a u t r i v t n i ; ) ( e d o C h s a H t e G
c i l b u p e p y T ; ) ( e p y T t e G
l a u t r i v c i l b u p g n i r t s T ; ) ( g n i r t S o
}
s A su e m you have r c eat d e a C# struc u t re named M i o P y nt, i s u g n t e h # C s r t uct k y e o w rd:
/ / s e r u t c u r t S e r a e u l a v ! s e p y t
t c u r t s t n i o P y M
{
c i l b u p t n i , x ; y
}
o T c o l l a ate a struc r u t e type, you may make s u e f o the e n w key o w rd, whi h c m y a e e s m
n u o c terintu t i i e v given t a h t e w typically think e n w a w l ays impl e i s heap al o l a c ti n o . s i h T s i
t r a p of h t e s e k o m and i m r r ors maintained by h t e CLR. As o r p gra m m e s r , we c n a
s a sume ever h t y i g n is n a ob e j ct and w e n a v lue ty e p s. , r e v e w o H wh n e e h t run m i t e
n e c t n u o ers a p y t e derived from e t s y S m a V . lu T e p y e , stack
/ / l l i t S n o e h t ! k c a t s
t n i o P y M p = w e n ; ) ( t n i o P y M
As an a t l erna i t e v , str c u tures a c n e b a l l o a c ted t i w u o h t s u i g n t e h e n w e k y r o w d:
t n i o P y M ; 1 p
x . 1 p = ; 0 0 1
y . 1 p = ; 0 0 1
If you take this appr c a o h, o h e w ve , r u o y s u m t i t i n i ali e z c a e h pi e c e of field a d ta e b fore
s u e.
Fa n i l i g to do so results in a o c mpi e l r err r o .
V e u l a T s e c n e r e f e R , s e p y T r o t a r e p O t n e m n g i s s A e h t d n a , s e p y
] [ g n i r t s ( n i a M d i o v c i t a t s ) s g r a
{

e D pt. f o T I B J S , E S I e g a P 0 5
=
b m u n er f o m m e s r e b .
ods e d fined
a c o l l ati n o s i achi v e d e :
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

o s n o C * * * * * " ( e n i L e t i r W . e l e u l a V s e p y T / e c n e r e f e R s e p y T ; ) " * * * * *
" ( e n i L e t i r W . e l o s n o C -> g n i t a e r C 1 p ) " ;
t n i o P y M 1 p = w e n ; ) ( t n i o P y M
x . 1 p = ; 0 0 1
y . 1 p = ; 0 0 1
" ( e n i L e t i r W . e l o s n o C - g n i n g i s s A > 2 p o t 1 p \ ; ) " n
t n i o P y M 2 p

/ / e r e H s i . 1 p
x . 1 p " ( e n i L e t i r W . e l o s n o C = , " } 0 { p ; ) x . 1
y . 1 p " ( e n i L e t i r W . e l o s n o C = , " } 0 { ; ) y . 1 p

/ / e r e H s i . 2 p
x . 2 p " ( e n i L e t i r W . e l o s n o C = , " } 0 { ; ) x . 2 p
y . 2 p " ( e n i L e t i r W . e l o s n o C = , " } 0 { ; ) y . 2 p

/ / e g n a h C x . 2 p . s i h T l l i w T O N e g n a h c . x . 1 p
" ( e n i L e t i r W . e l o s n o C - g n i g n a h C > x . 2 p o t ; ) " 0 0 9
x . 2 p = ; 0 0 9

/ / t n i r P n i a g a .
" ( e n i L e t i r W . e l o s n o C -> e r e H e r a e h t X s e u l a v n i a g a . . . ; ) "
x . 1 p " ( e n i L e t i r W . e l o s n o C = , " } 0 { ; ) x . 1 p
x . 2 p " ( e n i L e t i r W . e l o s n o C = , " } 0 { ; ) x . 2 p
Con l o s e e R . d a L ( e n i ; )
}
/ / s e s s a l C e r a s y a w l a e c n e r e f e r . s e p y t
s s a l c t n i o P y M / / = < w o N a ! s s a l c
{
c i l b u p t n i , x ; y
}

a V e u l s e p y T g n i n i a t n o C e c n e r e f e R s e p y T
Now h t at you have a bet e t r feeling for h t e i d ffer c n e es between value ty e p s and
n e r e f e r ce t p y e , s le t s a x e i m e n a m e r o o c mplex a x e mple. s A u s me you have h t e
o f l n i w o l g e r f c n e r e e (cla ) s s t p y e that ma n i t i a s n n a inf r o a m tion l a stri g n t t a h can e b e s t
i s u ng a u c stom c n o s r t t c u : r o
s s a l c o f n I e p a h S
{
c i l b u p g n i r t s ; g n i r t S o f n i
c i l b u p g n i r t s ( o f n I e p a h S ) o f n i
{ g n i r t S o f n i = ; o f n i }
}
Now as u s me that you wa t n to contain a a v ria l b e of this cl s a s type within a a v lue y t pe
R y M e t c angle . o T a l l ow t e h outside world to e s t h t e val e u of t e h inner
a custom c n o structor ( s a p x e a l n i ed in u j st a b , t i the
de a f l u t c s n o t c u r tor of a str c u ture is e r served a d n c n n a ot be redef n i ed):

e D pt. f o T I B J S , E S I e g a P 1 5
= ; 1 p
name d
S a h peI f n o, you al o s provide
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

s e l g n a t c e R y M t c u r t
{
/ / e r u t c u r t s s n i a t n o c a e c n e r e f e r e p y t . r e b m e m


c i l b u p t n i , p o t , t f e l , m o t t o b ; t h g i r
c i l b u p g n i r t s ( e l g n a t c e R y M ) o f n i
{
o f n I t c e r = w e n ; ) o f n i ( o f n I e p a h S
p o t = t f e l = ; 0 1
m o t t o b = t h g i r = ; 0 0 1
}
}
t A th s i p i o n , t you have conta n i ed a reference type within a va u l e type. e h T i l l i m n o -
i t on now be o c m , s e w a h t s n e p p a h if u o y assi n g one e R y M ct g n a le a v i r able to
o y u al d a e r y o n k w b a out va u l e y t e p s, o y u would e b o c rrect n i
a u s s i m ng that t e h t n i eger a d ta (which
y t i t n e o f r a e ch M t c e R y an l g e a v ri b a le .
c i t a t s d i o v ] [ g n i r t s ( n i a M
{
/ / e t a e r C e h t t s r i f . e l g n a t c e R y M
" ( e n i L e t i r W . e l o s n o C -> g n i t a e r C 1 r ) " ;
e l g n a t c e R y M 1 r = w e n s i h T " ( e l g n a t c e R y M s i y m t s r i f ; ) " t c e r

/ / w o N n g i s s a a w e n e l g n a t c e R y M o t . 1 r
" ( e n i L e t i r W . e l o s n o C -> g n i n g i s s A 2 r " 1 r ; )
e l g n a t c e R y M ; 2 r
r2

/ / e g n a h C s e u l a v f o 2 r .
" ( e n i L e t i r W . e l o s n o C -> g n i g n a h C l l a s e u l a v f o ; ) " 2 r
g n i r t S o f n i . o f n I t c e r . 2 r = s i h T " s i w e n ; " ! o f n i
m o t t o b . 2 r = ; 4 4 4 4

/ / t n i r P s e u l a v
" ( e n i L e t i r W . e l o s n o C -> s e u l a V r e t f a ; ) " : e g n a h c
( e n i L e t i r W . e l o s n o C "-> 1 r r . n I t c e f n i r t S o f n i . o : g { " } 0 , r e r . 1 I t c f n n i . o f i r t S o g n );
( e n i L e t i r W . e l o s n o C "-> 2 r r . n I t c e f : g n i r t S o f n i . o { " } 0 , r e r . 2 I t c f n n i . o f i r t S o g n );
" ( e n i L e t i r W . e l o s n o C -> : m o t t o b . 1 r , " } 0 { ; ) m o t t o b . 1 r
" ( e n i L e t i r W . e l o s n o C -> : m o t t o b . 2 r , " } 0 { ; ) m o t t o b . 2 r
}

e u l a V d n a e c n e r e f e R : s e p y T l a n i F s l i a t e D
To wrap p u this to i p c, d n o p er the informa i t on in b a T le 3-8, i h w ch summ r a i e z s t e h
co e r i d s i t c n i t ons betwe n e a v l e u t s e p y a d n re e f rence t p y es.

e l b a T 3-8. l a V ue y T pes n a d R e r e f e n e c Ty e p s i S de by Side

e D pt. f o T I B J S , E S I e g a P 2 5
e l g n a t c e R y M e h T
c i l b u p o f n I e p a h S ; o f n I t c e r
o d a l l r ques
n a t o e h r? i G v n e what
s i n i d d e e a u r t s c e r u t ) h s u o ld be an i d n e e p n e d nt
r a s g )
o t
= ; 1 r
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

g n i u g i r t n I n o i t s e u Q e u l a V T e p y e c n e r e f e R T e p y
e r e h W s i i h t s e p y t d e t a c o l l A n o h t e s k c a t . d e t a c o l l A n o e h t mana d e g
d e t a c o l l a ? a e h p.
w o H is a a v ri b a le Value type vari b a les are e c n e r e f e R e p y t a v r l b a i s e
s e r p e r ent d e ? local e r a s e i p o c p . o n i t n i g o t the
memory o c c upied y b th e
c o l l a a d e t n i s n a t ce.
t a h W s i h t e e s a b e p y t ? Mu t s e d r v i e r f om Can e d ri e v r f om any othe r
type

e j b O . m e t s y S : e d o N r e t s a M e h T 8 . 3 c t
n I N . E , T y r e v e e p y t s i m i t l u a l e t y d e v i r e d m o r f a co o m m n e s a b c a l s : s Sy m e t s . e j b O ct .
The Ob e j ct class
a c s s a l a h t t s e o d o n t p x e li i c l t y spe i c fy its a b se cla , s s
u o y l t i c i l p m i y e d r e v i fr m o S e t s y m. j b O ect :
/ / g n i v i r e d y l t i c i l p m I m o r f t c e j b O . m e t s y S .
s s a l c s s a l C o l l e H
{ . . .}
f I u o y h s i w o t e b e r o m r a e l c h t i w r u o y , n o i s n e t n i e h t # C n o l o c r o t a r e p o (:) a o l l ws o y u to
y l t i c i l p x e p s e i c fy a t p y es ba e s l c ass (such as Sy m e t s . e j b O ct ):
/ / n I h t o b s e s a c e w e r a y l t i c i l p x e g n i v i r e d m o r f . t c e j b O . m e t s y S
s s a l c o f n I e p a h S : t c e j b O . m e t s y S
{ . . .}

s s a l c o f n I e p a h S : t c e j b o
{ . . .}
. m e t s y S O c e j b t s e n i f e d a t e s of n i s c n a t e- e v e l l a d n c a l s s - e v e l l s ( t t a ic) me b m s r e . Note that
some of h t e n i stance-level mem e b rs are c e d a l r d e us n i g t e h i v r a u t l e k ywo d r n a d can
th f e r e ore be v o erridden
/ / e h T t s o m p o t s s a l c n i e h t T E N . : e s r e v i n u t c e j b O . m e t s y S
e c a p s e m a n
{
c i l b u p s s a l c t c e j b O
{
c i l b u p ; ) ( t c e j b O
c i l b u p l a u t r i v t c e j b O ( s l a u q E n a e l o o B ; ) j b o
c i l b u p l a u t r i v 2 3 t n I ; ) ( e d o C h s a H t e G
c i l b u p e p y T ; ) ( e p y T t e G
c i l b u p l a u t r i v g n i r t S ; ) ( g n i r t S o T
d e t c e t o r p l a u t r i v d i o v ; ) ( e z i l a n i F
d e t c e t o r p t c e j b O ; ) ( e n o l C e s i w r e b m e M
c i l b u p c i t a t s l o o b t c e j b o ( s l a u q E , A j b o t c e j b o ; ) B j b o
c i l b u p c i t a t s l o o b t c e j b o ( s l a u q E e c n e r e f e R , A j b o t c e j b o ; ) B j b o
}
}

e D pt. f o T I B J S , E S I e g a P 3 5
de s e n i f a n o m m o c t e s f o m e m b s r e suppo t r ed by e e v ry ty e p in t e h
. T E N v i n u erse. n e h W u o y cr a e te
by a de i r ved l c s a : s
m e t s y S
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

e h T e D fault Behavior of System.Obj c e t
To u l l i strate m o s e of t e h e d f u a lt be a h vior provided by the e t s y S m O . b e j ct base class,
s s a ume a cla s s
named Per o s n n i f e d d e n i a cus m o t names a p e c named b O je e M t c t o h ds :
/ / e h T ' e c a p s e m a n ' d r o w y e k s i y l l u f d e n i m a x e t a e h t d n e f o s i h t
s d o h t e M t c e j b O e c a p s e m a n
{
s s a l c n o s r e P
{
c i l b u p g n i r t s ( n o s r e P , e m a n f g n i r t s , e m a n l g n i r t s , s e t y b ) a
{
e m a N t s r i f
e m a N t s a l = ; e m a n l
N S S = ; s
e g a = ; a
}
c i l b u p } { ) ( n o s r e P

/ / e h T e t a t s f o a . n o s r e p
c i l b u p g n i r t s c i l b u p ; e m a N t s r i f g n i r t s ; e m a N t s a l
c i l b u p g n i r t s ; N S S
c i l b u p e t y b ; e g a
}
}
, w o N n i h t i w r u o ( n i a M ) me o h t d, we m k a e use of the e P r o s n p y t e s a s : o
c i t a t s d i o v ] [ g n i r t s ( n i a M ) s g r a
{
* * * * * " ( e n i L e t i r W . e l o s n o C g n i k r o W h t i w t c e j b O * * * * * \ ; ) " n

, " d e r F " ( n o s r e P , " k r a l C " 1 1 1 " - 1 1 - , " 1 1 1 1 ; ) 0 2
e l o s n o C r W . e t i e n i L ("->
W . e l o s n o C r e t i e n i L ("->
/ / e k a M e m o s r e h t o s e c n e r e f e r o t . ' d e r f '
n o s r e P 2 p = ; d e r f
c e j b o t o = ; 2 p

g n i t n i o p o t e h t e m a s t c e j b o n i ? y r o m e m
) d e r f ( s l a u q E . o ( f i & &
2 p , d e r f " ( e n i L e t i r W . e l o s n o C d n a o e r a g n i c n e r e f e r e h t e m a s ; ) " ! t c e j b o
Con l o s e e R . d a L ( e n i ; )
}
r i F st, o n i t e c o h w h t e defa t l u m i plementa i t on of o T St g n i r () simply returns the fully
u q a i l f e i d name of h t e t e p y e ( g . , . e m a n sp e c a . y t p a n e me ). Ge y T t ( e p ) e r i r t v e e s
a S m e t s y .Type j b o ec , t i h w ch f e d ines a o r p pe t r y n e m a d a B s p y T e e s a ( u o y c n a s e u g s, i h t s
l l i w y f i t n e d i e h t y l l u f d e i f i l a u q na e m f o e h t s e p y t
t a h t s e g a r e v e l e h t ( s l a u q E ) m h t e od. , e r e H a n w e s r e P on ob t c e j s i l p a d e c on the m a n a g d e

e D pt. f o T I B J S , E S I e g a P 4 5
t p a h c er.
= ; e m a n f
n o s r e P d e r f = w e n
. d e r f o T : g n i r t S , " } 0 { fre T . d o r t S in ) ) ( g ;
d e r f .G H t e h s a o C : e d {0 " } , e r f d t e G . H d o C h s a e ) ) ( ;
/ / e r A l l a 3 s e c n a t s n i
E . 2 p q a u s l ( ) o )
s a b e cla s s ). , w o N e n i m a x e e r e h t e d o c
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

hea , p and h t e e r f r e e c n e to this object is s o t red in the f e r d e r fe e r nce a v r a i ble.p2 is a s l o f o
y t p e Pe s r on h , o e v e w r, a u o y r e not cr a e i t ng a new e c n a t s n i of e h t r e P o s n cl s s a , but
assi n g ing 2 p o t fred. Therefore, r f ed d n a p2 ar b e ot h pointi g n t o th s e am o e bject in
m e m ory s a , i s t v e h ari l b a e o (of type o j b ect, which w s a thrown in for good m s a e u ) e r .
Given t a h t e r f d, p2, and o l l a po n i t to t e h a s me object i n
u s c e c ed . s

g n i d i r r e v O e m o S f e D a t l u Beh r o i v a s f o
l A though h t e cann d e be a h vior of S m e t s y . e j b O ct c n a f t i the bi l l in m t s o
e s a c s, t i is quite common f r o y u o r custom types o t over i r e d so e m f o h t e s e e h n i r t i e d
meth d o s . t p a h C er 4 v o r p ides a c l p m o e e t x e a i m nat o i n of O O P u d n er C#, but in a
nut h s e , l l overri i d g n is t e h pr c o e s s of red f e i n i g n t e h b v a h e ior
e m mber n i a derived cl s a s. As you have j s u t e s n e , y S s e t m O . bject e d fines a u n m e b r f o
i v rt a u l met d o h s (su h c as= To t S n i r g() and u q E a ( s l )) that o d define a canned
p m i lement
l a u t r i v me s r e b m o f r a e d r v i ed
g n i d i r r e v O ) ( g n i r t S o T . t c e j b O . m e t s y S
g n i d i r r e v O e h t o T S n i r t g() h t e m od pr v o e d i s a way to quick y l gain a snapshot of an
ob e j cts cu r r ent
r p ocess. T t e l , e t a r t s u l l i o e d i r r e v o s S e t s y m b O . j T . t c e o i r t S n ( g ) o t t e r u n r a te u t x al
n o i t a t n e s e r p e r f o a pe n o s r s sta e t ( o n te e w a e r n i s u g a n w e n s e m a pa e c n m a ed
Sys m e t . x e T t):
/ / d e e N o t e c n e r e f e r t x e T . m e t s y S o t s s e c c a
g n i s u ; m e t s y S
g n i s u ; t x e T . m e t s y S


{
/ / g n i d i r r e v O s y S . m e t j b O c e S o T . t t . ) ( g n i r
g n i r t s e d i r r e v o c i l b u p ) ( g n i r t S o T
{
n i r t S r e d l i u B g b s = w e n ; ) ( r e d l i u B g n i r t S

" ( t a m r o F d n e p p A . b s
a m r o F d n e p p A . b s ( t "
b s . e p p A F d n m r o ( t a " e g A = ] } 0 { , "
n r u t e r ; ) ( g n i r t S o T . b s
}
. . .
}
w o H u o y t a m r o f e h t g n i r t s d e n r u t e r m o r f Syst e j b O . m e c S o T . t t g n i r () s i y l e g r a l a r e t t a m f o
h c i o ce. n I this e a x mple, h t e name/va u l e pairs e v a h n e e b contai d e n i h t i w n
s a u q e r br c a kets, h t i w e c a h r i a p p e s arated by a e s m o l o c i n a ( c m m o on e u q i n h c e t n i h t i w
e h t T E N . e s a b s s a l c ) s e i r a r b i l . l A so n t o e c i that this exa p m e l makes use of a new y t p , e
. m e t s y S T S . t x e t B g n i r u e d l i r ( h c i h w is also

e D pt. f o I B J S , E S I T
memor , y h t e e u q a i l y t te t s
Sy t c e j b O . m e t s
of an i e h n r t i ed i v r u t al
t a t ion. , r e v e w o H f i u o y t n a w to bu d l i a m o t s u c l p m i e t n e m a o i t n of t e h se
ty e p , you make use of the C# e v o rr d i e . d r o w y e k


t a t s e. s A u o y t h g i m , s s e u g th s i n a c e b l u f p l e h g n i r u d e h t g n i g g u b e d



r e d l i u B g n i r t S . e p y t



s s a l c n o s r e P





b s . e p p A d n F r o m " ( t a [ r i F s N t m a e } 0 { = ; , " i h t s f . i t s r Na ) e m ;
s a L a N t e m ; } 0 { = ", h t . s i a l s m a N t e);
S S N= ; } 0 { , " h t is N S S . ; )
h t is e g a . ; )
p r e o s l a n
a a m t r e t f o s r e p o a n l c o h i e c ). h T is t e p y is

e g a P 5 5
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

descri d e b n i r g eat r e deta l i la e t r in t e h c a h t p r e . e v e w o h , r e w s n a t r o h s e h T r t a h t s i ,
B g n i r t S u e d l i r s i a mo e r t n e i c i f f e a e t l rn t a v i e to # C str n i g n o c c n e t a i t a n o .
O r r e v iding s y S tem.Obj c e t. a u q E ls()
Let s al o s overri e d h t e h e b av o i r of e t s y S m.O E . t c e j b q ( s l a u ) to work wi h t value-b s a ed
semantics. Rec l l a th t a by defa l u t, E a u q ls() re s n r u t e u r t y l n o f i e h t t o w r f e e n e r ces being
o t h t e same object on h t e e h ap. In n a m y a c se , s h w o ever, you
t n o d e c e n s r a s y c r a e if w t o s e c n e r e f e r r a e poin i t ng to the a s me b o j c e t in mem y r o , but
u o y are o m e r interest d e if t e h two o j b c e ts e v a h h t e same sta e t da a t ( a n me, S S N, and
g a e n i e h t s a c e of a Pe s r on):
c i l b u p e d i r r e v o l o o b t c e j b o ( s l a u q E ) o
{
/ / e k a M e r u s e h t r e l l a c t n e s a d i l a v
/ / n o s r e P t c e j b o e r o f e b . g n i d e e c o r p
f i o ( = ! l l u n & & o s i ) n o s r e P
{
/ / w o N e e s f i e h t g n i m o c n i n o s r e P
/ / s a h e h t t c a x e e m a s n o i t a m r o f n i s a
/ / e h t t n e r r u c t c e j b o . ) s i h t (
n o s r e P p m e t = ; o ) n o s r e P (
f i e m a N t s r i f . p m e t ( = = e m a N t s r i f . s i h t & &
e m a N t s a l . p m e t = = e m a N t s a l . s i h t & &
N S S . p m e t = = N S S . s i h t & &
e g a . p m e t = = ) e g a . s i h t
n r u t e r ; e u r t
}
n r u t e r ; e s l a f / / t o N e h t ! e m a s
}
Here o y u a e r i f r t s ve i r f i y ng t e h cal e l r did inde d e p s a s in a o s r e P n object to the
( s l a u q E ) m t e o h s u ing t e h # C is k y e word. t f A er i h t s po , t n i o y u go ab t u o x e m a in g n i e h t
s e u l a v f o e h t n i m o c n i g r e t e m a r a p n i a g a st e h t s e u l a v f o e h t t n e r r u c s t c e j b o d l e i f a t a d
e t o n ( e h t e s u f o h t e i h t s e k ywo , d r h c i h w f e r ers
e h T t o r p o p y t e of t s y S e c e j b O . m t a u q E . l ( s ) a t k s e a l g n i s e r a n e m u g t of e p y t b o j c e t. s u h T ,
u o y e r a e r r i u q ed to e p rform an explic t i ca t s i w thin the l a u q E s() m h t e od o t c a c s e s t e h
mem e b rs of t e h s r e P on
w t o b o j c e ts with h t e same state a d ta and therefo e r r u t e n r u r t e. f I a y n i o p nt of t a d a i s
t o n , l a c i t n e d i u o y e r u t rn f l a se. If y u o over i r e d Sy m e t s .O c e j b t o T . St n i r g() for a g n e v i
, s s a l c u o y n a c e k a t a very s l p m i e o h s r c t ut wh n e e v o r r g n i d i e t s y S m.O E . t c e j b q ( s l a u ) .
i G ven that the value re u t rned from S o T t g n i r () s u o h ld t k a e
s s o p d n a ( s s a l c t n e r r u c e h t f o s e l b a i r a v r e b m e m i a d e l b t c e d a lar d e i c e s a b n l , ) s e s s a
Equ ( s l a ) a c n s m i y l p com a p re t e h va u l es of h t e stri g n typ s e :
c i l b u p e d i r r e v o l o o b t c e j b o ( s l a u q E ) o
{
f i o ( = ! l l u n & & o s i ) n o s r e P
{
f i ) ( g n i r t S o T . s i h t ( = = ) ) ( g n i r t S o T . o
n r u t e r ; e u r t
e D pt. f o T I B J S , E S I e g a P 6 5
d e r a p m o c a e r g n i t n i o p
o t e h t t n e r r u c e j b o ct).
y t pe . If the a n m , e SSN, n a d age of ea h c a e r i n e d ti a c , l you a h v e
o t n i t n u o c c a l l a f o e h t
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

e s l e
n r u t e r ; e s l a f
}
n r u t e r ; e s l a f
}
Now, for t e h sake of argume , t n s a u s me you v a h e a t p y e named a C r, d n a a t p m e t t o t a p ss
n i a r a C n i stan e c to e h t Per n o s . u q E l a s() me d o h t as o s :
/ / s r a C e r a t o n ! e l p o e p
r a C c = w e n ; ) ( r a C
n o s r e P p = w e n ; ) ( n o s r e P
p.E a u q l c ( s );
n e v i G r u o y i t n u r e m ch k c e r o f a e u r t - u l b e o s r e P n o j b e t c ( a i v t e h is e h t ) r o t a r e p o
s l a u q E () method
/ / ! s p o O
n o s r e P p = w e n ; ) ( n o s r e P
p.E a u q l n ( s l u l);
s i h T d l u o w s l a o be , e f a s v i g en u o y r h c e k c f r o n a o c n i m g n i null ref r e e e c n .

O r r e v iding s y S t m e .Obj t c e .Get s a H h e d o C ()
h W en a cl s a s ov r e ri e d s e h t Equal ( s ) e m o h t d, best practi e c s dicta e t that you s o h ul d
also e v o r d i r e Syst . m e O j b ect G . e H t a h s C d o e() . If you fail to o d so, you are iss d e u a
comp l i erwarning. h T e role f o e G tHashCode() is o t ret n r u a u n merical v l a ue that
ident f i ies an object based on t i s internal state data. T u h , s f i o y u e v a h two
Per o s n o e j b cts that h v a e n a identical first name, a l st name, S S N, and age,
t b o a n i e h t same hash . e d o c

There are m y n a al r o g t i hms h t at can e b u d e s o t create a h s a h co e d some fan y c , others
t o n o s y c n a f . As me n o i t n e , d an objects a h sh va u l e w l l i be s a b d e n o its state data . s A
Sy m e t s . i r t S ng c s s a l has a v y r e solid m i plementa i t on of
Get s a H h d o C e() h t at is ba d e s on t e h strin s g
i e d n i t fy a stri g n field a h t t should e b uni u q e among objects
SSN fi l e d), you c n a si p m ly ca l l e G t h s a H C ( e d o ) n o t e h s d l e i f str n i g e r pr s e entation:
/ / n r u t e R a h s a h e d o c d e s a b n o e h t s ' n o s r e p N S S .
c i l b u p e d i r r e v o t n i ) ( e d o C h s a H t e G
{
n r u t e r ; ) ( e d o C h s a H t e G . N S S
}
f I u o y t o n n a c y f i t n e d i a e l g n i s t n i o p f o a t a d n i r u o y , s s a l c t u b e v a h v o n e d d i r r e
( g n i r t S o T ), y u o
can simply re u t rn the a h sh code of the string e r tur e n d from your c s u tom
n i r t S o T g() n e m e l p m i t i t a on:
/ / n r u t e R a h s a h e d o c d e s a b r u o m o t s u c ) ( g n i r t S o T .
c i l b u p e d i r r e v o t n i ) ( e d o C h s a H t e G
{
e r t n r u S o T i r t ) ( g n H t e G . s a o C h ( e d );
}

e D pt. f o T I B J S , E S I e g a P 7 5
s n r u t e r fa e s l . w o N c d i s n o er h t e fol o l i w ng n i voca i t on:
u o y o h s u d l
u l k c w l u o d e v a h , t i t e h
ch r a ac e t r d a t a . Therefore, if u o y n a c
(such as the e P r o s n s
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7


e R d n a s e p y T e u l a V n e e w t e B g n i t r e v n o C 0 1 . 3 f T e c n e r e y : s e p g n i x o B d n a
g n i x o b n U i t a r e p O n o s
i G ven h t at .NET defines w t o major ca e t gories of types v ( a u l e b s a ed a d n e r r e f e c n e
s a b e , ) d you a m y c o casionally e n d e to rep s e r ent a a v ri b a le of one cat o g e ry as a variable
f o the other cate o g ry. # C provides a e v ry i s mple m c e hanis , m term d e o b xing, to conve t r
a va u l e type to a e r e f rence ty e p . m u s s A e
/ / e k a M a t r o h s e u l a v e p y t .
t r o h s s = ; 5 2
, f I du i r ng the c u o rse f o y u o r appli a c tion, you wish to rep e r s n e t this va u l e t p y e as a
r e f e r ence type, you wo l u d box the v l a ue as f l o lows:
/ / x o B e h t e u l a v o t n i n a t c e j b o e c n e r e f e r .
t c e j b o t r o h S j b o = ; s
n i x o B g n a c be m r o f ally e d f n i ed as h t e proce s s of e p x l c i i l t y v n o c r e ti g n a u l a v e e p y t in o t
spondi g n refer n e ce t e p y by stori g n h t e vari b a le in a e t s y S m b O . ject . h W en you
x o b a va , e u l e h t R L C s e t a c o l l a w e n a t c e j b o n o e h t p a e h d n a s e i p o c e h t e u l a v s e p y t e u l a v
) 5 2 , e s a c s i h t n i ( o t n i t a h t e c n a t s n i . What is returned to you is a refer n e ce to h t e ne l w y
a l l ocated b o ject. Usi g n this technique, .NET e v e d l p o r e s v a h e o n ne d e to make e s u of a
set of w p a r per classes s u d e to te p m orar l i y t a e r t s a t k c a d ta as he p a -al o l cated objects.
The op t i s o p en o r e p a i t on is also e p rm d e t t i h t r h g u o n u boxing. U b n o f o s s e c o r p e h t s i g n i x
v n o c r e t n i g
e h t e u l a v d l e h n i e h t t c e j b o e c n e r e f e r k c a b o t n i a g n i d n o p s e r r o c e u l a v e p y t n o e h t k c a t s .
e h T g n i x o b n u i t a r e p o on s n i g e b y b g n i y f i r e v t a h t h t e g n i v i e c e r data t e p y s i equ l a v i ent to
e h t d e x o b , e p y t d n a f i , o s t i s e i p o c e h t e u l a v k c a b o t n i a l a c o l k c a t s - d e s a b a v r e l b a i . F r o
o w r s k c c u s e l u f s s ly, g v i en that the underlying
typ e o f h t e S j b o h r o tis i d n eed ash r o t (youll a x e i m ne t e h # C n i r o t a r e p o g n i t s a c d n i l i a t e
e h t t x e n , r e t p a h c o s d l o h t h g i t r o f : ) w o n
/ / x o b n U t e h e c n e r e f e r k c a b o t n i a g n i d n o p s e r r o c t r o h s .
t r o h s t r o h S r e h t o n a = ; t r o h S j b o ) t r o h s (
, n i a g A t i s i y r o t a d n a m t a h t u o y x o b n u o t n i an approp i r a e t da a t type. , s u h T h t e
g n i w o l l o f
b n u oxing l g o ic gene a r tes n a a v n I l a C d i s E t e c x p o i t n x e c n o i t p e e r o m ( l i a t e d s n o
c x e n o i t p e d n a h l n i g
/ / l a g e l l I g n i x o b n u .
c i t a t s d i o v ] [ g n i r t s ( n i a M ) s g r a
{
. . .


e h T e p y t d e n i a t n o c n i e h t x o b s i T O N a , t n i t u b a ! t r o h s
t n i i = ; t r o h S j b o ) t n i (
}
d i l a v n I ( h c t a c t s a C E e c x i t p n o ) e
{
o C n e l o s r W . e t i e n i L ( O O " ! S P \n } 0 { ", T . e S o r t in ) ( g );
}

e D pt. f o T I B J S , E S I e g a P 8 5
h t t a o y u have creat d e a variable of t p y e o h s rt:
a cor e r
, e l p m a x e e h t g n i w o l l o f g n i x o b n u n o i t a r e p o
in e t p a h C r 6):
y r t
{
/ /
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

}

o S me Practi a c l (U B ) n o i x ng Ex m a l p es
So u o y , m h t e b y a i k n i d l u o w n e h w , g n y u o r a e ll d e e n y t o m n a ually bo ( x o r unbox a d a ) t a
? e p y t e h T s u o i v e r p e l p m a x e s a w y l e r u p e v i t a r t s u l l i n i , e r u t a n s a e r e h t s a w no d o o g
o h s rt a t a d t n i o p . e h T h t u r t f o the r e t t a m s i t a h t u o y
l l i w m o d l e s f i r e v e d e e n o t y l l a u n a m x o b a t a d
r e l i p m o c y l l a c i t a m o t u a s e x o b s e l b a i r a v n e h w e t a i r p o r p p a . r o F , e l p m a x e f i u o y s s a p a
e u l a v e p y t o t n i a d o h t e m g n i r i u q e r n a t c e j b o , r e t e m a r a p n i x o b g s r u c c o d n i h e b e h t
curt n i a s.
s s a l c
{
c i t a t s d i o v ] [ g n i r t s ( n i a M ) s g r a
{
/ / e t a e r C n a t n i e u l a v ( . ) e p y t
t n i t n I y m = ; 9 9

/ / e s u a c e B t n I y m s i d e s s a p o t n i a
/ / d o h t e m d e p y t o t o r p o t e k a t n a , t c e j b o
/ / t n I y m s i ' d e x o b ' y l l a c i t a m o t u a .
Use i h T s j b O c e t I y m ( n ) t ;
Con l o s e e R . d a L ( e n i ; )
}

c i t a t s d i o v t c e j b o ( t c e j b O s i h T e s U ) o
{
e u l a V " ( e n i L e t i r W . e l o s n o C f o o : s i , " } 0 { ; ) o
}
}
c i t a m o t u A g n i x o b a s l o s r u c c o n e h w g n i k r o w wi h t h t e p y t s e of e h t . E N T e s a b cl s a s
a x e mp , e l t e h S e t s y m. l o C l t c e n o i s names a p ce (formally examined i n
a h C pter 7) defines a c s a l s ty e p named r r A a s i L y t. k i L e o m st o c e l l i t c n o e p y t , s
Arr L y a ist provides mem e b rs t a h t a l l ow you o t insert, o t b a , n i a d n remo e v t i ems:
t s i L y a r r A . s n o i t c e l l o C . m e t s y S s s a l c c i l b u p : , t c e j b o
y S s . m e t l o C c e l n o i t s I . Li t s ,
Sys m e t . l o C e l c n o i t s I . Co l l c e t n o i ,
Sys m e t . l o C e l c n o i t s E I . n m u r e a e l b ,
e l b a e n o l C I
{
. . .
c i l b u p l a u t r i v t n i t c e j b o ( d d A ; ) e u l a v
c i l b u p l a u t r i v d i o v t n i ( t r e s n I , x e d n i t c e j b o ; ) e u l a v
c i l b u p l a u t r i v d i o v t c e j b o ( e v o m e R ; ) j b o
c i l b u p l a u t r i v t c e j b o t n i [ s i h t ] x e d n i ; t e g { ; t e s }
}



e D pt. f o T I B J S , E S I e g a P 9 5
n o s a e r to b x o d n a ( h t en x o b n u ) e h t
e p y t s. h c u M f o h t e e m i t , h t e # C
m a r g o r P
a r b i l r e i s. F r o
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

As u o y a c n e e s , the e s mem e b rs ope a r te n o e g n r e ic s y S t . m e b O j c e t e p y t s. v i G en that
v e r e yth n i g
e f r e p c y l t : l a g e l
] [ g n i r t s ( n i a M d i o v c i t a t s ) s g r a
{
. . .
t s i L y a r r A s t n I y m = w e n ; ) ( t s i L y a r r A
myI s t n . d d A 8 ( 8);
myI s t n . d d A 3 ( . ) 3 3 ;
myI s t n . d d A f ( a ) e s l ;
}
, r e v e w o H n e v i g r u o y g n i d n a t s r e d n u f o e u l a v s e p y t d n a e c n e r e f e r , s e p y t u o y t h g i m
h w at s a w c a l p ed into h t e r r A a s i L y t y t pe. c n e r e f e R ( es? Co s e i p of
? s e c n e r e f e r s e i p o C f o ) ? s e r u t c u r t s t s u J e k i l h t i w e h t s u o i v e r p h T e s U s i ( t c e j b O ) method, it
h s o l u d be c a e l r t a h t ea h c of e h t e t s y S m 3 t n I . 2 a t a d e p y t s e r e w e d n i ed boxed befo e r
g n i e b d e c a l p o t n i the r A r y a List type. o T e v e i r t e r n a m e t i m o r f e h t
s i L y a r r A t , e p y t u o y e r a d e r i u q e r o t x o b n u : y l g n i d r o c c a
) ( s t n I x o b n U d n A x o B d i o v c i t a t s
{
/ / x o B s t n i o t n i . t s i L y a r r A
t s i L y a r r A s t n I y m = w e n ; ) ( t s i L y a r r A
myI s t n . d d A 8 ( 8);
myI s t n . d d A 3 ( . ) 3 3 ;
myI s t n . d d A f ( a ) e s l ;

/ / x o b n U t s r i f m e t i m o r f . t s i L y a r r A
t n i m e t I t s r i f = ; ] 0 [ s t n I y m ) t n i (
t s r i F " ( e n i L e t i r W . e l o s n o C m e t i s i , " } 0 { ; ) m e t I t s r i f
}
To be s , e r u o b xing a d n n u b n i x o g s e p y t s e k a t e m o s n i s s e c o r p g m i t e , d n a f i e s u d o h t i w ut
t n i a r t s e r , d l u o c t r u h e h t pe e c n a m r o f r f o r u o y p p a li a c ti n o . Ho , r e v e w t i w h s i h t .N T E
e u q i n h c e t , u o y e r a l b a e o t m y s metri a c l l y operate n o va e u l - s a b d e n a d r f e n e r e ce-ba e s d
e p y t s.

1 1 . 3 P g n i n i f e D r o g r a m
w o N t a h t u o y e v a h e s en w o h to e r a l c e d s s a l c , s e l b a i r a v s t e l e e s w o h o t e d f n i e t a d a h t at
be reassi e n g d . C# f f o ers the const e k yword to define v r a ia l b es with a fixe , d
una e t l rable v l a ue. Once the val e u of a s n o c t n a t has b e e n estab i l s d e h , a y n a t t e t p m o t
a e t l r t i r u s e t l s in a o c mp e l i r error . n U like
e s u d o t qua y f i l r a p ame s r e t r o n r u t e r s e u l a v , a d n is r s e er d e v for e h t c e r n o i t a of c o l l a o r
e c n a t s n i - l e v e l data. t I s i m i portant to und r e stand that t e h val e u a s s ig d e n o t a t n a t s n o c
a v r b a i e l t s u m e b n w o n k at o c mp e l i time, a d n th e r e fore a c n o st n a t member cannot be
d e n g i s s a to an object e r f e r e nce (whose value s i
e s u f o e h t co s n t e k ywo , d r e m u s s a e h t g n i w o l l o f s s a l c : e p y t
s s a l c a t a D t s n o C

e D pt. f o T I B J S , E S I e g a P 0 6
u m i t l a y l e t s e v i r e d m o r f th s i o c m n o m a b se s a l c s, e h t fol g n i w o l c d o e is
r e d n o w y l t c a x e
t n a t s n o C
d l u o h s e v e n r
in C , + + # C n i h t e s n o c t e k y r o w d cannot be
d e t u p m o c t a ) e m i t n u r . T e t a r t s u l l i o t e h
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

{
/ / e h T e u l a v d e n g i s s a o t a t s n o c t s u m e b n w o n k
/ / t a e l i p m o c . e m i t
c i l b u p t s n o c g n i r t s m a e T a b N t s e B = ; " s e v l o w r e b m i T "
c i l b u p t s n o c e l b u o d I P e l p m i S = ; 4 1 . 3
c i l b u p t s n o c l o o b
c i l b u p t s n o c l o o b y t i s l a F = ; h t u r T !
}

c i t o N e h t at the va u l e of each constant is known at the t m i e of m o c pilation. In fac , t f i
vi w e th s e e constants using a d l i s x e . m e, you would fi d n the value a h rd-
d o c ed direc l t y into the s a sem

n o i t a r e t I # C 2 1 . 3 s t c u r t s n o C
l l A progra m m n i g l n a g g a u es pr v o d i e a w ys to r p e e t a bl c o ks of c d o e u t n il a e t rm n i a i t g n
o c ndi i t n o has b e e n m t e . Regar l d e s s f o i h w ch lang a u e g u o y have used in h t e p s a , t t e h
# C t i erat o i n sta m e t ents sh u o ld n t o raise t o o m n a y e e y b w o r s and s o h u d l re u q i e r i l t e l t
l p x e a a n tion. C# provid s e t e h o f llowi g n
r u o f it r e at n o i constructs:
o f r p o o l
r o f each/in loop
i h w le p o o l
o d /wh l i e p o o l
Le t s c i u q kly exa i m e n e c a h loopi g n c n o struct in tu n r .

e h T o f r p o o L
n e h W u o y d e e n o t e t a r e t i r e v o a k c o l b f o e d o c a d e x i f r e b m u n f o , s e m i t e h t o f r t a t s em n e t
s i e h t c n o
k c o l b f o e d o c s t a e p e r , f l e s t i s a we l l as the e t r n i m a n i t g cond t i ion. t u o h t i W be o b a l r g n i e h t
, t n i o p e r e h s i a e l p m a s f o e h t : x a t n y s
/ / A c i s a b r o f p o o l .
] [ g n i r t s ( n i a M d i o v c i t a t s ) s g r a
{
y l n o e l b i s i v n i h t i w e h t e p o c s f o e h t r o f . p o o l
t n i ( r o f i = ; 0 i < ; 0 1 ) + + i
{
i r W . e l o s n o C r e b m u N " ( e n i L e t : s i } 0 { , " ; ) i

t o n e l b i s i v . e r e h

l l A of r u o y d l o C, + C +, and Java t i r cks s i t ll hold when buil i d ng a C# for s e t a t ment. Y u o
n a c e r c ate c p m o l x e e t r i m nat n i g cond t i io s n , bu l i d e d n l s e s loops, and a m ke use of th e
o t o g , co u n i t n e, and r b eak
o w rk 2.0 D S K do u c mentation if you req i u re
furt e h r de a t ils on h t e C# o f r k y e o w rd.

e D pt. f o T I B J S , E S I e g a P 1 6
h t u r T = ; e u r t
you e w re o t
l b y.
t c u r t s f o s n o i p m a h c . n I , e c n e s s e u o y e r a e l b a o t y f i c e p s w o h y n a m s e m i t a
/ / ! e t o N ' i ' s i
}
/ / ' i ' s i
}
o w y e k rds . u s s a l l I m i s i h t d n e b l l i w u o y t a h t e t t a r e s n o c n o i truct
i f e e s u o y s a t. C u s n o l T E N . e h t t Frame
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

e h T h c a e r o f Loop
h T e C# r o f c a e h k y e word a l l ows y u o to iter e t a over a l l items t i w hin an a y a r r , i w tho t u
the e n d e to e t st for the r a y a r s u e p p r i m i l t. He e r r a e two x e amp e l s using r o f each, one t o
r t a e v s r e n a a a r r y of strings
/ / e t a r e t I y a r r a s m e t i g n i s u h c a e r o f .
] [ g n i r t s ( n i a M d i o v c i t a t s ) s g r a
{
m o C " { e l p x A r o g l h t i s m ",
o D " u o y r e b m e m e R c i s s a l C , " ? M O C
# C " d n a e h t . T E N ; } " m r o f t a l P
h c a e r o f g n i r t s ( s n i ) s k o o b
Con l o s e r W . t i e e n i L ( ) s ;

] [ t n i s t n I y m = { , 0 1 , 0 2 , 0 3 0 4 ; }
h c a e r o f t n i ( i n i ) s t n I y m
Con l o s e r W . t i e e n i L ( ) i ;
}
n I d a i t i d n o to t i era i t ng over s m i ple arrays, r o f e c a h is also b a le to t i erate v o er system
s p u p i l d e r o s u er-def e n i d c l o lecti s n o . l l I ho d l f f o n o e h t deta s l i unt l i e t p a h C r 7, s a th s i
t c e p s a of h t e fo c a e r h d r o w y e k e t n a l i s an r e d n u sta d n ing f o in c a f r e t e- a b sed
g n i m m a r g o r p a d n t e h o r le f o t e h I n E u a r e m tor and n E I um l b a r e e t n i e a f r ces.

h T e w l i h e a d n / o d wh l i e L o o i p ng Co s n tructs
The wh l i e lo g n i p o u r t s n o c ct s i l u f e s u shou d l o y u w h s i o t e c e x ute a o l b ck of
t a t s e s t n e m u l i t n m o s e m r e t i a n i t g n condition a h s been reached. W t i hin the scope of
o t e s n u e r th s i e t i m r nati g n v e e t n s i e d n i ed
; d e h s i l b a t s e o , e s i w r e h t u o y l l i w be t s c u k in an s e l d n e s o l op. In h t e o f l l o i w g n e a x mple,
the m s e sage In while l p o o w l i l e b cont n i u s u o ly printed unt l i the user i m r e t nates e h t
p o o l by enter n i g yes at e h t c m o ma d n pr m o p : t
c i t a t s d i o v ] [ g n i r t s ( n i a M
{
g n i r t s e n o D s I r e s u = ; " o n "

/ / t s e T n o a e w o l r s s a l c
) ( r e w o L o T . e n o D s I r e s u ( e l i h w = !
{
e r A " ( e t i r W . e l o s n o C u o y ? e n o d : ] o n [ ] s e y [ ; ) "
e n o D s I r e s u = ; ) ( e n i L d a e R . e l o s n o C
n I " ( e n i L e t i r W . e l o s n o C e l i h w ; ) " p o o l
}
}
y l e s o l C l e r a e t d to h t e h w ile p o o l s i h t e do/w i h le a t s t e m e nt. k i L e a sim e l p i h w le l o o p,
do/ e l i h w
s i t a h t do/ l i h w e o l p o s e r a a u g a r nteed to e e x cute t e h
e r r o c spond g n i b k c o l of e d o c at s a e l t c n o e (in c n o - r t s a , t t i is s o p sib e l that a s m i l p e
l i h w e o l op may never c e x e ute if the ter i m nati g n condition is false

e D pt. f o T I B J S , E S I e g a P 2 6
and t e h other o t t a r ver e s n a arr y a of integers:
] [ g n i r t s s k o o b =
a wh l i e loo , p you l l i w , of c u o rse, e n ed
r a s g )
y p o c f o e h t . g n i r t s
e y " s")
is e s u d when u o y ne d e o t perform some c a i t n o for an e d n u ter i m e n d u n m r e b
of m i t es. e h T d f i fer e c n e
m o r f h t e s n o e . ) t
C# d n a P T E N . m m a r g o r i g n I 6 0 S 1 6 7

c i t a t s d i o v ] [ g n i r t s ( n i a M ) s g r a
{
g n i r t s e n o D s I r e s u = ; " "
{
W . e l o s n o C r e t i e n i L ( I " n
e r A " ( e t i r W . e l o s n o C
e n o D s I r e s u = ; ) ( e n i L d a e R . e l o s n o C
) ( r e w o L o T . e n o D s I r e s u ( e l i h w } = ! ; ) " s e y " / / e t o N e h t ! n o l o c i m e s
}

t c u r t s n o C w o l f l o r t n o C # C 3 1 . 3 s
Now h t at o y u n a c t i erate ov r e a bl k c o of statem n e t , s t e h n x e t relat d e con p e c t is how to
n o c t o r l h t e f w o l of pr g o r m a exe n o i t u c . # C s e n i f e d w t o l p m i s e s n o c r t t c u s to a t l er h t e
w o l f of u o y r pr g o a r , m a b s d e n o var o i us contin e g ncies:
e h T if/ l e se a t s t m e ent
e h T w s itch t s t a t n e m e

h T e i e / f l e s Sta m e t t n e
s r i F t up is our go d o fr e i nd the if/else a t s m e t n e t. e k i l n U in C n a d + C , + howe e v , r the
if/ s l e e st t n e m e t a in # C s e t a r e p o y l n o on o B o e l an r p x e s e , s n o i s o n t ad c o h va e u l s such
as , 1 0 . Given t i h , s if/ e s l e
n w o h s in l b a T e 3-6 in r e d r o o t t b o a n i a r e t i l al e l o o B an va . e u l

e l b a T 3-6. # C n o i t a l e R al d n a u q E a y t i l Op r e t a ors
# C y t i l a u q E /Rela i t onal p O erator Ex p m a le Us g a e g n i n a e M in L e f i
Re u t rns t u r e y l n o if e c a h p x e s e r n o i s s i

Re u t rns t u r e l n o y f i a e ch p x e r n o i s s e


n r u t e R s e u r t if e p x re s s n o i A is s s e l

r g e t a er , n a h t e l ss h t an or e a u q l to,
or greater t a h n or q e ual o t expressi n o



A a g in, C and C + + pr m a r g o mers need to be aw e r a t a h t t e h old tric s k of t s e ti g n a
n o c dition for a a v lue t o n eq a u l to e z ro w l l i not work in C#. Le s t s y a you n a w t to see if
t e h t s ring you r a e working w t i h is o l nger t n a h zero char c a ters. o Y u a m y be tempted o t
write \
/ / s i h T s i , l a g e l l i n e v i g t a h t h t g n e L s n r u t e r n a , t n i t o n a . l o o b
g n i r t s y a D e h T f O t h g u o h t = u o Y " N A C h c a e t n a d l o g o d w e n ; " s k c i r t
if( o h t u t h g f O T a D e h y e L . n t g h)
{

e D pt. f o T I B J S , E S I e g a P 3 6
e l i h w / o d o l o ) " p ;
u o y ? e n o d ] s e y [ : ] o n [ ; ) "
t s a e m e t n s t y t p c i l a y l e v l o v n i e h t e s u of the # C e p o r r o t a s
== e g a ( f i = = ) 0 3
e h t e m a s
!= " o o F " ( f i = ! ) r t S y m
s i
t n e r e f f i d
< s u n o b ( f i < ) 0 0 0 2
, n a h t
> s u n o b ( f i > ) 0 0 0 2
<= s u n o b ( f i = < ) 0 0 0 2
B
>= s u n o b ( f i = > ) 0 0 0 2






you need to o m dify your cond t i ional expr s e sion as l o f lows:

/ / , l a g e L s a s i h t s e v l o s e r o t r e h t i e e u r t r o . e s l a f
( f i 0 = ! ) h t g n e L . y a D e h T f O t h g u o h t
{
. . .
}
An if statement m y a be c m o d e s o p of complex expressions as we l l n a d can n o c tain
me t n s o t per o f rm e r o m -co l p m ex test g n i . h T e t n y s ax is i e d nti a c l to C + ( +)
d n a v a J a a ( nd not o t o far e r d e v o m r f m o i V s l a u c i s a B ). o T u b d l i o c mpl x e x e r p essi s n o ,
# C offe s r an e p x ect d e set of c d n o i t i a n o l operators, as shown n i Tab e l 3-7.
e l b a T 3-7. # C t i d n o C ional Ope t a r r o s
Operator E m a x p e l g n i n a e M n i Lif e
&& e g a ( ( f i = = ) 0 3 & & e m a n ( = = ) ) " d e r F " o C n l a n o i t i d AND
o t a r e p o r
|| e g a ( ( f i = = ) 0 3 | | e m a n ( = = ) ) " d e r F " Con i t i d a n o l O R
p o erator
! f i ( B y m ! ool) n o C t i d ional T O N p o erator

The switch Statement
The e h t o r simple se e l ction cons r t u t c o e f f red by C# is h t e t i w s ch stat e m e nt. As n i othe r
C b e s a d la , s e g a u g n e h t c t i w s h t s ateme t n a w o l l s o y u o t n a h dle g o r p m a r flow a b s d e on a
d e n i f e d e r p t e s f o s e c i o h c . r o F , e l p m a x e the foll i w o ng ( n i a M ) i g o l c s t n i r p a i c e p s fic str g n i
e g a s s e m a b e s d n o n o e of t o w o p ss l b i e e s l c e i t ons (t e h de u a f lt ca e s a h ndles n a n i a v i l d
sel c e o i t n):
/ / h c t i w S n o a l a c i r e m u n e u l a v .
c i t a t s d i o v ] [ g n i r t s ( n i a M
{
1 " ( e n i L e t i r W . e l o s n o C , ] # C [ 2 ; ) " ] B V [
a e l P " ( e t i r W . e l o s n o C e s k c i p r u o y e g a u g n a l : e c n e r e f e r p ; ) "

g n i r t s e c i o h C g n a l = ; ) ( e n i L d a e R . e l o s n o C
t n i n = ; ) e c i o h C g n a l ( e s r a P . t n i

h c t i w s ) n (
{
e s a c : 1
d o o G " ( e n i L e t i r W . e l o s n o C , e c i o h c # C s i a e n i f ; ) " . e g a u g n a l
bre k a ;
e s a c : 2
B V " ( e n i L e t i r W . e l o s n o C : T E N . , P O O , g n i d a e r h t i t l u m n a d ; ) " ! e r o m
bre k a ;
def l u a : t

e D pt. f o T I B J S , E S I e g a P 4 6
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

. . .
}
f I you i w h s to a m ke use of t e h e L . g n i r t S n t g h o r p pe t r y to de e t r i m e n f i you have a n
emp y t string,
s l e e state
r a s g )
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

Con l o s e r W . t i e e n i L ( e W " ll . . . d o o g k c u l h t i w ; ) " ! t a h t
bre k a ;
}
}
e n O e c i n u t a e f re of e h t # C i w s tch t s ate t n e m s i t a h t you can eva a u l te s i r t ng data in
n o i t i d d a o t r e m u n ic d a t a . He e r is n a updated sw c t i h statem n e t h t at does this very thing
n ( o i t e c we a h ve no e e n d
p a p a o r ch):
c i t a t s d i o v ] [ g n i r t s ( n i a M ) s g r a
{
W . e l o s n o C r e t i e n i L ( C " #
e s a e l P " ( e t i r W . e l o s n o C k c i p r u o y e g a u g n a l : e c n e r e f e r p ; ) "

g n i r t s e c i o h C g n a l = L d a e R . e l o s n o C ; ) ( e n i
h c t i w s h C g n a l ( o ) e c i
{
e s a c : " # C "
d o o G " ( e n i L e t i r W . e l o s n o C , e c i o h c # C s i a e n i f ; ) " . e g a u g n a l
bre k a ;
e s a c : " B V "
B V " ( e n i L e t i r W . e l o s n o C : T E N . , P O O g n i d a e r h t i t l u m d n a ; ) " ! e r o m
bre k a ;
def l u a : t
Con l o s e r W . t i e e n i L ( e W " ll . . . d o o g k c u l h t i w ; ) " ! t a h t
bre k a ;
}
}

3 4 1 . T e h o c p m lete s t e of C
C# CLS
n a h t r o h S d ? t n a i l p m o C m e t s y S T e p y g n a R e i n a e M ng in L e f i
sbyte No Sy t s m e . t y B S e 8 2 1 o t 127 n g i S ed 8-bit
b m u n e r
t y b e Y s e Sy t s m e . t y B e 0 o t 5 2 5 d e n g i s n U 8- t i b
number
short Y s e S e t s y m t n I . 16 2 3 ,768 to 2 3 ,767 d e n g i S 6 1 - t i b
u n mb r e
ush r o t No y S s . m e t U n I t16 0 o t 65 5 , 35 n U sign d e 16-b t i
number
n i t Y s e Sy m e t s . 3 t n I 2 , 2 4 1 7,4 , 3 8 648 to i S g d e n 2 3 - t i b
u n mb r e
, 7 4 1 , 2 4 7 4 6 , 3 8
n i u t No Sy t s m e . t n I U 32 0 to 4 9 2 , 4 6 9 , 7, 5 9 2 U s n ign d e 32-b t i
number
n o l g Y s e Sy t s m e . 6 t n I 4 9, 3 2 2 ,37 , 4 5 8 , 6 3 0 , 2 775,808 S e n g i d 64-bit
m u n b r e

e D pt. f o T I B J S , E S I e g a P 5 6
o t pa e s r e h t s u er a t a d o t n i a m u n e c i r v u l a e w t i h t i h s
r o ; ) " B V
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

o t 9, 3 , 3 2 2 72,0 8 , 6 3 54 7 7 , 5,8 7 0
ulong No Sy t s m e . t n I U 64 0 to U s n ign d e 64-bi t
8 1 , 6 4 4 , 9 0 7 , 3 7 0 , 4 4 7 ,55 1 6 , 1 5 u n m r e b
a h c r Y s e Sy t s m e . a h C r 0 0 0 U 0 to f U fff A single 6 1 - i b t
i n U code character
float Y s e S e t s y m n i S . gle . 1 5 0 1
- 5 4
to . 3 4 0 1
38
32- t i b floa i t ng
o p in t
number
dou l b e Y s e S e t s y m u o D . ble . 5 0 10
- 4 2 3
o t 1.7 10
0 3 8
4 6 -b t i float n i g
o p in t
number
o o b l Y s e Sy t s m e . l o o B n a e r t ue or s l a f e e R e r p e s nts tr t u h
or
fals y t i
deci a m l Y s e S s y e t m c e D . i l a m 10
0
o t 10
8 2
A 6 9 -b t i sig e n d
number
str n i g Y s e S e t s y m r t S . ing L m i ited by syst m e memory R p e resen s t a
e s t of
i n U c d o e characters
obj c e t Y s e S e t s y m j b O . ect y n A e p y t a c n e b t s or d e e h T base l c ass
of l l a
in an o j b c e t r a v i b a le types in the .NET
v i n u erse

d o h t e M 5 1 . 3 r e t e m a r a P s r e i f i d o M
t e M ho s d s ( ta i t c a d n instan e c level) t n e d to take pa a r mete s r pa e s s d in by the caller.
, r e v e w o H k i l n u e m o s e g o r p ra n i m m g languages, C# provides a set of a p a r me e t r
o m dif r e i s h t at control how argum n e s t a e r e s t n n i to (and o p ssibly retur d e n from) a
given met d o h , s a h s o n w n i T b a le 3-5.

e l b a T 3-5. C# Pa a r e m ter M e i f i d o s r
r e t e m a r a P o M difier g n i n a e M n i L e f i
o n ( n ) e If a parame e t r is o n t marked t i w h a parameter m d o ifie , r t i is
a u s s m d e t o
be a p ss d e y b v l a , e u me n a i g n the a c l l ed method re e c ives a o c py of t e h
original data.
u o t t u p t u O parameter a s r s s a e ign d e b e h t y me h t d o b n i e g a c d e l l ( d n a
there o f r e
passe d by refere c n e . ) f I e h t d e l l a c d o h t e m s l i a f o t n g i s s a t u p t u o , s r e t e m a r a p
y e r a u o i o c a d e u s s mpi r e r e l ror.
par m a s h T is a p ramet r e mo i d fier a l l w o s u o y o t send in a a v ria l b e
nu e b m r o f
iden c i t a l l y e p y t d g r a u t n e m s s a a e l g n i s i g o l c l a parameter. A o h t e m d n a c
e v a h y l n o a e l g n i s m a r a p s mo i d f , r e i and t i u m st e b t e h f n i l a r a p ame e t r o f

e D pt. f o T I B J S , E S I e g a P 6 6
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

e h t d o h t e m .
e r f Th a v e l i e u s in t i i l a l s s a y ig e n d by the a c l e l r, a d n may be opti n o ally
reassi n g ed
b l a c e h t y l s a ( d o h t e m d e t a d e h t a i a s l d e s s a p o s b y r e f e r c n e e). N p m o c o il r e
r e ror is n e g e a r ted if h t e a c l l ed met o h d fails to a s s i n g a e r f e t e m a r a p r .

t l u a f e D e h T P e t e m a r a r- r o i v a h e B g n i s s a P
e h T f e d a t l u r e n n a m n i i h w ch a par m a e e t r s i sent t n i o a u f nc o i t n is by value . m i S l p y
pu , t if y u o o d t o n k r a m n a g r a u e m nt with a parameter-centric m i d o fie , r a copy of h t e
varia e l b s i pass d e in o t h t e u f ncti : n o
/ / s t n e m u g r A e r a d e s s a p y b e u l a v y b t l u a f e d .
c i l b u p c i t a t s t n i t n i ( d d A , x t n i ) y
{
t n i s n a

/ / r e l l a C l l i w t o n e e s e s e h t s e g n a h c
/ / s a u o y e r a g n i y f i d o m a y p o c f o e h t
/ / l a n i g i r o
x = ; 0 0 0 0 1
y = ; 8 8 8 8 8
n r u t e r ; s n a
}
e H re, t e h n i o c i m g n integer parameters l l i w be pas d e s by a v lue. T e h e r f , e r o if you
a h c nge the a v lues of the paramete s r n i h t i w e h t o c s pe of the memb r e , the a c e l l r is
b s i l sfu y l l unaware, gi n e v that you r a e h c anging the a v e u l s of copies of h t e a c l r e l s
inte e g r d a t a t p y e : s
] [ g n i r t s ( n i a M d i o v c i t a t s ) s g r a
{
t n i x = , 9 y = ; 0 1
. e l o s n o C r W e t i e n i L ( e B " r o f e
. e l o s n o C r W e t i e n i L ( n A " e w s r
. e l o s n o C r W e t i e n i L ( f A " r e t
}
As you would ho e p , the values of x d n a y re a m n i iden i t a c l before and f a e t r t e h a c l l o t
d A d ) ( .
e h T t u o r e i f i d o M , t x e N e w e v a h h t e s u e of t u p t u o a p ram e t e rs. s d o h t e M t a h t e v a h n e e b
d e n i f e d o t e k a t t u p t u o e t e m a r a p rs e r a under ob i l a g i t n o to ass g i n t e h m o t an p a pr p o riate
va u l e r o f e b e e n i t i x g the meth d o in ques i t n o (if y u o fa l i to s n e r u e this, y u o w l l i rece v i e
T t s u l l i o r t a e, e h r e i l a n a s t r e a n ti e v e v rsion o f t e h Ad ( d ) d o h t e m t a h t
s n r u t e r e h t m u s f o o w t s r e g e t n i g n i s u e h t # C u o t r e i f i d o m e t o n ( e h t l a c i s y h p n r u t e r e u l a v
f o s i h t d o h t e m s i w o n i o v d):
/ / t u p t u O s r e t e m a r a p e r a d e t a c o l l a y b e h t . r e b m e m
c i l b u p c i t a t s d i o v t n i ( d d A , x t n i , y t u o t n i ) s n a
{
s n a = x + ; y
}

e D pt. f o T I B J S , E S I e g a P 7 6
Y:
= x + ; y
d t a . a
} 0 { , " , x ( d d A ; ) ) y
: l l a c : X , } 0 { : Y
, " } 1 { , x ; ) y

, " } 1 { , x ; ) y
a c ll:
s i :
: X , } 0 {
r e l i p m o c ) s r o r r e .
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

g n i l l a C a d o h t e m h t i w t u p t u o m a r a p ete s r l a so s e r i u q e r e h t use of t e h u o t e i f i d o m r .
Recall h t a t
r o f e b e u e s ( f i you do so, the ori i g a n l v l a ue s i lo t s af e t r e h t a c l l ), for e a x mple:
] [ g n i r t s ( n i a M d i o v c i t a t s ) s g r a
{
/ /





e h t e u l a v f o r u o y n o i t a m m u s g n i s u n a u o t t u p r e t e m a r a p . Ho , r e v e w the C#
u o t m d o ifier o d es serve a very use u f l purpose: it a l l ows the caller to b o tain mul i t ple
re u t rn values from a single met o h d i o v n ca i t on.
/ / g n i n r u t e R e l p i t l u m t u p t u o s r e t e m a r a p .
c i t a t s d i o v t u o ( s e u l a V e s e h T l l i F t n i , a t u o g n i r t s , b t u o l o o b ) c

9;
y o j n E " r u o y ; " . g n i r t s
c = ; e u r t
}
e h T r e l l a c o w u d l e b ab e l o t k o v n i e e h t fo l l ow g n i me o h t : d
i r t s ( n i a M d i o v c i t a t s ] [ g n ) s g r a
{
t n i ; i
g n i r t s ; r t s
l o o b ; b

t u o ( s e u l a V e s e h T l l i F , i t u o
o C n e l o s r W . e t i e n i L ( n I " t is:
g n i r t S " ( e n i L e t i r W . e l o s n o C : s i , " } 0 { ; ) r t s
n a e l o o B " ( e n i L e t i r W . e l o s n o C : s i , " } 0 { ; ) b
}

e h T f e r r e i f i d o M
w o N r e d i s n o c e h t e s u of h t e C# e r f r a p ameter m i d o fier. f e R e n e r ce r a p ame s r e t e r a
u o y i w sh o t a l l w o a me o h t d to opera e t on (and usu l l a y change the
va e u l s of) a v r s u o i da a t s t n i o p l c e d a d e r in t e h a c e l l rs c s ope (su h c as a o s r i t g n o r
swappi g n u o r i t ne). N e t o h t e i d s n o i t c n i t between u o t t u p d n a f e r ere e c n pa r e t e m a r : s
Out u p t a p ramet r e s o d o n t d e e n to e b i t i n i a i l zed f e b o e r t y e h p s a s d e o t t e h me h t od.
T e h re o s a n for this? h T e me h t d o m s u t assign u o p t ut pa a r meters before x e i t i g n .
R f e er n e ce paramete s r t s u m e b in i t i a z i l ed r o f e b e y e h t e r a s a p sed o t e h t meth d o . T e h
a e r n o s r o f ? s i h t u o Y e r a g n i s s a p a e c n e r e f e r o t n a i x e s n i t g v r a e l b a i . f I u o y t n o d n g i s s a t i
o t n a n i t i ial a v l e u , t a h t wo l u d be t e h u q e i a v le t n of p o era n i t g on an n u ass g i ned local
varia l b e.
s t e L k c e h c t u o e h t e s u f o e h t e r f d r o w y e k by a w y f o a me d o h t h t at swa s p w t o t s r g n i s:

e D pt. f o T I B J S , E S I e g a P 8 6
local a v ria s e l b a p s d e s as out u p t va i r ables are o n t requ r i ed to be s a sign d e
o N d e e n o t n g i s s a o l l a c t u p t u o . s e l b a i r a v
t n i ; s n a
, 0 9 ( d d A , 0 9 t u o ; ) s n a
0 9 " ( e n i L e t i r W . e l o s n o C + 0 9 = } 0 { , " ; ) s n a
}
The i v e r p ous exa p m le is inte d n d e o t be illustrative in na u t re; you really h v a e no e r ason
to return
{
a =
b =
c i l b u p
, r t s t u o ; ) b
, " } 0 { ; ) i
y r a s s e c e n h w en
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

/ / e c n e r e f e R . r e t e m a r a p
c i l b u p c i t a t s d i o v f e r ( s g n i r t S p a w S g n i r t s , 1 s f e r g n i r t s ) 2 s
{
g n i r t s r t S p m e t = ; 1 s
1 s = ; 2 s
s2
}
This method can be called as so :
c i t a t s d i o v i a M ] [ g n i r t s ( n
{
g n i r t s s = t s r i F " ; " g n i r t s
g n i r t s 2 s = y M " r e h t o ; " g n i r t s
: e r o f e B " ( e n i L e t i r W . e l o s n o C , } 0 { } 1 { , " , s ; ) 2 s
f e r ( s g n i r t S p a w S , s f e r ; ) 2 s
: r e t f A " ( e n i L e t i r W . e l o s n o C , } 0 { } 1 { , " , s ; ) 2 s
}
Her , e the a c l l r e h s a s s a i e n g d an initial a v lue to lo a c l string a d ta (s a d n s2). O e c n t e h
a c l l to p a w S S i r t n ( s g ) returns, s now c n o tai s n the a v l e u M " y h t o er s g n i r t ", while s2
rep r o ts the value " t s r i F s g n i r t ".

e h T a r a p ms o M d i f i er
e h T l a n i f r e t e m a r a p r e i f i d o m is h t e m a r a p s , r e i f i d o m h c i h w a s w o l l u o y to c e t a e r a
se t n o t a e s t of c i t n e d i l l a y e p y t d a g r ume t n s s a a sin l g e l g o i l a c
r e t e m a r a p . , s e Y s i h t can e b g n i s u f n o c . o T cl a e r the r i a , s a s m u e a me h t od that e r turns h t e
r e v a age of any number of doubles:
/ / n r u t e R e g a r e v a f o e m o s ' ' r e b m u n f o s e l b u o d .
e l b u o d c i t a t s c l a C s m a r a p ( e g a r e v A e t a l u ] [ e l b u o d ) s e u l a v
{
e l b u o d m u s = ; 0
r o f t n i ( i = ; 0
m u s = + ; ] i [ s e u l a v
n r u t e r m u s ( / ; ) h t g n e L . s e u l a v
}
This me h t od a h s been e d i f n d e to take a parameter array of doubl s e . t a h W s i h t d o h t e m s i
n i t c a f g n i y a s , s i d n e S me any numb r e of o d ub e l s a d n Ill compute t e h av r e age. Given
l a C c v A e t a l u e e g a r () n i y n a f o e h t g n i w o l l o f s y a w f i ( o y u d d i not k a m e
e s u f o the m a r a p s r e i f i d o m n i f o n o i t i n i f e d e h t a r e v A e t a l u c l a C e g ) ( , e h t t s r i f n o i t a c o v n i f o
thi s met d o h w u o l d r u s e l t i o c a n mpile r err r o ):
t a t s ] [ g n i r t s ( n i a M d i o v c i ) s g r a
{
/ / s s a P n i a a m m o c - d e t i m i l e d t s i l f o s e l b u o d . . .
e l b u o d ; e g a r e v a
e g a r e v a = , 0 . 4 ( e g a r e v A e t a l u c l a C , 2 . 3
e g a r e v A " ( e n i L e t i r W . e l o s n o C f o , 0 . 4 , 2 . 3
/ / . . . r o s s a p n a y a r r a f o . s e l b u o d
] [ e l b u o d a t a d = { , 0 . 4 , 2 . 3 7 . 5 ; }

e D pt. f o I B J S , E S I T
= ; r t S p m e t
r a s g )
o h t e m d t a h t y a m b e
i < ; h t g n e L . s e u l a v ) + + i
this, you can ca l l
5 ) 7 . ;
, " } 0 { ; ) e g a r e v a



e g a P 9 6
7 . 5 : s i
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

e g a r e v a = ; ) a t a d ( e g a r e v A e t a l u c l a C
f o e g a r e v A " ( e n i L e t i r W . e l o s n o C a t a d : s i , " } 0 { ; ) e g a r e v a
Con l o s e e R . d a L ( e n i ; )
}
T a h t w p a r s p u our initial l k o o at pa a r meter mo i d fiers. l l e W i v e r s t i th s i i p o t c later in
e h t t p a h c erw n e h we a x e n i m e h t e dis i t c n o i t n between va u l e types and refer n e ce types.
t x e N , p u s t e l k c e h c t u o e h t n o i t a r e t i d n a n o i s i c e d s n o i t c u r t s n o c f o e h t C#
g n i m m a r g o r p l n a u g age .
3.16 A a r r y Mani u p lat o i n in C#
l l a m r o F y n i k a e p s g, n a y a r r a is a col e l cti n o of data po n i ts, of the a s e m defined data
, e p y t h t at a e r ac e c s d e s using a u n meri a c l i e d n x . a r r A ys are re r e f enc s e ty e p s and deri e v
named S t s y em r r A . ay. By default, .NET arrays always have a
l b i s e o t r c a e te an arr y a w t i h an ar i b trary low r e
Sy m e t s .A y a r r . r C a e te n I s c n a t e() m t e hod.
C# a r r ays c n a be c e d lared in a h n a d u f l of wa s y . Fi s r t of a l l , if u o y are r c e i t a g n n a r r a ay
e s o h w u l a v es l l i w e b sp c e ified at a later m i t e (perha s p d e u to e y t- o t -be- b o tained use r
i s ze of t e h a r r y a using squ r a e brackets ([]) t a e h t t m i e of s t i
, n o i t a c o l l a for x e a l p m e:
/ / n g i s s A a g n i r t s y a r r a g n i n i a t n o c 3 s t n e m e l e 0 { - } 2
str g n i ] [
M O C n O s k o o b = w e n ; ] 3 [ g n i r t s

/ / e z i l a i t i n I a 0 0 1 m e t i g n i r t s , y a r r a d e r e b m u n 0 { - } 9 9
] [ g n i r t s t e N t o D n O s k o o b = w e n ; ] 0 0 1 [ g n i r t s
c n O e o y u a h ve e d clared an ar y a r , you can ma e k use of the ind x e er syntax to f l i l each
t i em i w th a valu : e
/ / , e t a e r C , e t a l u p o p d n a t n i r p n a y a r r a f o e e r h t . s g n i r t s
] [ g n i r t s o b ok C n O s ; M O
M O C n O s k o o b = w e n ; ] 3 [ g n i r t s
s ' r e p o l e v e D " p o h s k r o W o t M O C d n a L T A ; " 0 . 3
e d i s n I " ; " M O C
e d i s n I "
f h c a e r o g n i r t s ( s n i ) M O C n O s k o o b
Con l o s e r W . t i e e n i L ( ) s ;
s A a d n a h t r o h s , n o i t a t o n f i u o y w o n k n a s y a r r a s e u l a v t a e h t e m i t f o u o y , n o i t a r a l c e d
y a m y f i c e p s h t e s e values t i w hin u c rly b c a r kets. t o N e t a h t in this a c se, the a r r ay si e z is
op i t onal (as t i is a c lcul t a ed
s n o i t a r a l c e d e r a : l a c i t n e d i
/ / d n a h t r o h S y a r r a n o i t a r a l c e d s e u l a v ( t s u m e b n w o n k t a e m i t f o . ) n o i t a r a l c e d
] [ t n i n = w e n ] [ t n i { , 0 2 , 2 2 , 3 2 0 ; }
] [ t n i 3 n = { , 0 2 , 2 2 , 3 2 0 ; }
T e h re is one final m n n a er in whi h c u o y a c n cr a e te n a arr y a type:
[ t n i ] 2 n = w e n ] 4 [ t n i { , 0 2 , 2 2 , 3 2 0 / / ; } 4 , s t n e m e l e 0 { - } 3
n I this case, the n e m u ric val e u p s eci i f ed r p e r e s e nts the e b m u n r f o el m e e s t n in h t e arr y a ,
o n t t e h va e u l of t e h up r e p bound. If h t r e e is a mismatch be w t e e n h t e declared s z i e an d
the e b m u n r of initial- z i er , s u o y r a e s i s d e u a c p m o i e l time e r r or .

e D pt. f o T I B J S , E S I e g a P 0 7
r f om a c m m o on a b se cl s a s
lo e w r bou d n of e z r , o although it is o p s
n u o b d usi g n t e h static
u p n i t , ) spec f i y t e h
o o b k C n O s OM;
] 0 [ M O C n O s k o o b =
] 1 [ M O C n O s k o o b =
] 2 [ M O C n O s k o o b = AT " L ;
n o e h t fly), as is t e h e n w r o w y e k d. , s u h T e h t g n i w o l l o f
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

s s e l d r a g e R f o w o h u o y e r a l c e d n a , y a r r a e b e r a w a t a h t e h t s t n e m e l e n i a T E N . y a r r a e r a
l l a c i t a m o t u a y set o t th r i e r p s e ective e d fa l u t val s e u un i t l you i d n c i ate otherw s i e. Thus, i f
o y u have an array of nu e m r c i al , s e p y t a e ch m b m e r e is t e s to 0 (or 0.0 in h t e case f o
g n i t a o l f - t n i o p , ) s r e b m u n s t c e j b o e r a t e s o t , l l u n d n a n a e l o o B s e p y t e r a t e s o t e s l a f .

r r A a s y s A Para e m e t s r (a d n Re u t rn V e u l a s)
e c n O u o y e v a h d e t a e r c n a , y a r r a u o y e r a e e r f o t s s a p t i s a a r e t e m a r a p d n a e v i e c e r t i s a a
a v lue . r o F , e l p m a x e e h t g n i w o l l o f t n i r P A ( y a r r ) method takes an inco n i m g
y a r r a f o s t n i d n a pr s t n i h c a e r e b m e m o t e h t n o c s , e l o wh e l i h t e
i r t S t e G n a r r A g y() = d o h t e m o p s e t a l u p n a y a r r a f o s g n i r t s d n a s n r u t e r t i o t e h t : r e l l a c
c i t a t s d i o v ] [ t n i ( y a r r A t n i r P ) s t n I y m
{
t n i ( r o f i = ; 0 i < ; h t g n e L . s t n I y m ) + + i
m e t I " ( e n i L e t i r W . e l o s n o C } 0 { s i , " } 1 { , i ; ) ] i [ s t n I y m
}

c i t a t s ] [ g n i r t s ) ( y a r r A g n i r t S t e G
{
] [ g n i r t s s g n i r t S e h t = { l l e H " , " o , " m o r f " " y a r r A g n i r t S t e G " ; }
n r u t e r ; s g n i r t S e h t
}
T e h se me h t ods may be inv d e k o from a i a M n() method as o s :
] [ g n i r t s ( n i a M d i o v c i t a t s ) s g r a
{
] [ t n i s e g a = , 0 2 { , 2 2 , 3 2 } 0 ;
; ) s e g a ( y a r r A t n i r P
] [ g n i r t s s r t s = ; ) ( y a r r A g n i r t S t e G
g n i r t s ( h c a e r o f s n i ) s r t s
o C s ( e n i L e t i r W . e l o s n ; )
Con l o s e e R . d a L ( e n i ; )
}

g n i k r o W t i w h i d i t l u M mens o i nal Arrays
n I o i t i d d a n to t e h si g n le-d m i s n e i n o arrays u o y a h ve seen t s u h f r a , C# also supports two
varie e i t s of i t l u m d m i ens o i nal arra s y . e h T i f r t s of s e h t e is t r e e m d a re t c angul r a a r r ay,
h w ich is simply an array of i t l u m ple dim s n e i s n o , h w ere h c a e row is of e h t a s me l g n e th.
o T c e d r a l e a d n f l l i a mu i t l d m i ensi a n o l rect n a gu a l r r a y a r , pr c o eed as follows:
c i t a t s d i o v ] [ g n i r t s ( n i a M
{
. . .
/ / A r a l u g n a t c e r D M . y a r r a
] , [ t n i ; x i r t a M y m
i r t a M y m x = w e n ; ] 6 , 6 [ t n i

/ / e t a l u p o P 6 ( * ) 6 . y a r r a
t n i ( r o f i = ; 0 i < ; 6 ) + + i
t n i ( r o f j = ; 0 j < ; 6 ) + + j

e D pt. f o T I B J S , E S I e g a P 1 7
r e b m e m r n r u t e
r a s g )
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

, i [ x i r t a M y m ] j = i * ; j

/ / t n i r P 6 ( * ) 6 . y a r r a
t n i ( r o f i = ; 0 i < ; 6 ) + + i

{
t n i ( r o f j = ; 0 j < ; 6 ) + + j
, i [ x i r t a M y m ( e t i r W . e l o s n o C ] j + "\ ; ) " t
Con l o s e r W . t i e e n i L ();
}
. . .
}
T e h c e s ond type of u m i t l d m i s n e i a n o l arr y a s i e t rm d e a a j g e g d ar a r y. As the a n me
m i p , s e i l
j g a g d e arr s y a co t n ain o s me u n m e b r of n i ner arrays, each of which may e v a h a n u i e u q
u p p er li t i m , for m a x e p e l :
] [ g n i r t s ( n i a M d i o v c i t a t s ) s g r a
{
. . .
/ / A d e g g a j D M y a r r a , . e . i ( n a y a r r a f o . ) s y a r r a
/ / e r e H e w e v a h n a y a r r a f o 5 t n e r e f f i d . s y a r r a
y a r r A g a J y m ] [ ] [ t n i = w e n ; ] [ ] 5 [ t n i

/ / e t a e r C e h t d e g g a j . y a r r a
r o f t n i ( i = ; 0 i < r A g a J y m ; h t g n e L . y a r ) + + i
] i [ y a r r A g a J y m = w e n i [ t n i + ; ] 7

/ / t n i r P h c a e w o r , r e b m e m e r ( h c a e t n e m e l e s i d e t l u a f e d o t ) ! o r e z
t n i ( r o f i = ; 0 i < ; 5 ) + + i
{
h t g n e L " ( e t i r W . e l o s n o C f o w o r } 0 { s i } 1 { :\ , " t , i ; ) h t g n e L . ] i [ y a r r A g a J y m
t n i ( r o f j = ; 0 j < h t g n e L . ] i [ y a r r A g a J y m ; ) + + j
] j [ ] i [ y a r r A g a J y m ( e t i r W . e l o s n o C " + ; ) "
Con l o s e r W . t i e e n i L ();
}
}
w o N h t at o y u n u der t s and w o h o t build a d n po l u p ate C# arrays, t e l s u t rn our
a t t e n o i t n o t h t e

The Sy t s em. y a r r A s a B e Cla s s
r e v E y .N T E array y u o a e r c e t s i automa c i t l l a y d r e i d e v from S m e t s y A . a r r y. h T is c s a l s
e d f e n i s a num r e b of he p l ful me h t ods that make o w rking i w th a a r r ys mu h c more
pala a t ble. Ta e l b 3- 4 1 g e v i s a u r n n w o d of s m o e (but not all) of h t e more int r e e i t s n g
members.

Ta l b e 3- 4 1 . l e S ect e M mbers of y S s m e t A . r a r y
r e b m e M n i n a e M g n i Lif e

e D pt. f o T I B J S , E S I e g a P 2 7
m i t l u a e t a b se c s a l s of any a r r : y a y S s . m e t A r r ay.
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

Bin y r a S r a e ( h c ) This st t a ic m t e hod s a e rches a ( e r p vi u o sly so t r d e ) a r r ay f r o a
giv n e item.
f I h t e a r r ay is composed of custom t p y s e you have created, the type i n
n o i t s e u q t s u m p m i t n e m e l e h t ICo r a p m er in e t rface (see Chap e t r 7) to
n e g e g a in a binary a e s rch.
Cle ( r a ) This sta i t c method sets a r g n a e of elem n e ts in t e h a r r y a o t
empty a v l s e u
(0 for va u l e t p y es; null for refe e r nce ty e p s).
Cop ( o T y ) This me h t od is us d e o t y p o c elem n e ts from t e h sour e c array
into h t e
de n o i t a n i t s r r a ay.
Len t g h s i h T a e r d- y l n o p o r p e y t r is s u ed to t e d r e mine h t e u n b m er f o
t n e m e l e s
n i n a a r r ay.
n a R k s i h T y t r e p o r p s n r u t e r e h t b m u n er f o m i d e s n i n o s of h t e cu n e r r t
y a r r a .
Rev s r e e() s i h T t s a c i t me d o h t v e r e s e s r e h t t n o c e s t n f o a one-dim n e siona l
a r r a y.
Sor ( t ) s i h T d o h t e m s t r o s a e n o - n e m i d s l a n o i r a y a r f o n i trins c i ty s e p . If
the
l e m e ents in the array l p m i ement the IC r a p m o er inte a f r c , e you a c n a o s l
t r o s y u o r custom types a ( gain, see Chapter 7).


s t e L e e s e m o s f o e h t se b m e m s r e n i act n o i . The following code makes u e s of the st t a ic
) ( e s r e v e R a d n a e l C r() e m t d o h s d n a ( t e h L n e gth ) y t r e p o r p o t p m u p t u o e m o s
n o i t a m r o f n i t u o b a n a y a r r a f o stri s g n named f N t s r i a e m s to h t e con o s le:
/ / e t a e r C e m o s g n i r t s s y a r r a d n a s i c r e x e e e m o s y a r r A . m e t s y S s r e b m e m .
] [ g n i r t s ( n i a M d i o v c i t a t s ) s g r a
{
/ / y a r r A f o s g n i r t s .
] [ g n i r t s s e m a N t s r i f = , " e v e t S " { , " c i n i m o D " , " w o l l a w S " } " y d l a B " ;

/ / t n i r P s e m a n s a d e r a l c e d


e h t s i e r e H " ( e n i L e t i r W . e l o s n o C ; ) " : y a r r a
t n i ( r o f i = ; 0 i < s e m a N t s r i f ; h t g n e L . ) + + i
: e m a N " ( e t i r W . e l o s n o C } 0 { \ , " t ; ) ] i [ s e m a N t s r i f
i L e t i r W . e l o s n o C e n ("\n ; ) "

/ / e s r e v e R y a r r a d n a t n i r p .
( e s r e v e R . y a r r A r i f s a N t s e m ; )
s i e r e H " ( e n i L e t i r W . e l o s n o C e h t y a r r a e c n o ; ) " : d e s r e v e r
t n i ( r o f i = ; 0 i < ; h t g n e L . s e m a N t s r i f ) + + i
i r W . e l o s n o C : e m a N " ( e t } 0 { \ , " t ; ) ] i [ s e m a N t s r i f

e D pt. f o T I B J S , E S I e g a P 3 7
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

Con l o s e r W . t i e e n i L ("\n");

/ / r a e l C t u o l l a t u b . y d l a B
t u o d e r a e l C " ( e n i L e t i r W . e l o s n o C l l a t u b y d l a B ... ; ) "
, s e m a N t s r i f ( r a e l C . y a r r A , 1 ; ) 3
t n i ( r o f i = ; 0 i < f ; h t g n e L . s e m a N t s r i ) + + i
o C n e l o s r W . e t i a N " ( m : e { } 0 \t", i f t s r e m a N i [ s ; ) ]
Con l o s e e R . d a L ( e n i ; )
}
3. 7 1 r t S ing a M ni l u p at n o i in # C
T e h C# s n i r t g y e k word is a h s o t r hand nota i t on of h t e e t s y S m i r t S . ng t p y e, i h w ch
pro i v d s e a num e b r
s i l ts so e m (but n t o a l l ) of t e h inte s e r ting membe . s r

Ta l b e 3- 2 1 . l e S ect e M mbers of y S s m e t S . t n i r g
r e b m e M g n i n a e M n i L e f i
Len t g h i h T s p o r p e y t r e r t s n r u h t e l g n e th of the cu n e r r t st i r ng.
Con i a t n ( s ) T s i h me d o h t s i d e s u o t d e n i m r e t e if t e h r u c r t n e s r t i g n ob e j t c
n i a t n o c s
a s e p cified t s r n i g.
For ( t a m ) This sta i t c met d o h is us d e o t form t a a stri g n t i l e a r l s u i g n othe r
pr i t i m i ves
. i ( e , . l a c i r e m u n t a d a and e h t o r t s ri g n s) n a d t e h {0} t o n a i t on exa i m ned earlier
n i s i h t chapter.
Ins ( t r e ) h T s i method is e s u d to rec e v i e a copy of t e h u c rr n e t stri g n that
c n o tai s n
ne l w y i e s n rted str n i g data.
Pad f e L t() T s e h e e m o h t ds e r u t rn c p o e i s f o the u c r r ent str n i g that a h s b e e n
pa d d ed
Pad g i R h ( t ) h t i w s e p cif c i data.
Rem ( e v o ) Use th s e e methods to e r e c i e v a copy of a string, i w th modif c i ation s
Rep c a l e() (c t c a r a h e s r d e v o m e r r o re c a l p e ) d .
Sub r t s i ( g n ) s i h T d o h t e m n r u t e r s a s g n i r t t a h t re t n e s e r p s a s s b u tring of th e
t n e r r u c st i r ng.
ToC r a h A a r r y() T s i h me h t od ret r u s n a cha a r cter array r p e r e e s nt g n i h t e cu r r ent
s i r t ng.
ToU e p p r() These meth d o s create a c p o y of a gi e v n string in p u pe c r ase or
l w o ercase.
ToL e w o r()
Basic String Op r e ations
To u l l i strate so e m a b sic string e p o ra i t n o s, co s n ider h t e follo i w ng M i a n() e m th d o :
c i t a t s d i o v ] [ g n i r t s ( n i a M
{
* * * * * " ( e n i L e t i r W . e l o s n o C n u F h t i w s g n i r t S ; ) " * * * * *
g n i r t s s = , y o B " s i h t is

e D pt. f o I B J S , E S I T
f o s r e b m e m u o y l u o w d e p x e t c r f m o such a t u ility s a l c s. Table 3-12
r a s g )
g n i k a t a g n o l ; " . e m i t

e g a P 4 7
C# d n a m m a r g o r P T E N . i g n

" ( e n i L e t i r W . e l o s n o C - - > s s n i a t n o c : ? ' y o '
" ( e n i L e t i r W . e l o s n o C - - > s s n i a t n o c : ? ' y o B '
Con l o s e r W . t i e e n i L (s.Re c a l p e ' . ' ( ,
Con l o s e r W . t i e e n i L (s.In t r e s (0,
Con l o s e e R . d a L ( e n i ; )
}
o Y u sh u o ld be a r a w e that a t l hough r t s ing is a ref r e e c n e y t e p , the q e ua t i l y operators
(== d n a !=)
whi h c t e h y refer. o f e r e h T re, h t e o f l l w o n i g com r a p i o s n a v e luat s e o t true:
g n i r t s 1 s = o l l e H " ; "
g n i r t s 2 s = o l l e H " ; "
1 s " ( e n i L e t i r W . e l o s n o C = = : 2 s , " } 0 { 1 s = = ; ) 2 s
where s a this o c m r a p i n o s a v e luat s e o t false:
g n i r t s 1 s = o l l e H " ; "
g n i r t s 2 s = ; " ! d l r o W "
1 s " ( e n i L e t i r W . e l o s n o C = = : 2 s , " } 0 { 1 s = = ; ) 2 s
W e h n you wi h s to concat n e a e t x e is i t ng strin s g into a new stri g n t a h t is h t e sum of a l l
C# provides t e h + p o erator as we l l as t e h sta i t c Stri g n .Con a c t() method .
n e v i G i h t s, h t e g n i w o l l o f t s a s t n e m e t a e r u f nc i t o l a n ly equ v i l a n e : t
/ / n o i t a n e t a c n o C f o s g n i r t s .
g n i r t s g n i r t S w e n = s + 1 s + ; 2 s
s " ( e n i L e t i r W . e l o s n o C + 1 s + 2 s = , " } 0 { ; ) g n i r t S w e n
, s ( t a c n o C . g n i r t s " ( e n i L e t i r W . e l o s n o C ) 2 s , 1 s = , " } 0 { c n o C . g n i r t s , s ( t a , 1 s ; ) ) 2 s
n A o e h t r hel f p ul f a e u t re of the st n i r g type is h t e a y t i l i b to t i erate over e h c a i d n ivi u d al
char c a ter
a c c e s s to h t eir n o c t n e ts e k a m s u e of an i d n x e er met o h d. u o Y l l l a e rn w o h o t bu l i d
n i d x e e s r in Ch p a ter 9; howeve , r to i l l u t s r e t a t e h o c n e c pt, t e h fo l l owi g n e d o c prints e c a h
char c a ter of t e h s1 stri g n obj c e t to t e h console:
/ / g n i r t S . m e t s y S o s l a s e n i f e d n a r e x e d n i o t s s e c c a h c a e
/ / r e t c a r a h c n i e h t g n i r t s .
r o f t n i ( k = ; 0 k < ; h t g n e L . 1 s ) + + k
r a h C " ( e n i L e t i r W . e l o s n o C } 0 { s i , " } 1 { , k ; ) ] k [ 1 s
s A n a v i t a n r e t l a e o t in g n i t c a r e t t i w h h t e s e p y t , r e x e d n i e h t str g n i c a l s s n a c l a so e b us d e
the C# r o f e c a h n o c r t s c u t. i G ven t a h t S S . m e t s y t n i r g s i ma t n i a g n i n i an a a r r y o f
i d n i v l a u d i y S s . m e t C a h r , s e p y t e h t fo l l o g n i w c e d o l a so pr s t n i e h c a ch r a t c a er of s1 to the
n o c sole:
r a h c ( h c a e r o f c n i ) 1 s
Con l o s e r W . t i e e n i L ( ) c ;

e p a c s E Ch a r a c s r e t
e k i L in r e h t o C- d e s a b , s e g a u g n a l n i C# t s r n i g li e t r l a s may c i a t n o n var s u o i c s e e p a
, s r e t c a r a h c c i h w h l a u q i y f w o h e h t h c a a r c r e t a t a d d l u o h s e b p i r n d e t to e h t out u p t
t s r m a e . a E ch es a c pe h c aracter b g e ins with a a b ckslash, o f llow d e y b a p s c e i i f c to n e k . n I
e s a c u o y a e r a t i b y t s u r n o e h t a e m n s g n i be n i h d these es p a c e h c r a c a e t rs, T b a le 3- 3 1
lists h t e more common p o i t n o s.
Ta l b e 3- 3 1 . n i r t S g l a r e t i L Escape Ch r a acter s

e D pt. f o J S , E S I T I B e g a P 5 7
I 6 0 S7 1 6

, " } 0 { n o C . s a t o " ( s n i y ; ) ) "
, " } 0 { o C . s t n " ( s n i a B ; ) ) " y o
' ' ! ));
y o B " ' O ; ) ) "
e r a de d e n i f o t compa e r h t e e u l a v w t i h h t e string o e j b cts, o n t t e h mem y r o to
t i s parts,
using an a r r ay i l ke y s n x a t . Forma l l y sp k a e ing, o e j b cts t a h t u s pport ar y a r i l k e
n i h t i w
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

C a h ra t c er g n i n a e M n i L e f i
\' Inserts a sin l g e quote into a string e t i l ral.
\" Inser s t a double quote into a str n i g t i l eral.
\\ Inserts a b k c a sl h s a into a s i r t g n t i l eral. i h T s c n a be u q t i e e h l u f p l
w n e h
e d fining file p t a hs.
\a i r T gg s r e a s t s y m e a e l t r b ( e p e ). r o F c e l o s n o a , s n o i t a c i l p p t s i h n a c be
n a aud o i
clue to t e h s u er.
\n s t r e s n I a new l e n i ( n o n i W 32 l p atform ) s .
\r n I se s t r a ca g a i r r e r t e urn.
\t n I se t r s a l a t n o z i r o h b a t o t n i the g n i r t s t i l e a r l.

o F r example, to print a stri g n t a h t contai s n a tab be w t n e e each word, y u o can m e k a
\t e c s ape h c arac e t : r
/ / l a r e t i L s g n i r t s y a m n i a t n o c y n a r e b m u n f o e p a c s e s r e t c a r a h c .
e H " l o l \ T t h e r e \tA n i a g ; "
Con l o s e r W . t i e e n i L ( ) 3 s ;
As a o n h t er exa l p m e, as u s me y u o wi h s o t create a str n i g t i l e a r l that o c nta n i s quota i t n o
marks, a t o n her that defines a dir c e o t ry path, n a d a final s r t ing literal that inserts
g n i t n i r p e h t r e t c a r a h c a t a d . To do so w t i h u o t o c mpiler r e o r rs,
o y u wou d l e n d e to make use of h t e \",
\\, a d n \n escape characters:
e n o y r e v E " ( e n i L e t i r W . e l o s n o C l s e v o \ o l l e H " W l r o d\ ) " " ;
Con l o s e r W . t i e e n i L ( : C " \\M A y pp\\bin\\ e d b " g u ; )
e t i r W . e l o s n o C e n i L ( l A " l f n i d e h s i .\n\n\ ; ) " n

Wo g n i k r h t i w C# m i t a b r e V n i r t S gs
# C t n i r u d o ces t e h @-pref x i ed stri g n e t i l ral o n ta i t on term d e a v r e bat m i stri g n . Using
e v rba m i t t s ri g n s,
o m st e s u u f l = e h w n o w rking t i w h str n i s g r p e e r senting dire t c ory d n a n t e w r o k p h t a s.
Th , e r o f e r e r e h t a r a h t n n i k a m g use of \\ esc p a e
fol o l i w ng: / / e h T g n i w o l l o f g n i r t s s i d e t n i r p m i t a b r e v
/ / , s u h t l l a e p a c s e s r e t c a r a h c e r a d e y a l p s i d .
o C n e l o s r W . e t i e n i L ( C " @ :\M A y p p \ i b n\ u b e d g ; ) "
Al o s n t o e h t at ver a b i t m str n i gs c n a e b us d e to preser e v white s a p ce f r o strings that
f o l w over mu i t l ple i l nes:
/ / e t i h W e c a p s s i d e v r e s e r p h t i w m i t a b r e v s g n i r t s .
g n i r t s g n i r t S g n o L y m = s i h T " @ s i a e v y r
y r e v
y r e v
g n o l ; " g n i r t s
Con l o s e r W . t i e e n i L ( L y m o g n t S r ) g n i ;
o Y u a c n l a so insert a d u o l b e quote in o t a i l e t r l a stri g n by doubling t e h " token, for
x e ample:


e D pt. f o T I B J S , E S I e g a P 6 7
use of t e h
g n i r t s 3 s =
three bl k n a lines afte r
you i d sable h t e processing of a literals e c s ape h c a a r cters. This can be
c a r a h c e t r , s o y u can s m i ply write the
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

e h T e l o R f o T . m e t s y S e B g n i r t S . t x r e d l i u
Wh l i e e h t s n i r t g c e f r e p s i e p y t t w n e h o y w u i h s t o r p e r s e n e t a b si s c t i r g n a v ri b a l f ( s e ir t s
t i n a c e b t n e i c i f f e n i f i u o y e r a g n i d l i u b a m a r g o r p t a h t s e k a m y v a e h e s u
f o t x e tu = l a a d ta. e h T n o s a e r s a h o t o d h t i w a y r e v t n a t r o p m i t c a f g n i d r a g e r T E N . s : s g n i r t
e h t e u l a v f o a g n i r t s t o n n a c e b d e i f i d o m e c n o d e h s i l b a t s e . # C s g n i r t s e r a . e l b a t u m m i
n O e h t , e c a f r u s i h t s so s d n u l k i e a flat-out i l e, e v i g n h t at we r a e al s y a w s a s g n i n g i n w e
str g n i a v r b a i l s e . Ho e w v , r e if you a x e mine the m t e hods of S S . m e t s y t n i r g, y u o
e c i t o n t a h t e h t s d o h t e m t t a h seem to in e t rnally modify a string in fact re u t rn a
o m dified p o c y of the o i r ginal s i r t g n . o F r x e am
j b o ec , t you r a e not= o m di y f i g n e h t un e d rlyi g n b f u e f r f o an exis i t g n stri g n obj c e t, but
c e r e v i e a new st i r ng object in upper a c se for : m
] [ g n i r t s ( n i a M d i o v c i t a t s ) s g r a
{
. . .
/ / e k a M s e g n a h c o t ? d e x i F r t s ! e p o N
g n i r t S . m e t s y S d e x i F r t s = s i h T " s i w o h I n a g e b ; " e f i l
Con l o s e r W . t i e e n i L ( r t s F x i d e ; )
g n i r t s n o i s r e V r e p p u = ; ) ( r e p p U o T . d e x i F r t s
Con l o s e r W . t i e e n i L ( r t s F x i d e ; )
o C n e l o s r W . e t i e n i L ( 0 { " }\n\n , " p u pe r e V r s n o i ; )
. . .
}
n I a r a l i m i s ve n i , e h w n u o y n g i s s a an e s i x t n i g str g n i ob e j ct o t a new va u l e, u o y hav e
l l a u t c a y
a l l ocat d e a new str g n i in e h t p o r c s e s (the origin l a string ob e j ct i w l l e e v ntually b e
a g rbage l o c e l cted). A s i m i l r a process o c c u s r t i w h stri g n co a c n tena i t n o .
To help d e r uce h t e a u o m nt of stri g n copying, h t e S m e t s y . x e T t m a n e e c a p s defi e n s a
s s a l c na d e m r t S i i u B g n l e d r t s r i f ( se n e u d r g n i r u o exa n i m ation of y S s e t m.O j b ect ear i l er
n i this chapter). i l n U ke t s y S e n i r t S . m g, e d l i u B g n i r t S r pr s e d i v o u o y t c e r i d s s e c c a o t e h t
r e f f u b g n i y l r e d n u . e k i L n i r t S . m e t s y S g, St i r ng u B i d l er v o r p i e d s u n m o r e us me b m e s r that
w o l l a u o y o t appe d n , fo m r t a , i s n e , t r n a d r m e e v o
r F amework 2.0 S K D d c o m u a t n e i t on for f l l u d a t e i s l ).
n e h W u o y cr t a e e a t S r l i u B g n i der , t c e j b o u o y y a m y f i c e p s a i v ( a o c n t s ruc o t r ar t n e m u g )
e h t s r e t c a r a h c f o r e b m u n l a i t i n i e h t j b o ect a c n c i a t n o n. s o d t o n o d u o y f I o t l u a f e d e h t ,
c a p a c t i a f o y t S i r n B g ui d l r e 6 1 s i . n I r e h t i e , e s a c f i u o y d d a r e t c a r a h c e r o m a t a d o t
a r t S i i u B g n l e d r e l b a s i = t i n a h t d l o h o t , t f u b e h f r e is
e l p m a x e f o g n i k r o w wi h t s i h t cla s s p y t e:
g n i s u ; m e t s y S
g n i s u ; t x e T . m e t s y S / / r e d l i u B g n i r t S s e v i l . e r e h

s s a l c p p A g n i r t S
{
c i t a t s d i o v ] [ g n i r t s ( n i a M ) s g r a
{
r e d l i u B g n i r t S r e f f u B y m = w e n y M " ( r e d l i u B g n i r t S g n i r t s ; ) " a t a d
i r W . e l o s n o C y t i c a p a C " ( e n i L e t f o s i h t : r e d l i u B g n i r t S , " } 0 {

e D pt. f o T I B J S , E S I e g a P 7 7
m a n e t e , N S S , . c ),
e u l a v s o t
, e l p when o y u a c ll e p p U o T r() on a stri g n
data from t e h ob e j t c (con u s lt the . E N T
l f e h t n o d e z i s e r y. e r e H is n a
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

myB f f u e C . r p a a y t i c ; )
" ( d n e p p A . r e f f u B y m s n i a t n o c e m o s l a c i r e m u n : a t a d ; ) "
, } 0 { " ( t a m r o F d n e p p A . r e f f u B y m , " . } 1 { , 4 4 ; ) 9 9
y t i c a p a C " ( e n i L e t i r W . e l o s n o C f o s i h t : r e d l i u B g n i r t S , " } 0 {
; ) y t i c a p a C . r e f f u B y m
Con l o s e r W . t i e e n i L ( B y m u f f r e ; )
}
}
, w o N o d e d n u s r tand t a h t n i m y n a cases, S s y t n i r t S . m e g wi l l e b your textual o j b e t c of
h c o e c i . F r o
f o a h c rac e t r a d ta l l i w e b e l b i g i l g e n . Ho e v e w r , i f y a u o r i u b e ld n i a g text-int n e siv e
n o i t a c i l p p a h c u s ( s a a d r o w r o s s e c o r p , ) m a r g o r p u o y l l i w t s o m y l e k i l d n i f t a h t g n i s u
. m e t s y S T S . t x e t B g n i r u e d l i r s e v o r p m i e c n a m r o f r e p .
8 1 . 3 W g n i k r o h t i w E N . T E u n i t a r e m n o s

n I ad i d t n o i o t st u r t c r u e , s u n e m r e i t a n o s (or si p m y l u n e ms) are the other mem e b r of h t e
. T E N e u l a v e p y t y r o g e t a c . n e h W u o y d l i u b a pr g o r , m a it is oft n e c v n o e n e i n t to r c eate a
t e s of b m y s o i l c e m a n s r o f d n u erlyi g n numeri l a c va e u l s. r o F m a x e p e l , if u o y ar e
g n i t a e r c n a m e p e e y o l l l o r y a p s t s y m e , o y u m y a
e g a n a M r, n u r G t, a r t n o C c r o t , and VP r e h t a r n a h t e l p m i s l a c i r e m u n s e u l a v h c u s s a , 0 { , 1
, 2 } 3 . # C su s t r o p p h t e t o n i n o f o = c o t s u m en m u e a r tions f r o th s i very a e r son. F r o
x e m a ple, e h re is h t e p m E T p y e n e umera i t : n o
/ / A m o t s u c n o i t a r e m u n e .
m u n e e p y T p m E
{
Man r e g a ,
Gru t n ,
Con a r t c r o t ,
P V
}
The p y T p m E e r e m u n e t a n o i f e d i s e n r u o f na d e m const n a s t cor e r s o p d n i g n to sp c e ifi c
e m u n ric l a
, t l u a f e d d e w o l l o f y b a n n + 1 o r p gress o i n. You e r a e e r f o t e g n a h c th s i r o i v a h e b as u o y

n i g e B g n i r e b m u n t a . 2 0 1
m u n e e p y T p m E
{
r e g a n a M = , 2 0 1
Gru t n , / / = 3 0 1
Con a r t c r o t , / / 4 0 1 =
VP / / = 5 0 1
}
s n o i t a r e m u n E o d t o n y l i r a s s e c e n d e e n o t w o l l o f a u q e s e t n i l a r e d r o . f I f ( or m o s e
g d o o sen e s o t s e t b a i l h s o y ur Em y T p pe s a o f l l ows, h t e c l i p m o er
n o c n i t ues o t e b h p a py:

e D pt. f o T I B J S , E S I e g a P 8 7
mo t s a p p i l c i t a ons, the e v o r e h ad associated with returning o m di e i f d copi s e
i w sh to u e s the c n o sta t n s
/ / = 0
/ / = 1
/ / 2 =
/ / = 3
s e u l a v . In , # C e h t n i r e b m u n g e m e h c s e s ts e h t f r i st eleme t n o t zero (0) y b
e e s : t i f
/ /
reason) t i made
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

/ / s t n e m e l E f o n a n o i t a r e m u n e d e e n t o n e b ! l a i t n e u q e s
m u n e e p y T p m E
{
r e g a n a M = , 0 1
t n u r G = , 1
r o t c a r t n o C = , 0 0 1
P V = 9
}
U r e d n t e h d o o h , h t e sto e g a r y t e p u d e s o f r e c a h t i em in n a enumera i t on m s p a o t
y b t l u a f e d . u o Y e r a o s l a e e r f o t e g n a h c i h t s o t r u o y g n i k i l . r o F , e l p m a x e f i
u o y t n a w o t t e s e h t g n i y l r e d n u e g a r o t s a v lue of p y T p m E e o t be a byte r e h t a r n a h t n a int,
u o y d l u o w i r w e t the f l o l w o ing:
/ / s i h T , e m i t e p y T p m E s p a m o t n a g n i y l r e d n u e t y b .
m u n e e p y T p m E : e t y b
{
r e g a n a M = , 0 1
t n u r G = , 1
o C r o t c a r t n = , 0 0 1
P V = 9
}

e t o N # C r e m u n e a s n o i t e b n a c n i d e n i f e d a l i m i s ar m e n n a r for n a y f o t e h u n me c i r al ty e p s
( t y b e, t y b s e, s o h rt, o h s u rt, int, i u nt, o l g n , or l u o g n ). s i h T n a c e b l u f p l e h if o y u a e r pr g o ra i m m g n
r o f w o l - y r o m e m s e c i v e d h c u s s a t e k c o P s C P r o T E N . - d e l b a n e r a l u l l e c s e n o h p .

c n O e you have es a t b i l e h s d the ra g n e a d n sto g a r e type of your e u n meratio , n you can
l p c a e of so-c l a led magic num e b rs. As u s me you ha e v a cl s a s defining a t s a i t c
fun t c ion, t k a ing p m E Type
c i t a t s d i o v e p y T p m E ( s u n o B r o F k s A ) e
{
swi h c t (e)
{
e s a c o C . e p y T p m E t n r t c a or:
u o Y " ( e n i L e t i r W . e l o s n o C y d a e r l a t e g h g u o n e h s a c . . . ; ) "
bre k a ;
e s a c : t n u r G . e p y T p m E
u o Y " ( e n i L e t i r W . e l o s n o C e v a h t o g o t e b g n i d d i k . . . ; ) "
bre k a ;
e s a c g a n a M . e p y T p m E er:
w o H " ( e n i L e t i r W . e l o s n o C t u o b a k c o t s s n o i t p o ; ) " ? d a e t s n i
bre k a ;
e s a c : P V . e p y T p m E
" ( e n i L e t i r W . e l o s n o C E V Y R , D O O G r i S " ! ; )
bre k a ;
: t l u a f e d ; k a e r b
}
}

e D pt. f o T I B J S , E S I e g a P 9 7
a 2 3 t n I . m e t s y S
e s u them in
as t e h o s le parameter:
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

This met o h d can be in o v ked s a so:
c i t a t s d i o v ] [ g n i r t s ( n i a M
{
/ / e k a M a r o t c a r t n o c . e p y t
e p y T p m E ; d e r f
d e r f = ; r o t c a r t n o C . e p y T p m E
Ask r o F B u n o ( s f ) d e r ;
}

The S s y e t m E . u n m Base Class
The e r e t n i s i t ng thing a o b ut E N . T enumera i t o s n s i that they m i plic t i ly e d ri e v from
Sys m e t . u n E m. This
n a d tra s n form a given n e m u er o i t a n. l b a T e 3-9 t n e m u c o d s e m o s t i e s m f o e t n i re t s , all o f
h c i h w e r a sta i t c.

e l b a T 3-9. S t c e l e t S c i t a Members of t s y S e n E . m um
r e b m e M g n i n a e M n i Lif e
For ( t a m ) C v n o erts a va u l e of a specif e i d en m u erat d e type to t i s
e u q ival n e t stri g n
r p e r e t n e s a i t n o accor i d ng to h t e s e p c f i ied format
Get m a N e() R t e r e i e v s a a n me (or an arr y a n o c ta n i n i g a l l a n m s e ) for t e h
o c nsta t n in
Get m a N e ( s ) e h t d e i f i c e p s n o i t a r e m u n e t t a h s a h e h t spe e i f i c d va u l e
Get d n U e y l r n i g e p y T () Retur s n t e h e d n u n i y l r g data t e p y us d e o t h d l o h t e val s e u for
a giv n e
o i t a r e m u n e n
Get l a V u ( s e ) Retrieves an a r r y a of the values of the cons a t nts in a spe i c fi d e
o i t a r e m u n e n
IsD i f e n ( d e ) Returns an in i d a c o i t n of w e h t r e h a const n a t t i w h a sp c e i f i d e
a v lue
exists n i a s c e p ifi d e e u n m a r e i t n o
Par ( e s ) o C n s t r e v e h t g n i r t s re s e r p e n o i t a t n f o the na e m r o nume i r c
v u l a e of one
r o e r o m d e t a r e m u n e s t n a t s n o c o t n a a v i u q e l t n e d e t a r e m u n e t c e j b o


o Y u can a m ke u e s f o the s a t tic . m u n E F t a m r o () me h t od and t e h same x e act string
o f rm t a ting f g a l s exami d e n a e r i l er in h t e cha t p er u d r g n i our examina n o i t o f
e t s y S m n o C . s l o e . r o F example, y u o may e t x c a r t t e h stri g n name ( y b c e p s ifying G), the
he a x dec m i al value (X), or u n meric val e u (D, F, e c t .) of
e n i f e d s a s c i t a t me o h t d m a n ed ( s e u l a V t e G ). s i h T d o h t e m s n r u t e r n a e c n a t s n i f o
. m e t s y S A a r r y d e n i m a x e ( r e t a l n i s i h t , ) r e t p a h c h t i w h c a e item n i e h t y a r r a g n i d n o p s e r r o c
o t e u l a v / e m a n n
fo l l owing:
] [ g n i r t s ( n i a M d i o v c i t a t s ) s g r a

e D pt. f o T I B J S , E S I e g a P 0 8
r a s g )
base class d f e in s e a num e b r of me h t s d o t a h t a l l w o u o y to inter o r gate
a gi n e v n e um. s y S t u n E . m e m l a so
i c e p s e h t f o s r i a p f e i d enumer o i t a n. T l i o l s u t a r t e s e h t e points, n o p r e d th e
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

{
/ / t n i r P n o i t a m r o f n i r o f e h t e p y T p m E . n o i t a r e m u n e
y a r r A j b o = ; ) ) e p y T p m E ( f o e p y t ( s e u l a V t e G . m u n E
C s i h T " ( e n i L e t i r W . e l o s n o m u n e s a h } 0 { , " . s r e b m e m ; ) h t g n e L . j b o

e p y T p m E ( h c a e r o f e n i ) j b o
{
g n i r t S " ( e t i r W . e l o s n o C : e m a n , " , } 0 {
" ( e t i r W . e l o s n o C : t n i , " , ) } 0 { (
" ( e t i r W . e l o s n o C : x e h ) } 0 { ( \ , " n






na e m is a m e m e b r of h t e cu r r n e t e u n e m a r o i t n. o F r x e a e l p m , s a s m u e o y u
s i w h to w o n k if e h t v l a e u o s r e P s e l a S n is part of the p y T p m E e n e m u eration. o T o d , o s u o y
t s u m n e s d t i h t e p y t e in o i t a m r o f n f o e h t n e u n o i t a r e m c i h w ( h n a c e b e n o d a i v e h t # C
o e p y t f ) r o t a r e p o n a d the g n i r t s n e m a f o e h t e u l a v u o y h s i w
l l i w e b d e n i m a x e n i h c u m r e t a e r g l i a t e d n i r e t p a h C : ) 2 1
] [ g n i r t s ( n i a M d i o v c i t a t s ) s g r a
{
. . .
/ / s e o D e p y T p m E e v a h a n o s r e P s e l a S ? e u l a v
f i ( n i f e D s I . m u n E d e p y t ( e f o E ( m p y T p e , ) " s e l a S P o s r e n ) " )
e w , p e Y " ( e n i L e t i r W . e l o s n o C e v a h s e l a s ; ) " . e l p o e p
e s l e
, o N " ( e n i L e t i r W . e l o s n o C e w a h e v o n s t i f o r p . . " . ; )
}
t I is also p s o sible to generate n a u n e mera i t n o set to t e h cor c e r t value from a stri g n
t i l e a r l vi a
h t e a t s t c i E u n m r a P . s ( e ) me h t od. e v i G n h t at a P r ( e s ) retu s n r a ge e n i r c s y S t O . m e b t c e j ,
u o y l l i w n e e d
/ / : s t n i r P y l l a S " s i a " r e g a n a M
m E e p y T p y l l a s = , ) e p y T p m E ( f o e p y t ( e s r a P . m u n E ) e p y T p m E ( ; ) " r e g a n a M "
y l l a S " ( e n i L e t i r W . e l o s n o C s i a , " } 0 { ; ) ) ( g n i r t S o T . y l l a s
a L st b t u o n t l a e s , t t i is o w t r h pointi g n out that C# e u n e m rations sup o p rt h t e use f o
a v r o i us e p o rators, which e t st aga n i st t e h assi n g d e val e u s, for examp : e l
] [ g n i r t s ( n i a M d i o v c i t a t s ) s g r a
{
. . .
/ / h c i h W f o e s e h t o w t e p y T p m E s e l b a i r a v s a h e h t t s e t a e r g l a c i r e m u n ? e u l a v
e p y T p m E e o J = ; P V . e p y T p m E
e p y T p m E n a r F = ; t n u r G . e p y T p m E

e o J ( f i < ) n a r F
e D pt. f o T I B J S , E S I e g a P 1 8
T . e o t S r g n i ());
m u n E . t a m r o F ( e p y t o ( f p m E T ) e p y ,
u n E m a m r o F . t p y t ( e f o m E ( p e p y T , )
, e ; ) ) " D "
, e ; ) ) " X "
}
}
As you a c n g s e u s, this c d o e block prints out the a n me/va u l e pairs (in dec m i al and
x e h ade m i c al)

Ne , t x s t e l e r o l p x e e h t I e n i f e D s d() e m o h t d. Th s i pr p o erty al o l s w you to de e t rmine f i a
e v i g n t s i r g n
o t y r e u q ( p y t e m r o f n i a n o i t
o t t s a c e h t re n r u t e u l a v i o t n the o c r c e r t en m u ty : e p
C# d n a m a r g o r P T E N . mi g n I 6 0 S 1 6 7

s ' e o J " ( e n i L e t i r W . e l o s n o C e u l a v s i s s e l n a h t s ' n a r F ; ) " . e u l a v
e s l e
s ' n a r F " ( e n i L e t i r W . e l o s n o C e u l a v s i s s e l n a h t s ' e o J ; ) " . e u l a v
}

3. 9 1 e D fin n i g Structures in C #
The nu e m r c i al types of . T E N s p u port a M xVa u l e d n a n i M V u l a e pr p o erti s e t t a h prov d i e
informa




= ; 0 0 0 0 3
n a 6 1 t n I U : s i } 0 { , " ; ) e u l a V x a M . 6 1 t n I U
n a 6 1 t n I U : s i } 0 { , " ; ) e u l a V n i M . 6 1 t n I U
: s i } 0 { , " ; ) 6 1 t n I U y m
m a : a } 0 { , " ; ) ) ( e p y T t e G . 6 1 t n I U y m

/ / w o N n i 6 1 t n I U . m e t s y S d n a h t r o h s , . g . e ( a . ) t r o h s u
t r o h s u 6 1 t n I U r e h t O y m = ; 0 0 0 2 1
. e l o s n o C x a M " ( e n i L e t i r W r o f n a 6 1 t n I U : s i } 0 { , " ; ) e u l a V x a M . t r o h s u
n i M " ( e n i L e t i r W . e l o s n o C r o f n a 6 1 t n I U : s i } 0 { , " ; ) e u l a V n i M . t r o h s u
: s i e u l a V " ( e n i L e t i r W . e l o s n o C } 0 { , " ; ) 6 1 t n I U r e h t O y m
Con l o s e r W . t i e e n i L ("I } 0 , " ; ) ) ( e p y T t e G . 6 1 t n I U r e h t O y m
Con l o s e e R . d a L ( e n i ; )
}
In a n o i t i d d o t h t e n i M V u l a e/ x a M V u l a e p o r p ert e i s, a i g v n e system y t pe a m y de n i f e
u f rther s u eful me b m e s r . For a x e mpl , e t e h Sy . m e t s D l b u o e type a l l ows y u o to obtain t e h
a v lues o f r Eps l i on and
" ( e n i L e t i r W . e l o s n o C -> : n o l i s p E . e l b u o d , " } 0 { ; ) n o l i s p E . e l b u o d
o d l b u . e : y t i n i f n I e v i t i s o P , " } 0 { l b u o d . e o P i t i s v i f n I e ni ) y t ;
o d l b u . e : y t i n i f n I e v i t a g e N , " } 0 { l b u o d . e e N i t a g v i f n I e ni ) y t ;
: e u l a V x a M . e l b u o d , " } 0 { l b u o d . e M a V x a u l e ; )
o d b u i M . e l n : e u l a V " } 0 { o d , u M . e l b i u l a V n e);

Ne t x , consider the Syste o B . m o e l an t a d a type. e k i l n U ( C ++), the only v i l a d i s s a gn t n e m a
# C o o b l n a c take is m o r f the set e u r t { |
. e ( g., 1, 0, 1) to a C# bool,
Given h t is o p in , t t i shou d l be cl a e r that y S s o o B . m e t l a e n s e o d o n t u s pp r o t
aMin u l a V e/ a V x a M lue , t e s y t r e p o r p t u b rat e h r u r T e t S ri g n / t S e s l a F i r g n :
/ / o N e r o m d a c o h B n a e l o o s e p y t n i ! # C
l o o b b = ; 0 / / ! l a g e l l I
l o o b 2 b = - ; 1 / / o s l A ! l a g e l l i
l o o b 3 b = ; e u r t / / o N m e l b o r p .
l o o b 4 b = ; e s l a f / / o N m e l b o r p .
L e t i r W . e l o s n o C e n i ("-> : g n i r t S e s l a F . l o o b {0 " } , b n i r t S e s l a F . l o o g ; )

e D pt. f o T I B J S , E S I e g a P 2 8
tion regarding the ra g n e a g v i en t p y e can sto e r . Assume you have r c eated
some vari b a les of t p y e
Sys m e t . n I U t16 (an d e n g i s n u t r o h s ) and ex r e s i c ed it as f l l o w o s:
c i t a t s d i o v ] [ g n i r t s ( n i a M ) s g r a
{
6 1 t n I U . m e t s y S 6 1 t n I U y m
i L e t i r W . e l o s n o C e n ( x a M "
i L e t i r W . e l o s n o C e n ( n i M "
r o f
r o f
o C n e l o s r W . e t i e n i L ( a V " l e u
i L e t i r W . e l o s n o C e n ( I "
m a : a {
in i f nity values:
e l o s n o C r W . e t i e n i L ("->
e l o s n o C r W . e t i e n i L ("->
e l o s n o C r W . e t i e n i L ("->
" ( e n i L e t i r W . e l o s n o C ->
fa s l e}. Y u o cannot as i s n g mak s e hift v l a ues
w i h ch (to most r p ogr m a mers) is a welc e m o change.
C# d n a r g o r P T E N . m m a i g n I 6 0 S 1 6 7

" ( e n i L e t i r W . e l o s n o C -> : g n i r t S e u r T . l o o b 0 { }", o b n i r t S e u r T . l o g ; )

# C l a u t x e t a t a d s i d e t n e s e r p e r y b e h t c i s n i r t n i # C g n i r t s d n a a h c r a t a d s e p y t . E N . l l A T-
r a w a e langua e g s m p a t x e tual a d ta to the e m a s n u derlyi g n types ( s y S t S . m e tr n i g d n a
Sys m e t . a h C r , ) t o b h of h c i h w a e r
The y S s . m e t C a h r ty e p v o r p i e d s you with a gr a e t deal f o func i t n o a t i l y e b yond h t e a i b i l ty
to h l o d
ods of t s y S e h C . m ar , u o y are b a le to e d e t r n i m e if a v i g n e h c arac e t r
is n m u r e i a c , l alph e b a c i t a , l a p i o nt
e h t : g n i w o l l o f
c i t a t s d i o v ] [ g n i r t s ( n i a M
{
. . .
/ / t s e T e h t h t u r t f o e h t g n i w o l l o f s t n e m e t a t s ...
e l o s n o C r W . e t i e n i L ("->
e l o s n o C r W . e t i e n i L ("->
W . e l o s n o C r e t i e n i L ("->
e l o s n o C r W . e t i e n i L ("->
W . e l o s n o C r e t i e n i L ("->
o l l e H " ( e c a p S e t i h W s I . r a h c , " e r e h T ; ) ) 5
" ( e n i L e t i r W . e l o s n o C -> h c c a p S e t i h W s I . r a e o l l e H ' ( , ' e r e h T 6): {0 , " }
H " ( e c a p S e t i h W s I . r a h c e o l l , " e r e h T ) 6 ; )
r a h c . e L s I e t t O r i g i D r t( ) ' ? ' : {0 " } ,
cha I . r s t e L e t r i D r O g ( t i '? ) ) ' ;
r a h c . u P s I t c n a u o i t n(' ) ' ! : 0 { }",
cha I . r s n u P t c u o i t a n ! ' ( ' ) ) ;
r a h c . u P s I t c n a u o i t n(' ) ' > : 0 { }",
cha I . r s n u P t c u o i t a n > ' ( ' ) ) ;
r a h c . u P s I t c n a u o i t n(' ) ' , : 0 { }",
cha I . r s n u P t c u o i t a n , ' ( ' ) ) ;
. . .
}
s A you can s , e e e h c a of th e s e s a t i t c mem e b s r of m e t s y S . a h C r s a h o w t g n i l l a c : s n o i t n e v n o c
a e l g n i s
char c a ter to t s e t.

g n i n i f e D 0 2 . 3 m o t s u C s e c a p s e m a N
Up to i h t s o p in , t you have n e e b bu l i di g n sma l l test programs l v e er g a n i g x e is i t g n
.N T E i n u r e v se (Sy e t s m in r a p t c i ula ) r . h W en y u o bu d l i y u o r ow n
u c stom = ap i l p a c n o i t , s t i c n a be e v ry e h lpf l u o t gro p u yo r u l e r a d e t t e p y s into u c stom
e m a n s e c a p s. In # C , this s i ac o c m l p is e h d u n i s g the
e m u s s A u o y e r a g n i p o l e v e d a n o i t c e l l o c f o c i r t e m o e g s e s s a l c d e m a n r a u q S e, c r i C le, d n a
v i G en th i e r s a l i m i r t i e i , s you wou d l l k i e to u o r g p h t em l a l t e h t e g o r t n i o a
u o Y e v a h o w t c i s a b s e h c a o r p p a . r i F t s , y u o m y a c o h s o e to
d i f e e n a e ch l c s a s t i w hin a single l i f e ( e p a h s s- c . b i l s l l o f s a ) o s w :
e D pt. f o T I B J S , E S I e g a P 3 8
e d o c i n U e d n u r e h t . d o o h
a sin l g e point of h c arac e t r data (which must be p a l ced be w t e e n single quote ) s .
Us n i g t e h sta i t c meth
f o , n o i t a u t c n u p r o t o n t a h w . o T , e t a r t s u l l i k c e h c t u o
r a s g )
h c a . r s I D t i g i ( ) ' K ' :
h c a . r s I D t i g i ( ) ' 9 ' :
c . r a h I t e L s t ' ( r e 10',
h c a . r s I L e t t e r ) ' p ' ( :
, " } 0 { h c . r a I i g i D s ( t ' ; ) ) ' K
, " } 0 { h c . r a I i g i D s ( t ' ; ) ) ' 9
: ) 1 , " } 0 { , " 0 1 " ( r e t t e L s I . r a h c ; ) ) 1
, " } 0 { c r a h . t e L s I e t ' ( r ; ) ) ' p
o l l e H ' ( e c a p S e t i h W s I . r a h c , ' e r e h T : ) 5 , " } 0 {
W . e l o s n o C r e t i e n i L ("->
W . e l o s n o C r e t i e n i L ("->
W . e l o s n o C r e t i e n i L ("->
W . e l o s n o C r e t i e n i L ("->
h c ar c a ter or a stri g n t i w h a u n m r e i a c l index h t at p s c e ified h t e o p s o i t i n of the
a n m s e pa e c s in t e h
e m a n s a p ce . d r o w y e k
Hexagon.
o m m o c n t s u c om na e m spa . e c
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

/ / s c . b i l s e p a h s




e l c r i C . s s a l c
s s a l c { e l c r i C * / g n i t s e r e t n I s d o h t e m . . . / * }
/ / n o g a x e H s s a l c .
s s a l c { n o g a x e H * /
/ / e r a u q S s s a l c .
{ e r a u q S s s a l c * /


No c i t e w o h the S y M h e p a s na e m s c a p e t c a s as e h t c c n o e a u t p l co t n ai r e n of t e h se t s e p y .
a n r e t l A
p a r w e h t n e v i g c s s a l d f e n i i o i t ns n i the m a s e na e m spac : e
/ / s c . e l c r i c
g n i s u ; m e t s y S

e c a p s e m a n s e p a h S y M
{
/ /








n o g a x e H . s s a l c








e r a u q S . s s a l c

}
s A u o y a e r l a y d , w o n k n e h w r e h t o n a e c a p s e m a n s e h s i w o t e s u s t c e j b o n i h t i w a t c n i t s i d
h t e i s u ng k d r o w y e n a c e b s u ed as f : s w o l l o
/ / e k a M e s u f o s e p y t d e n i f e d e h t e p a h S y M . e c a p s e m a n
g n i s u ; m e t s y S
g n i s u ; s e p a h S y M

e D pt. f o T I B J S , E S I e g a P 4 8
g n i s u ; m e t s y S

e c a p s e m a n s e p a h S y M
{
/ /
e r o M g n i t s e r e t n i s d o h t e m . . . / * }
g n i t s e r e t n i e m d o h t s . . . / * } n e v E e r o m
v i t e , y l u o y n a c t i l p s a e l g n i s name p s a e c into multip e l C# f e l i s. o T o d , o s y l p m i s
e l c r i C . s s a l c
s s a l c { e l c r i C }
}

/ / s c . n o g a x e h
g n i s u ; m e t s y S

e c a p s e m a n s e p a h S y M
{
/ /
s s a l c { n o g a x e H }
}

/ / s c . e r a u q s
g n i s u ; m e t s y S

e c a p s e m a n h S y M s e p a
{
/ /
s s a l c { e r a u q S }
n m a espa e c ,
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7


e c a p s e m a n p p A y M
{
s s a l c r e t s e T e p a h S
{
c i t a t s d i o v ] [ g n i r t s ( n i a M ) s g r a
{
n o g a x e H h = w e n ; ) ( n o g a x e H
e l c r i C c = w e n ; ) ( e l c r i C
e r a u q S s = w e n ; ) ( e r a u q S
}
}
}





































e D pt. f o T I B J S , E S I e g a P 5 8
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

d e d n e m m o c e R qu s e n o i t s
. 1 e f y e k e h t e r a t a h W u t a e r s # C ?
. 2 n i d l i u b c i s a b e h t e r a t a h W g k c o l b s t S l a u s i V k r o w e m a r f T E N . f o u n a o i d d
l p x e a n i .
. 3 i a l p x E n th g n i s c e l e e w t e b e t a i t n e r e f f i D . s e i r a n i b T E N . f o t p e c n o c d n a e l i f e n
n a e l i f e l g n i s m e s s a e l i f i t l u m d l b e i s.
. 4 a d a t e M e p y T T E N . f o e l o r e h t s i t a h W t a M y l b m e s s A d n a a i n t s e f
. 5 i t s i r e t c a r a h c s s a l c S T C e h t e r a t a h W c s

































e D pt. f o T I B J S , E S I e g a P 6 8
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7


N U IT 4
OOP with C#
i n i f e D 1 . 4 n s a l C # C e h t f o g s ,
i t i n i f e D 2 . 4 o l b u P t l u a f e D e h t n i e c a f r e t n I c o y T a f p e
l i P e h t g n i p p a c e R 3 . 4 l r a s o , P O O f
i P t s r i F e h T 4 . 4 l i t a l u s p a c n E s # C : s r a l o v r e S n i u e s P , s e c o d -
n o i t a l u s p a c n E : n i t a e r C g d a e R - l n O y l e i F s d
h n I s # C : r a l l i P d n o c e S e h T 5 . 4 s t r o p p u S e c n a t i r e
. 4 8 , s n o i t i n i f e D e p y T d e t s e N , d r o w y e K d e t c e t o r P e h T : s t e r c e S y l i m a F g n i p e e k
. 4 9 m y l o P s # C : r a l l i P d r i h T e h T o p r h , t r o p p u S c i
e B g n i t s a C 8 . 4 t e e w n.




























e D pt. f o T I B J S , E S I e g a P 7 8
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7


. 4 1 Defition f o h t e # C C s a l s
f I o y u e v a h b n e e d n i o g objec s t in o n a ther prog a r m i m g n l e g a u g n a , u o y are o n b u o d t
a a w e r of t e h role of class d f e in o i t i ns. Forma y l l , a class is n t o hing m e r o th n a a s u c m o t
u e s r-d f e n i ed type (UDT) h t at is co p m osed of field data (so e m i t mes termed me b m er
varia l b s e ) a d n func o i t s n f o ( ten called met d o h s in OO a e p s k) h t at c a t on this a d a t . e h T
t e s f o i f e d l da a t col e l t c v i e y l r p e e r se s t n h t e a t s te of a class instance.
T e h o p wer of b o j c e t-o i r n e t d e l n a g a u ges is t a h t y b gr p u o ing t a d a and u f nc i t a n o y t i l in a
si g n le , T D U you r a e a e l b to model your o s ftware typ s e after r l a e -wo l r d e i t n ties . F r o
ex m a p , e l as u s me you r a e in e t rested n i m e d o ling a generic employee for a payr l l o
system. t A i m n m i u , m u o y y a m s i w h to d l i u b a s s a l c t t a h a m i a t n ins the nam , e r u c rent
pa , y d n a = m e p o l y e e ID for each w r o ker . n I n o i t i d d a ,
, d o h t e m na e m d Gi o B e v n s u (), whi h c incr a e ses an i d n ivi a u d l s cur n e r t p y a by so e m
a o m unt, and n a o h t er, = named l p s i D ay a t S t ( s ) , h c i h w pr t n i s t u o e h t sta e t t a d a f r o th s i
a u d i v i d n i l .
/ / e h T l a i t i n i e e y o l p m E s s a l c . n o i t i n i f e d
s e e y o l p m E e c a p s e m a n
{
c i l b u p s s a l c e e y o l p m E
{
/ / d l e i F . a t a d
e t a v i r p g n i r t s ; e m a N l l u f
e t a v i r p t n i ; D I p m e
e t a v i r p t a o l f ; y a P r r u c

/ / . s r o t c u r t s n o C
c i l b u p { ) ( e e y o l p m E }
g n i r t s ( e e y o l p m E c i l b u p , e m a N l l u f t n i p m e , D I t a o l f ) y a P r r u c
{
e m a N l l u f . s i h t = ; e m a N l l u f
D I p m e . s i h t = ; D I p m e
y a P r r u c . s i h t = ; y a P r r u c
}

/ / p m u B e h t y a p r o f s i h t . e e y o l p m e
d i o v c i l b u p t a o l f ( s u n o B e v i G
{ y a P r r u c = + ; t n u o m a }

/ / w o h S t n e r r u c e t a t s f o s i h t . t c e j b o
c i l b u p d i o v ) ( s t a t S y a l p s i D
{
Con l o s e r W . t i e e n i L ( a N " me:
Con l o s e r W . t i e e n i L ( a P " : y
Con l o s e r W . t i e e n i L ( D I " :
}

e D pt. f o I B J S , E S I T
h t e E p m l e y o e s s a l c e d f e n i s o e n
a n u o m ) t
} 0 { , " f N l l u a ) e m ;
r r u c P ) y a ; } 0 { , "
} 0 { , " m e p D I );


e g a P 8 8
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

}
}
c i l b u p s s a l c e e y o l p m E
{
. . .
c i l b u p { ) ( e e y o l p m E }
. . .
}
e k i L + + C d n a , a v a J f i u o y e s o o h c o t d f e e n i u c s m o t n o c s o t c u r t rs in a l c s a s e d finit n o i ,
e h t t l u a f e d c n o r t s c u tor is y l t n e l i s d e v o m e r . T e r e h for , e if y u o wish to allow the b o e j ct
e s u r to create an n i stance of your class as f l l o o : s w
] [ g n i r t s ( n i a M d i o v c i t a t s ) s g r a
{
/ / s l l a C e h t t l u a f e d . r o t c u r t s n o c
e e y o l p m E e = w e n y o l p m E ; ) ( e e
}
o y u u m st explic t i ly redef n i e the defa t l u c n o structor for your c s a l s ( s a e w ha e v done
h r e e). f I o y u do no , t you w l l i rece v i e a comp l i er r e o r r w e h n creating an ins a t n e c of you r
o t c u r t s n o c r. In n a y case, the fol o l i w g n a M i ( n ) me h t o d
creat s e a few = p m E e y o l e b o e j cts s u ing our u c s-
m o t e e r h t - m u g r a e t n construct r o :
/ / e k a M e m o s e e y o l p m E s t c e j b o .
c i t a t s d i o v ] [ g n i r t s ( n i a M
{
e = w e n , " e o J " ( e e y o l p m E , 0 8 ; ) 0 0 0 0 3
2 e ;
2 e = w e n , " h t e B " ( e e y o l p m E , 1 8 ; ) 0 0 0 0 5
; ) ( e n i L d a e R . e l o s n o C
}

2 . 4 g n i n i f e D e h t c i l b u P t n I e c a f r e f o a s s a l C
O e c n o y u a h ve establ s i hed a clas s s e t n i rnal state a d ta n a d constructor e s , t o y ur n x e t
step is to fl s e h out t e h deta s l i of e h t i l b u p c t n i erf c a e o t e h t s s a l c . e h T m r e t e f e r s r o t e h t
t e s f o r e b m e m t a h t e r a y l t c e r i d a c c e s s l b i e r f om an j b o c e t vari b a le via t e h dot p o erator.
r F om h t e c s a l s b s r e d l i u o p int of view, the public int r e face is a y n t i em decl r a d e in a
the pu i l b c r o w y e k d. Be o y nd field data d n a o c nst c u r to s r , the b u p i l c
inter e c a f of a class m y a e b pop-ulat d e by u n merous mem e b rs, i c n l d u i g n the f l l o o i w g n :
d o h t e M s: Nam d e u i n ts of o w rk h t at model some e b hav o i r of a c a l ss
Pr p o r e i t es: l a n o i t i d a r T r o s s e c c a a d n t u m a r o t n o i t c n u f s in i d s i u g se
s t n a t s n o C / d a e R - y l n o l e i f ds: i F e d l a d a t t a h t can o n t be c a h nged af e t r s a g i s n n e m t
n e v i G t a h t r u o e y o l p m E e r r u c e t n ly e d f n i es two i l b u p c d o h t e m s (Gi e v B ( s u n o ) d n a
( s t a t S y a l p s i D ) , )
we r a e able to int r e act i w th the public int f r e ace as o f llows:
/ / t c a r e t n I h t i w e h t c i l b u p e c a f r e t n i f o e h t e e y o l p m E s s a l c
] [ g n i r t s ( n i a M d i o v c i t a t s ) s g r a

e D pt. f o S , E S I T I B J e g a P 9 8
l c a s s t p y e = using h t e de a f ult
r a s g )
e e y o l p m E
e e y o l p m E
l c a s s usin g
ty e p .
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

{
* * * * * " ( e n i L e t i r W . e l o s n o C e h T e e y o l p m E e p y T t a k r o W * * * * * \ ; ) " n
e = w e n , " e o J " ( e e y o l p m E , 0 8 ; ) 0 0 0 0 3



2 e ;
= w e n , " h t e B " ( e e y o l p m E , 1 8 ; ) 0 0 0 0 5
e2. v i G e n o B s u ( 0 0 0 1 ; )
e2. s i D p y a l t S a ) ( s t ;
Con l o s e e R . d a L ( e n i ; )
}

4.3 Reca g n i p p the P l l i s r a of O O P
l l A t c e j b o - e t n e i r o d l n a s e g a u g c d n e t n o h t i w e r h t e co e r n i r p c p i e l s f o b o ject-o i r e e t n d
, g n i m m a r g o r p o t f en ca e l l d the fa d e m l i p lars f o O O . P
En a c psula o i t n: H w o o d es i h t s l g n a u e g a h d i e n a b o jec t s inte a n r l m i plem n e ta i t ? n o
I h n er t i n a ce: H w o do s e this langu g a e pr m o ote co e d e r use ?
Polym p r o i h sm: w o H s e o d i h t s g a u g n a l e let u o y t e r at r l e a d e t ob e j t c s in a i s mi a l r wa ? y
Before di g g n i g into the y s nt c a i t c e d ta l i s of ea h c p l l i , r a t i is im r o p ta t n you d n u r e st d n a
t e h = basic r l o e of each. , e r o f e r e h T e r e h s i a , k s i r b h g i h - v e l el r d n u o , n w s u j t o t clear f o f
y n a = s b e w b o c u o y may ha e v ac i u q red between p j o r ect deadl n i es.

Encaps l u a i t on
h T e first i p llar of OOP is called s p a c n e l u a i t on. s i h T t i a r t l i o b s w o d n to the langua s e g
t i l i b a y o t hide e c e n n u s y r a s l p m i eme t n a n o i t l i a t e d s from e h t ob t c e j r e s u . r o F , e l p m a x e
na d e m D b a t a a R e s e e d a r h t at a h s w t o me h t ods m a n ed

/ / s e t a l u s p a c n e r e d a e R e s a b a t a D e h t s l i a t e d f o e s a b a t a d n o i t a l u p i n a m .
j b O b d r e d a e R e s a b a t a D = w e n ; ) ( r e d a e R e s a b a t a D

dbO . j b O n e p @ ( " : C \Em o l p y e e . s m ) " f d ;
/ / o D g n i h t e m o s h t i w e s a b a t a d ...
dbO . j b C s o l ( e ; )
T e h fic i t i t ous a D tabaseReader cl s s a h s a n e capsulated the inner de l i a t s of lo a c ti g n ,
, g n i t a l u p i n a m d n a c g n i s o l h t e a d a t f e l i . Ob e j ct users l v o e e a c n s p ulati n o , as
this p r a l l i of P O O e k e s p pro r g am i m ng t k s a s m i s pler. e r e h T is o n ne d e o t o w r r y t u o b a
g n i d n i h e b e h t s e n e c s o t r a c ry t u o e h t k r o w f o
e h t a t a D b e R e s a a e d r s s a l c . l l A u o y do s i e t a e r c n a e c n a t s n i
s e g a s s e m , . g . e ( n e p o e h t e l i f . ) e v i r d C y m n o d e t a c o l f d m . s e e y o l p m E d e m a n
r e h t o n A c e p s a t f o n e c l u s p a a n o i t s i e h t n o i t o n of da a t pr t o t c e ion. , y l l a e d I n a s t c e j b o
t s ate data should e b defined as pri a v te r e h t a r n a h t pu l b ic (as a w s h t e case in e r p i v u o s
chapters). n I h t is , y a w the outs d i e o w r d l u m s a t sk t i l o p ely in d r o er to cha g n e or b o ta n i
the und l r e ying val e u .
In e h ritanc e


e D pt. f o T I B J S , E S I e g a P 0 9
e e y o l p m E
e.G e v i B u n o ( s 2 0 0 );
e.D p s i l S y a a t t ) ( s ;

e e y o l p m E
2 e
e m u s s a u o y r a e us n i g a s a l c s
Ope ( n ) a d n Cl ( e s o ):
loa n i d g,
e h t s u o r e m u n s e n i l f o e d o c t a h t e r a k r o w
n a d d n e s h t e p o r p p a r e t a i
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

The e n xt a l l i p r of , P O O n i h t i r e a c n e, boils down to the a l n u g a e g s ab i l i ty to allow you
to b d l i u e n w c a l s s e d fin t i io s n b s a ed on exis i t ng c s a l s defi t i n io s n . In ess e c n e ,
i h n er t i n a ce a l l ows you to x e t d n e the be a h v o i r of a base (or a p rent) class by ena l b ing a
b u s cla s s to i e h n rit o c re fu c n tiona t i l y (a s l o called a e d i r e v d class or d l i h c l c ass).
o F r example, if u o y are m d o eling an u a tom b o ile, o y u might wish to e p x ress the idea
t a h t
a car h as-a ra i d o. It would be l i l g o c i al to attempt o t e d r v i e the Car s s a l c fr m o a a R dio,
or vice e v s r a.
A ( Car s i -a R i d a o? I n i h t k o n ! t ) Ra e h t r, you v a h e w t o i d n epe d n ent l c a e s s s o w k r ing
toge r e h t , where
u f c n o i t a n t i l y:
c i l b u p c s s a l o i d a R
{
c i l b u p d i o v l o o b ( r e w o P ) n O n r u t
{ o i d a R " ( e n i L e t i r W . e l o s n o C : n o , " } 0 { } ; ) n O n r u t
}

c i l b u p s s a l c r a C
{
/ / r a C s a h " - " a . o i d a R
e t a v i r p o i d a R o i d a R y m = w e n ; ) ( o i d a R

d i o v c i l b u p l o o b ( o i d a R n O n r u T ) f f O n o
{
/ / e t a g e l e D o t r e n n i . t c e j b o
myR i d a o o P . e w r O n o ( f ) f ;
}
}
He e r , the containi g n t e p y ( a C r) is responsible for c a e r i t g n h t e n o c tain d e o j b c e t (Radio).
If the a C r i w s e h s to make the d a R io s i v a h e b r o acce s s l b i e r f m o a a C r t s n i a , e c n t i t s u m
d n e t x e t i s own p b u
t n o c a d e n i e p y t . c i t o N e t a h t h t e e j b o ct s u er has o n clue that h t e a C r c s s a l is mak n i g s u e
f o n a e n n i r d a R io o j b c e : t
] [ g n i r t s ( n i a M d i o v c i t a t s ) s g r a
{
/ / l l a C s i d r a w r o f o t o i d a R . y l l a n r e t n i
r a C r e p i v = w e n ; ) ( r a C
vip . r e T n r u n O R o i d a ( u r t e);
}
Polymorphism
e h T l a n i f r a l l i p of OOP s i l o p y i h p r o m sm. s i h T r t t i a e r u t p a c s a s e g a u g n a l y t i l i b a to r t e t a
l e r ated b o e j cts t e h e m a s way. h T is t e n e t n of an b o j c e t-ori n e t d e l n a g a u ge a l l ows a base
c a l ss to d f e ine a set of members f ( orma l l y termed the p l o y o m h p r ic in e t rface) o t l l a
e d sc d n e e t n s. A c s s a l s e p y t c i h p r o m y l o p i t n erf c a e is co t s n ru t c d e us n i g any b m u n er of
i v r u t l a or b a s r t act memb r e s. In a nut h s ell, a i v r u t al me b m er a m y e b g n a h c ed (o r
more forma y l l sp a e king, overri d d en) y b a derived clas , s whereas an a s b t a r t c m h t e o d

e D pt. f o T I B J S , E S I e g a P 1 9
h t e conta i n i g n class cr a e tes n a d e o p x s s e the contai e n d cla s s s
i l c t n i e e c a f r h t i w m o s e set f o f n u c i t o s n h t at p o e t a r e on h t e
C# d n a P T E N . m m a r g o r i g n I 6 0 S 1 6 7

m t s u be overri e d n by a deri d e v t p y e. h W en der e v i d p y t es override the e m mbers
def n i d e y b a ba e s c a l s s , th y e are esse t n ia y l l red f e ining o h w t e h y d n o p s e r to t e h a s me
r u q e est.

4.4 h T e i F s r t P l i l r a : C# s En a c psula i t on Servi e c s
e h T co c n t p e f o e p a c n s n o i t a l u revo s e v l d n u o r a e h t n o i t o n h t at an s t c e j b o i f e d l t a d a
d l u o h s t o n be direc y l t a c c e s s ible from t e h pu i l b c t n i erface. Ra r e h t , f i an b o ject user
i w s e h s to al e t r h t e state of an b o jec , t t i e o d s o s n i dir c e l t y using accessor e g ( t) and
mutator e s ( t) me h t d o s. In # C , en a c p u s la o i t n s i
pub i l c, r p i t a v e, o r p t c e ted, and t c e t o r p ed t n i e a n r l key
3. o T e t a r t s u l l i e h t e n ed r o f c n e a s p u n o i t a l , as m u s e u o y e v a h t a e r c ed e h t fol o l w g n i
s s a l c e d f n i i o i t n:
/ / A s s a l c h t i w a e l g n i s c i l b u p . d l e i f
c i l b u p s s a l c
{
c i l b u p t n i ; s e g a P f O r e b m u n
}
T e h problem t i w h pub i l c fie d l data is that the items have o n b a t i l i y to d n u erst n a d
w e h t e h r
t e h cur e r t n a v l e u o t whi h c t y e h are assi d e n g is a v i l d w t i h g e r ard o t h t e u c rre t n busi e n ss
t s y s em. As you k , w o n t e h up e p r range of a C# n i t is i u q te a l g r e
(2, , 7 4 1 4 3 8 ,647). The e r fore, h t e o c m
/ / m m u H . . .
c i t a t s d i o v ] [ g n i r t s ( n i a M
{
k o o B l e v o N i n i m = w e n ; ) ( k o o B
min o N i v l e . s e g a P f O r e b m u n = ; 0 0 0 0 0 0 0 3
}
A t l g u o h h you o d not o e v rflow the bou a d n ri s e of n a int e g e r data t e p y , it should be
clear that a i m ni-n v o el i w th a p g a e count of 30,000,0 0 0 pa e g s is a b t i n u reas n o b a le n i
the real o w r d l . s A y u o n a c e s e, u p bl c i fie d l s do o n t pr d i v o e a a w y to n e fo c r e t a d a
n o i t a d i l a v ru s e l . f I o y ur system h s a a b s u i s e n s rule th t a states a n i m i- v o n el must b e
be e w t en 1 and 200 a p g , s e y u o r a e at a o l ss to n e f c r o e this progr m m a a c i t a l l y. u a c e B e s
f o i h t s, u p b c i l i f e s d l ty c i p a y l l v a h e o n p c a l e in a pr u d o c n o i t - l e v e l c s a l s def i t i n i n o
p ( u i l b c r a e d- n o ly fields e b i g n t e h x e c p e o i t n). a c n E ps l u a i t n o o r p d i v es a w y a o t
preser e v h t e integr t i y of state data. Ra h t er n a h t n i n i f e d g pu i l b c fiel s d (w i h ch can
s a e ily fost r e data c r o r p u tion), you should e g t in the habit of e d fin n i g pri a v te da a t
d l e i f s, which a e r i d n irec l t y m n a p i ulated by the ca l l er using one of w t o main
tec n h i s e u q :
e n i f e D a r i a p f o t l a n o i t i d a r s e c c a s r o and m t u t a r o e m thods.
e n i f e D a named y t r e p o r p .
h W i v e h c er te n h c i u q e u o y o o h c s , e the o p int is t a h t a we l l - n e caps l u ated class u o h s ld
hide t i s
w a r data and the details of o h w t i p o erates from h t e prying e e y s of t e h outside o w r d l .
This is o t f en e t r d e m l b c a k box pr g o a r m m ing. e h T a e b t u y of this p p a o r c a h is h t at a
class a t u hor is fr e e to ch n a ge o h w a gi e v n met o h d is m i plem n e t d e d n u er the d o o h ,

e D pt. f o T I B J S , E S I e g a P 2 9
enfo c r d e at t e h syntactic level using t e h
o w rds, s a descri d e b in C p a h ter
k o o B
l u r es of h t e
r e l i p l l a o s w e h t fol g n i w o l a s s ig t n e m n :
r a s g )
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

t i w u o h t k a e r b i g n any exis i t ng e d o c m k a i g n s u e of t i (p v o r ided h t at e h t sig t a n u e r f o
e h t d o h t e m re s n i a m c n o stant).

Enforcin g Encapsula i t on Usi g n Tr d a i t i n o al Acce s s r o s a d n Mutators
s t e L n r u t e r o t e h t ex g n i t s i Em l p y o ee s a l c s. If u o y wa t n t e h u o tsi e d world to n i ter c a t t i w h
yo r u private u f l m a N l e a d ta field, tr d a o i t i n i d ctat s e defining an c c a s e r o s t e g ( ) d o h t e m
d n a m t a t u or t e s ( e m h t od).
/ / l a n o i t i d a r T r o s s e c c a d n a r o t a t u m r o f a t n i o p f o e t a v i r p . a t a d
c i l b u p s s a l c
{
e t a v i r p g n i r t s ; e m a N l l u f
. . .
/ / . r o s s e c c A
i l b u p c g n i r t s F t e G u l l N m a e ) ( { n r u t e r ; e m a N l l u f }

/ / . r o t a t u M
c i l b u p d i o v F t e S u a N l l me g n i r t s ( ) n
{
/ / e v o m e R y n a l a g e l l i s r e t c a r a h c , ! ( , @ , # , $ , ) %
/ / k c e h c m u m i x a m h t g n e l r o ( e s a c ) s e l u r e r o f e b g n i k a m . t n e m n g i s s a
e m a N l l u f
}
}
e d n U r a t s n , d f o u o c r , e s h t at e h t l i p m o c r e c u o ld o n t c e r a l s e s h w at you l a c l u o y r c a ce s s or
a d n
u m t r o t a m t e ho s d . i G v n e the fact h t at Ge u F t l l N ( e m a ) d n a F t e S u m a N l l e() l u s p a c n e a e t a
e t a v i r p g n i r t s named ful m a N l e, i h t s choice of method a n e m s seems to f t i h t e b l i l. h T e
cal g n i l lo i g c s i as f l o o l s w :
/ / r o t a t u m / r o s s e c c A e g a s u .
c i t a t s d i o v ] [ g n i r t s ( n i a M ) s g r a
{
e e y o l p m E p = w e n ; ) ( e e y o l p m E
p.S u F t e ll a N me e r F " ( d F ; ) " e n o t s t n i l
e e y o l p m E " ( e n i L e t i r W . e l o s n o C s i : d e m a n , " } 0 { . p e G tF l l u N m a e ) ( );
Con l o s e e R . d a L ( e n i ; )
}

n A o r e h t m r o F f o l u s p a c n E a o i t n: Class P i t r e p o r es
n I c t s a r t n o o t r t a l a n o i t i d r o s s e c c a d n a t u m t a or t e m h d o s, . E N T a l n a u g g s e r p efer t o
c r o f n e e p a c n e sula i t n o using o r p pe t r ies, which s u m i late u p blicly acc s e sible points of
data. R h t a er than requiring t e h s u er to a c l l two differ n e t methods to e g t and set t e h
state data, h t e user is b a le to ca l l what p a pears
s a sume you have p o r v d i ed a pr e p o rty named ID that wraps the t n i ernal
empID me b m er vari b a le of t e h Em e y o l p e e p y t . The c l l a i g n sy t n ax w u o ld look like thi : s
/ / g n i t t e S / g n i t t e g a s ' n o s r e p D I u o r h t h g y t r e p o r p x a t n y s .
c i t a t s d i o v ] [ g n i r t s ( n i a M

e D pt. f o T I B J S , E S I e g a P 3 9
r o F m a x e ple:
e e y o l p m E
= ; n
to e b a p b u lic field. o T il r t s u l t a e ,
r a s g )
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

{
e e y o l p m E p = w e n ; ) ( e e y o l p m E

/ / t e S e h t . e u l a v
p. D I = ; 1 8

/ / t e G e h t . e u l a v
n o s r e P " ( e n i L e t i r W . e l o s n o C D I : s i } 0 { , " . p ID ; )
Con l o s e e R . d a L e n i ( ; )
}
T e p y pr p o er i t es a w l ays map o t r a e l c a cessor and mutat r o methods u d n er t e h hood.
Th , e r o f e r e as a c a l ss des g i ner y u o are able to perform n a y internal logic nec s e sary
e b fore m k a i g n the value ass g i nme t n e ( g . ., p u percase the a v lue, scr b u the v l a ue for
a n m u erical a v l e u , n a d so on) . e r e H is the # C
y s ntax behind the ID p o r p e , y t r a h t o n er o r p p t r e y ( a P y) h t at e p a c n s l u a e t s h t e
a P r r u c y f e i l , d n a d a i f nal o r p t r e p y ( e m a N ) to enc p a su a l te the N l l u f ame a t a d . t n i o p
/ / n o i t a l u s p a c n E h t i w s e i t r e p o r p .
c i l b u p s s a l c
{
. . .
e t a v i r p t n i ; D I p m e
e t a v i r p t a o l f ; y a P r r u c
e t a v i r p g n i r t s ; e m a N l l u f

/ / y t r e p o r P r o f . D I p m e
c i l b u p t n i
{
t e g { n r u t e r } ; D I p m e
t e s
{
/ / u o Y e r a l l i t s e e r f o t e t a g i t s e v n i d n a ( y l b i s s o p ) m r o f s n a r t
/ / e h t g n i m o c n i e u l a v e r o f e b g n i k a m n a t n e m n g i s s a .
D I p m e = va u l e;
}
}

/ / y t r e p o r P r o f . e m a N l l u f
c i l b u p g n i r t s e m a N
{
t e g n r u t e r { } ; e m a N l l u f
t e s e m a N l l u f { = } ; e u l a v
}

/ / y t r e p o r P r o f . y a P r r u c
c i l b u p t a o l f y a P
{
t e g n r u t e r { } ; y a P r r u c

e D pt. f o T I B J S , E S I e g a P 4 9
l l i egal r a h c acter , s che k c the b n u o ds f o
e e y o l p m E
D I
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

t e s { y a P r r u c = } ; e u l a v
}
}
A C# property is c p m o osed usi g n a get b o l k c (ac s e c sor) and set b o l k c (mutato ) r . h T e
C# va e u l k o t en re r p esen s t the g i r ht-hand i s de of the ass n e m n g i t. The un e d rlyi g n
e d p d n e s n o i h w ch so t r of d a t a it r p e e r s t n e s. In th s i
x e m a ple, h t e ID p o r p e y t r s i p o e i t a r ng on aint a t a d t p y , e whi h c , as o y u reca , l l maps t o
a S s y t I . m e nt32 :
/ / 1 8 s i a , 2 3 t n I . m e t s y S o s " e u l a v " s i a . 2 3 t n I . m e t s y S
e e y o l p m E e = w e n ; ) ( e e y o l p m E
e. D I = ; 1 8
o T e v o r p e h t , t n i o p e m u s s a u o y e v a h u t a d p ed e h t ID pr p o e t r y s t e s l g o ic as follows :
/ / y t r e p o r P r o f e h t D I p m e .
c i l b u p t n i
{
t e g { n r u t e r } ; D I p m e
t e s
{
e u l a v " ( e n i L e t i r W . e l o s n o C s i n a e c n a t s n i : f o } 0 { , " . e u l a v t e G Type( ; ) )
s ' e u l a v " ( e n i L e t i r W . e l o s n o C : e u l a v } 0 { , " ; ) e u l a v

D I p m e = ; e u l a v
}
}

d a e R - d n a y l n O W e t i r - t r e p o r P y l n O s e i
h W en cre t a n i g class types, you may wish to c n o figure a e r ad-only pr e p o r y t . T , o s o d o
m i s p y l l i u b d a p o r perty w t i hout a cor e r s n o p d g n i s t e k c o l b . , e s i w e k i L f i u o y h s i w o t e v a h
t e h e g t bl k c o . W o n e v a h e nee d t f o s o d o o r thi a x e s mple;
how e v e r, e h r e is how t e h m u N y t i r u c e S l a i c o S b r e
: y l n o
c i l b u p s s a l c
{
. . .
/ / w o N s a a d a e r - y l n o . y t r e p o r p
c i l b u p g n i r t s r e b m u N y t i r u c e S l a i c o S { t e g { n r u t e r ; N S S p m e } }
}
i G ven this adjustmen , t the only ma e n n r in wh c i h an emplo e y es US o S cial Se u c rity
nu e b m r can be set is h t r g u o h a cons u r t c r o t g r a ume t n .

4.5 h T e Sec d n o P l l i ar: # C s n I her t i ance S o p p u rt
Now h t at o y u ha e v se n e various t n h c e iques t a h t a l l ow u o y to cr a e te a si g n le we l l -
en p a c sulated cl s a s, it is t m i e to t r u n y u o r atten i t on to bu l i ding a fa i m ly of related
cla e s s s. As m n e i t on d e , n i heri- t n a e c is h t e as e p ct of P O O that fac i l i tates code reuse.
e h n I r t i a c n e comes in o w t fla r o v s: c s a l si a c l a t i r e h n i n e c e h t ( is-a re n o i t a l s ) p i h d n a e h t

e D pt. f o T I B J S , E S I e g a P 5 9
da a t t p y e of t e h value t k o e n
D I
e t i r w a - t r e p o r p y l n o y t i m o ,
p o r p e y t r c d l u o e b d e t t i f o r t e r as read-
e e y o l p m E
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

d / t n e m n i a t n o c n o i t a g e l e l e d o m e h t ( s a h - a ) p i h s n o i t a l e r . s t e L n i g e b y b g n i n i m a x e e h t
l a c i s s a l c s i - a l e d o m .
h W en u o y t s e a h s i l b s i - a l e r a o i t nships betwe n e classes, u o y a e r u b i n i d l g a
de n e p dency
be w t een y t pes. h T e basic idea b h e i d n classi a c l inh r e t i an e c is h t at new classes m y a
lever e g a ( d n a po s s l i b i y exte d n ) h t e f n u c i t ona i l ty of oth r e cla s e s s . o T u l l i s r t ate, as m u s e
t e h f c n u o i t a n t i l y of t e h E o l p m yee c s s a l to r c ea e t w t o new

F a x e r u o r o mple , w w e ill u s s a m e t a h t t e h n a M a e g r e s s a l c xt n e s d m E plo e y e b y r e h t g n i d r o c e
n r e b m u f o c o t s k op i t o , s n wh e l i e h t l a S e e P s s r on c s s a l s n i a t n i a m h t e nu b m er of sa s e l . In
# C , e t x e d n ing a cl s a s s i acc l p m o ished n i s u g t e h co o l n opera o t r (:) on the l c ass
de i f i n t o i n. Th s i being a s d i , here are t e h
/ / d d A o w t w e n s e s s a l c b u s o t e h t s e e y o l p m E a n e c a p s e m .
s e e y o l p m E e c a p s e m a n
{
c i l b u p s s a l c r e g a n a M : e e y o l p m E
{
/ / s r e g a n a M d e e n o t w o n k r i e h t r e b m u n f o k c o t s . s n o i t p o
g n o l u e t a v i r p ; s n o i t p O f O r e b m u n
c i l b u p g n o l u s t p O b m u N
{
t e g { n r u t e r } ; s n o i t p O f O r e b m u n
t e s { s n o i t p O f O r e b m u n }
}
}

c i l b u p s s a l c S n o s r e P s e l a : e e y o l p m E
{
/ / e l p o e p s e l a S d e e n o t w o n k r i e h t r e b m u n f o . s e l a s
e t a v i r p t n i ; s e l a S f O r e b m u n
c i l b u p t n i s e l a S b m u N
{
t e g { n r u t e r } ; s e l a S f O r e b m u n
t e s { s e l a S f O r e b m u n = ; e u l a v }
}
}
}
s i n a d e h s i l b a t s e e v a h u o y t a h t w o N -a i h s n o i t a l e r p , Sa P s e l er o s n n a d g a n a M er ha e v
l l a c i t a m o t u a y h n i e t i r ed l l a c i l b u p d n a ( t o r p e ) d e t c me b m ers of the Em o l p e y e b e s a class.
o T : e t a r t s u l l i
/ / e t a e r C a s s a l c b u s d n a s s e c c a e s a b s s a l c . y t i l a n o i t c n u f
c i t a t s d i o v ] [ g n i r t s ( n i a M
{
/ / e k a M a . n o s r e p s e l a s
n o s r e P s e l a S n a t s = w e n ; ) ( n o s r e P s e l a S


e D pt. f o T I B J S , E S I e g a P 6 9
th t a you wish to e l e v rage
s s a l c s e .
e d rived s a l c s e p y t s:
= ; e u l a v
r a s g )
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

/ / e s e h T s r e b m e m e r a d e t i r e h n i m o r f e h t e e y o l p m E e s a b . s s a l c
D I . n a t s = ; 0 0 1
e m a N . n a t s = ; " n a t S "

/ / s i h T s i d e n i f e d y b e h t n o s r e P s e l a S . s s a l c
s e l a S b m u N . n a t s = ; 2 4
Con l o s e e R . d a L ( e n i ; )
}
o D e b w a are that n i heritance r p e e s rves encapsul t a o i n. Therefore, a deri d e v class
cann t o
dir c e l t y a c c e s s t e h private mem e b s r d f e ined by t i s b e s a cl s a s.

Control i l ng Base Class C e r a i t on with base
, y l t n e r r u C e l a S sP o s r e n d n a a n a M ger can y l n o e b c e r t a ed using a e d f t l u a n o c s c u r t t r o .
h t i W th s i n i m n i , d as u s me you have added a new s x i -ar u g ment n o c struc o t r to the
g a n a M er ty , e p wh c i h is inv e k o d
c i t a t s d i o v ] [ g n i r t s ( n i a M ) s g r a
{
/ / e m u s s A e w w o n e v a h e h t g n i w o l l o f r o t c u r t s n o c .
/ / , e m a n ( , e g a , D I , y a p , N S S r e b m u n f o k c o t s . ) s n o i t p o
y k c u h c r e g a n a M = w e n , 5 3 , " y k c u h C " ( r e g a n a M , 2 9 , 0 0 0 0 0 1 3 3 3 " - 3 2 - , " 2 2 3 2 ; ) 0 0 0 9
}
If you look at t e h g r a m u e t n i l s , t you can clea l r y see t a h t m s o t of t e h se parameters
should be
d e r o t s n i e h t me r e b m s e l b a i r a v de d e n i f y b e h t p m E l e y o e ba e s c s a l s. To o d s , o u o y coul d
= m i plement
/ / f I u o y o d t o n y a s , e s i w r e h t o a s s a l c b u s r o t c u r t s n o c y l l a c i t a m o t u a s l l a c e h t
/ / t l u a f e d r o t c u r t s n o c f o s t i e s a b s s a l c .
g n i r t s ( r e g a n a M c i l b u p , e m a N l l u f t n i , e g a t n i , D I p m e
t a o l f , y a P r r u c g n i r t s , n s s o l u g n ) s t p O f O b m u n
{
/ / s i h T t n i o p f o a t a d s g n o l e b h t i w ! s u
s n o i t p O f O r e b m u n ; s t p O f O b m u n =

/ / e g a r e v e L e h t s u o i r a v s r e b m e m d e t i r e h n i m o r f e e y o l p m E
/ / o t n g i s s a e h t e t a t s a t a d .
D I = ; D I p m e
e g A
e m a N = ; e m a N l l u f
r e b m u N y t i r u c e S l a i c o S = ; n s s
y a P = ; y a P r r u c
}

6 . 4 i p e e K ng Fa i m y l c e S r : s t e h T e t o r p e d e t c r o w y e K d
As u o y alr a e dy n k o , w u p b i l c t i ems are direc l t y ac e c ssible r f om anywh r e e, while private
t i ems c n n a ot be acc s e sed f o r m any b o j c e t be n o y d h t e class that has de i f ned it. C#

e D pt. f o T I B J S , E S I e g a P 7 9
as f l l o ows:
this new n o c struc o t r as follow : s
= ; e g a
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

t s e k a e h t l d a e of many ot r e h o m e d rn b o ject l g n a u s e g a d n a provides an add i t i a n o l l e v e l
of a c c e s s ib t i l i y: prot c e t d e . h W en a b e s a cl s s a n i f e d s e pr t o c e ted data r o pr e t o cted
t i ems that can be accessed directly y b a y n
e d sc n e dent. If y u o wish o t o l l a w the S l a esPer o s n and Ma a n ger ch l i d c s a l s s e to i d rectl y
s e c c a s the data sector e d f n i ed by E o l p m yee, y u o n a c update t e h orig n i al p m E l e y o e cl s a s
n o i t i n i f e d s a : s w o l l o f
/ / d e t c e t o r P e t a t s . a t a d
c i l b u p s s a l c
{
/ / d l i h C s e s s a l c n a c t c e r i d y l s s e c c a s i h t n o i t a m r o f n i . t c e j b O s r e s u . t o n n a c
d e t c e t o r p g n i r t s ; e m a N l l u f
d e t c e t o r p t n i ; D I p m e
d e t c e t o r p t a o l f ; y a P r r u c
d e t c e t o r p g n i r t s ; N S S p m e
d e t c e t o r p t n i
. . .
}
T e h n e b efit of def i n i g n prot c e t d e me b m ers n i a base class is h t at deri d e v y t s e p o n
l g n o er have o t acc s s e the data using p b u i l c me o h t ds r o properties. The p s s o ible
o d wnfall, of cour , e s is h t at h w n e a der v i d e type has i d rect acce s s to its a p r n e t s
in e t rnal data, t i is e v y r possible to acci e d l l a t n y a p y b ss exis i t g n business u r les o f und
t i w h n i pub i l c p p o r erties s ( u h c as the i m ni- e v o n l t a h t ex e e c ds h t e page c u o nt). e h W n
u o y define pr t o c e ted mem e b rs, u o y are crea n i t g a l e v e l of trust be w t n e e t e h p r a ent and
l i h c d clas , s as t e h o c mpi e l r l l i w not c t a ch a y n viola i t on of y u o r typ s e u b sines s
u r l s e . l a n i F l , y r e d n u sta d n t a h t s a r a f s a e h t t c e j b o s u er s i c c n o e n r ed, pr e t o c d e t data is
g e r a d r ed as v i r p a e t s a ( e h t r e s u s i o uts e d i of h t e fam ) y l i . e h T e r for , e h t e f l l o i w o g n is
l l i e l a g :
] [ g n i r t s ( n i a M d i o v c i t a t s ) s g r a
{
/ / ! r o r r E
e e y o l p m E p m e = w e n m E ; ) ( e e y o l p
N S S p m e . p m e = 1 1 1 " - 1 1 - ; " 1 1 1 1
}

4.7 h T e Third l l i P r a : C#s P l o m y or i h p c Sup o p rt
s t e L w o n e n i m a x e e h t l a n i f r a l l i p f o O O P: po i h p r o m y l sm. l l a c e R h t at h t e E o l p m yee base
clas s
def n i ed a method named Giv n o B e u ( s ), wh h c i s a w e m e l p m i n d e t as f l l o ow : s
// e v i G s u n o b o t s e e y o l p m e .
c i l b u p s s a l c
{
. . .
d i o v c i l b u p t a o l f ( s u n o B e v i G ) t n u o m a
{ y a P r r u c = + ; t n u o m a }
}


e D pt. f o T I B J S , E S I e g a P 8 9
member , s t i is a l b e to cr a e te a e s t of
e e y o l p m E
A p m e ge;
t ' n a C s s e c c a d e t c e t o r p a t a d m o r f t c e j b o . e c n a t s n i
e e y o l p m E
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

B a c e use this method has e e b n defi d e n as b u p i l c, you can o n w gi e v n o b u e s s to
salesp p o e le and ma g a n ers (as we l l as part- m i t e sal s e people) :
c i t a t s d i o v ] [ g n i r t s ( n i a M
{
/ / e v i G h c a e e e y o l p m e a . s u n o b
y k c u h c r e g a n a M = w e n , 0 5 , " y k c u h C " ( r e g a n a M , 2 9 , 0 0 0 0 0 1 3 3 3 " - 3 2 - , " 2 2 3 2 ; ) 0 0 0 9
chu y k c . v i G B e o u n s( ) 0 0 3 ;
chu y k c . s i D l p a a t S y t ) ( s ;

n o s r e P s e l a S f n a r = w e n , " n a r F " ( n o s r e P s e l a S , 3 4 , 3 9 , 0 0 0 3 2 3 9 " - 2 3 - , " 2 3 2 3 ; ) 1 3
fran.Gi B e v u n o s( 0 2 ) 0 ;
fra D . n i l p s y a S s t a t ();
; ) ( e n i L d a e R . e l o s n o C
}
e h T r p o l b em h t i w e h t t n e r r u c e d s n g i is h t at t e h n i heri e t d Gi n o B e v us() e m o h t d
ca l l y for a l l subcla s s es. , y l l a e d I h t e bo u n s f o a sa s r e p s e l on or t r a p -ti e m
p s e l a s e n o s r d l u o h s ta e k n i to c a cou t n e h t nu b m er f o s l a es . e P rh s p a m n a age s r u o h s ld
a g in a d d i t i onal sto k c p o i t o s n in c n o u j nc i t on i w th a m e n o tary u b mp n i sa y r a l . i G e v n
t i h s, u o y are n e d d u s ly faced t i w h n a inter s e i t g n ques i t n o : How c n a related o j b c e ts
e r d n o p s dif e f re l t n y to t e h a s me r s e u q e t?
4.8 s a C ting Betwee . n
Next u , p you need to learn the laws of C# a c t s n i g oper t a io s n . R c e l l a t e h
Emp y o l ees i h era c r hy and the fact t a h t t e h t p o m s o t class n i the y s stem is t s y S em b O . j c e t.
Th o f e r e r , e e r e v ything s i - a obje t c and
l a g e l o t e r o t s n a e c n a t s n i of a y n p y t e i w i h t n a e j b o ct a v ri b a le:
/ / A r e g a n a M s i " - " a t c e j b O . m e t s y S .
t c e j b o k n a r f = w e n k n a r F " ( r e g a n a M , " a p p a Z , 9 , 0 0 0 0 4 1 1 1 " - 1 1 - , " 1 1 1 1 ; ) 5
In t e h E o l p m y e e s t s y s em, a n a M g r e s, S e l a sP s r e on, and T P S s e l a Pe o s r n e p y t s l l a x e d n e t
so e w can store any of t e h se objec s t in a va i l d a b se cl s a s re r e f n e e c . e r e h T fore ,
the o f n i w o l l g statem n e ts are also legal:
/ / A r e g a n a M s i " - " a e e y o l p m E o o t .
e e y o l p m E t i n U n o o m = w e n t i n U n o o M " ( r e g a n a M , " a p p a Z , 2 , 0 0 0 0 2 1 0 1 " - 1 1 - , " 1 2 3 1 ; ) 1

/ / A n o s r e P s e l a S T P s i " - " a . n o s r e P s e l a S
n o s r e P s e l a S l l i j = w e n , " l l i J " ( n o s r e P s e l a S T P , 4 3 8 , 0 0 0 0 0 1 1 1 1 " - 2 1 - , " 9 1 1 1 ; ) 0 9
e h T t s r i f law f o i t s a c ng be e w t n e cl s a s e p y t s is h t at h w n e two s e s s a l c r a e e r e t a l d by an
s i -a
l e r a i t ons i h p, it is always s f a e to s o t re a deriv d e type i w thin a base class reference.
o F rma y l l , th s i s i called an m i l p icit cast, s a t i t s u j s k r o w n e v i g e h t a l ws of h n i i r e tance.
g n i m m a r g o r p s t c u r t s n o c . r o F m a x e ple, if u o y have a l c s a s
m a n ed a M e h T c n i h e h t t a s s t r o p p u the f l o
u p c i l b s s a l c e n i h c a M e h T
{
c i l b u p c i t a t s d i o v ( n o s r e P s i h T e r i F e e y o l p m E e)
{
/ / e v o m e R m o r f e s a b a t a d . . .

e D pt. f o T I B J S , E S I e g a P 9 9
r a s g )
s e t a r e p o i t n e d i
c n a be treated as s c u h. v i G en i h t s , t c a f it s i
Emp e y o l e,
This le d a s to o s me po e w r u f l
lo i w ng sta i t c method:
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

/ / t e G y e k d n a l i c n e p r e n e p r a h s m o r f d e r i f e e y o l p m e . . .
}
}
o y u can e e f f ctive y l a p ss n a y descendent from h t e Em e y o l p e class i o t n this method
dir c e y l t , gi n e v t e h
/ / e n i l m a e r t S e h t f f a t s .
; ) t i n U n o o m ( n o s r e P s i h T e r i F . e n i h c a M e h T / / " t i n U n o o m " s a w d e r a l c e d s a n a
Emp y o l ee.
The c a M h e n i F . i h T e r i e P s r o s ( n j l l i ); / / " l l i j " s a w d e r a l c e d s a a . n o s r e P s e l a S
e h T g n i w o l l o f e d o c co s e l i p m g n e v i e h t m i p i l i c t c s a t r f m o e h t a b se c s a l s p y t e ( l p m E o e y e)
to the
stor d e in a g n e er c i t s y S e . m b O j c e t ? ) e c n e r e f e r f I u o y p s a s h t e n a r f k e j b o ct i d rectly n i to
The c a M h e n i F . i h T e r i e P s r o s n() as
/ / A r e g a n a M s i " - " a , t c e j b o t u b . . .
c e j b o t k n a r f = w e n k n a r F " ( r e g a n a M , " a p p a Z , 9 , 0 0 0 0 4 1 1 1 " - 1 1 - , " 1 1 1 1 ; ) 5
. . .
The c a M h e n i F . i h T e r i e P s r o s ( n f k n a r ); / / ! r o r r E
u o y a e r u s s i ed a c i p m o ler r r e r o . h T e a e r s n o s i y u o ca o n n t a o t u m i t a cal y l treat a
e t s y S m j b O . ect as a d r e ived E e y o l p m e r i d e y l t c , i g v n e that c e j b O t s i - t o n - a E y o l p m ee. As
o y u can s e e , ho e w v , r e the object refer n e e c is o p in n i t g to n a m E p e y o l e- o c p m a b i t e l
j b o ect. o Y u n a c f s i t a s y h t e co p m i e l r by p f r e g n i m r o
n I C#, explicit casts are e d o n ted by p a l cing pa e r ntheses around the t p y e you w s i h to
a c st to, fo o l l wed y b h t e obj c e t u o y are a t t emp n i t g to cast f o r m. r o F : e l p m a x e
/ / t s a C m o r f e h t c i r e n e g t c e j b O . m e t s y S o t n i a y l g n o r t s
/ / d e p y t r e g a n a M .
r e g a n a M r g m = ; k n a r f ) r e g a n a M (
s ' k n a r F " ( e n i L e t i r W . e l o s n o C : s n o i t p o , " } 0 { ; ) s t p O b m u N . r g m
f I you w u o ld rath r e o n t de a l c re a speci c i f variable of t ype to cast t , o o y u are able to
e d n o c nse e h t pr u o i v e s d o c e s a f w o l l o s:
/ / n A " e n i l n i " t i c i l p x e t s a c .
o C n e l o s r W . e t i e n i L ( r F " k n a s ' i t p o : s n o 0 { " } , ( ( a M na r e g ) a r f nk N . ) u p O b m ts);
s A a f r s a p s a n i s g the y S s . m e t Ob e j ct r e f e r ence t n i o h t e r i F e s i h T P s r e o ( n ) , d o h t e m e h t
prob e l m
/ / y l t i c i l p x E t s a c t c e j b O . m e t s y S o t n i n a e e y o l p m E .
The c a M h e n i F . i h T e r i e P s r o s ( n ( l p m E oy f ) e e r k n a ; )













e D pt. f o T I B J S , E S I e g a P 0 0 1
s i -a rel o i t a ns p i h :
derived y t e p . e w o H , r e v what if you a s l o a w nt d e to f r i e F a r nk Zappa ( u c r r en y l t
l o f l w o s:
an l p x e i t i c t s a c .
can be rectified as fo l l ows:
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7


t s e u Q d e d n e m m o c e R i n o s:
. 1 r a t a h W m a r g o r P d e t n e i r O t c e j b O f o s r a l l i P e e r h T e m ? g n i
. 2 ? # c n i n o i t a l u s p a c n E n i a l p x E
. 3 i L s r e h n I f o s e p y t s u o i r a v e h t n i a l p x E d n a t t i en . e c




































e D pt. f o T I B J S , E S I e g a P 1 0 1
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7


T I N U - 5
Ex s n o i t p e c a c e j b O d n t e m i t e f i L
e d O 1 . 5 o t , s r o r r E , s g u B d n a Ex , s n o i t p e c
2 . 5 e h T e l o R f o T E N . Ex n o i t p e c d n a H in , g
e h T 3 . 5 S m e t s y . Ex n o i t p e c e s a B s s a l C ,
4 . 5 g n i w o r h T a c i r e n e G , n o i t p e c x E
5 . 5 g n i h c t a C Ex n o i t p e c ,
6 . 5 R L C m e t s y S L l e v e n o i t p e c x E (S m e t s y . S s y t m e Ex , ) n o i t p e c
7 . 5 m o t s u C n o i t a c i l p p A - l e v e L n o i t p e c x E (S e t s y m. S m e t s y , ) n o i t p e c x E
8 . 5 g n i l d n a H e l p i t l u M x E n o i t p e c
9 . 5 e h T i n a F ly , k c o l B e h t t s a L e c n a h C n o i t p e c x E l a c i m a n y D ly
g n i y f i t n e d I n o i t a c i l p p A d n a S m e t s y l e v e L i t p e c x E n o g n i g g u b e D S e t s y m
0 1 . 5 Ex n o i t p e c g n i s U S V . , T E N
1 1 . 5 g n i d n a t s r e d n U t c e j b O Li e m i t e f ,
2 1 . 5 e h T C T I f o n w e ,
3 1 . 5 e h T s c i s a B f o e g a b r a G t c e l l o C i n o ,
4 1 . 5 n o i t a z i l a n i F a y T pe, e h T i l a n i F z n o i t a s e c o r P s,
5 1 . 5 g n i d l i u B n a d A c o H t c u r t s e D i n o , d o h t e M
6 1 . 5 e g a b r a G n o i t c e l l o C i m i t p O z t a i , s n o
7 1 . 5 e h T S e t s y m. C G T e p y .



























e D pt. f o T I B J S , E S I e g a P 2 0 1
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7



. 5 1 e d O to r r E ors, u B gs, d n a Ex p e c tion s
e r h T e o c m y l n o m d e s u m o n a aly- e c n c i r t r e t m : s
Bugs: This is, s m i y l p u p , t an error on t e h part of t e h p o r grammer . o F r exa p m le,
a e r prog m a r i m g n t i w h n u ma a n g d e C++. If you make ca l l s n o a L U N L p i o nter
or fa l i o t e d le e t allocated memory r ( e u s l i t ng in a memory leak), y u o have a b g u .
Use r errors: Un i l ke s g u b , user errors r a e t p y i a c l y l c e s u a d by the i d n ividu l a r n n u i g n
your c i l p p a a , n o i t h t a r er th n a by those h w o cr d e t a e t i . For x e ample, an d n e s u er w o h
n e ters a ma f l ormed t s ri g n into a t x e t box c u o ld very we l l ge e n rate an error f i u o y fa l i to
e l d n a h i h t s y t l u a f n i u p t in your c d o e e s a b .
c x E eptions: E c x e i t p n o s r a e typ c i ally reg r a e d d as r n u t m i e anomal e i s h t at are difficu , t l
f i not e l b i s s o p m i , o t a o c c u t n r o f e l i h w p a r g o r mmi g n your a p p l a c i ti n o . Possible
ex p e c i t s n o i c n lude a t t m e pt g n i o t c e n n o ct to a database that no n o l ger ex t s i s, opening
a o c rr e t p u d e l i f , r o c a t n o cti g n a c a m hi e n h t at is cu r r ently o n i l f f e. n I ea h c f o h t ese
a c ses, t e h o r p gra m m er (a d n e d n use ) r h s a l t t i l e c n o tr l o over these ex p e c i t a n o l
circumst c n a es. i G e v n the pr v e ious definiti n o s, it shou d l be cle r a h t at . T E N struc r u t ed
ex e c p o i t n ha d n ling is a t h c e nique we l l s t i u ed o t d a e l w h t i r n u t m i e x e p e c i t ons.
How e v e r, s a o f r the bugs a d n user errors that h e v a c s e aped your view, the C R L w l i l
often e g ne a r te a co r r es n o p di g n exc p e i t on that i n e d ti- fies the b o r p lem at ha d n . T e h
. E N T s a b e cla s s i l brari s e define num r e u o s ex e c p i t ons su h c s a
I d n exO t u O R f a g n e x E ception , Fi e l Not o F u d n E c x e t p ion , Ar u g m n e tOu f O t R n a geE c x e t p ion ,
and so f r o th.

5.2 e h T Role of .N T E Exc t p e ion Ha d n i l ng
Pri r o to N . E , T o r r e r l d n a h n i g u d n er the Windows operating system w s a a confus d e
mi h s m s a h of techni u q s e . Many r p o r g a r e m m s roll d e h t eir own r e ror a h ndling logic
t i w hin the cont x e t of a gi e v n a t a c i l p p ion. r o F x e m a , e l p a de p o l e v e m t n team ma y
e n i f e d a t e s f o m u n e l a c i r co t s n a s t n a h t t re t n e s e r p n k o n w error c n o i d t n o i s, n a d e k a m
e s u f o th m e s a o h t e m d return va u l es. By a w y f o an example, po e d n r the fo l l owing
a p rtial C o c de:
* / A y r e v C- e l y t s r o r r e g n i p p a r t m s i n a h c e m . / *
D N U O F T O N E L I F _ E e n i f e d # 0 0 0 1

t n i i t c n u F e m o S o ) ( n
{
/ / e m u s s A g n i h t e m o s s n e p p a h n i t s i h ) x ( f
/ / t a h t s e s u a c e h t g n i w o l l o f n r u t e r . e u l a v
n r u t e r _ E L I F E F T O N N U O D;
}

d i o v ) ( n i a m
{
t n i l a V t e r = ; ) ( n o i t c n u F e m o S
= = l a V t e r ( f i ) D N U O F T O N E L I F _ E
t o n n a C " ( f t n i r p d n i f e l i f ... ; ) "

e D pt. f o T I B J S , E S I e g a P 3 0 1
a s s ume you
r o F m t a Exc p e t o i n ,
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

}
s i h T p a a o r p ch s i e l ss than id a e , l i g ven t e h fa t c that h t e constant E_ N E L I F O U O F T ND is
little m e r o
w o h o t deal with t e h pr b o e l m. , y l l a e d I u o y d l u o w e k i l o t p a r w h t e name, messag , e
d n a h t o er e h l u f p l n i forma i t on e r gard n i g this err r o c n o d t i ion into a singl , e well-
defined package w ( hich is exactly what happens under structured c x e epti n o
ha g n i l d n ). n I n o i t i d d a o t a s r e p o l e v e d d a c o h , s e u q i n h c e t e h t s w o d n i W I P A s e n i f e d
s d e r d n u h f o r o r r e s e d o c h t at o c e m by w y a of #d s e n i f e , S E R H U T L s, and far too a m y n
a v riations n o h t e m i s ple o B olean ( o b ol, O B OL, A I R A V N O O B _ T L , and so ) n o . , o s l A many
+ + C O C M e v e d lop s r e (and ind r i ectl , y ma y n B V 6 C M O developers) have made use of
a sma l l set of s a t n a d rd COM inte f r ac s e (e.g , . o p p u S I r f n I r o r r E t o ,
IEr r o r I f n o, IC a e r t r E e ro f n I r o ) to e r turn me n a ing u f l error informati n o to a COM
c i l n e t.
symmetry. a E ch a p p r a o ch is more or s e l s tailo e r d to a g v i n e t c e hnol g o y, a give n
lang a u ge, a d n pe h r aps v e n e a given pr t c e j o . n I r e d r o o t t u p n a end to t i h s a m d e n , s s h t e
. T E N l p a m r o f t s e d i v o r p a d n a t s a d r n h c e t ique o t se d n a d n r t ap u r t n e m i erro : s r
u r t s c d e r u t c x e e n o i t p g n i l d n a h S ( H E . )
T e h Atoms f o .NET Ex e c ption Hand i l ng
o r P gramm n i g w t i h structu d e r e p e c x i t on han i l d g n i o v n lves the s u e of four in e t rrelated
n e i t i t es:
A cla s s y t pe t a h t e r pres n e ts the e d ta s l i of the e p e c x i t on that u c c o r r d e
A r e b m e m h t at s w o r h t n a i s n tan e c of h t e x e ce i t p n o class o t the ca e l l r
A bl c o k of c d o e on the call r e s side that in o v kes h t e x e ce t p ion-pr n o e membe r
A block of code on the c l a lers side that will pr c o ess (or a c tch) t e h exc p e i t on s u o h ld t i
oc u c r
T e h C# pr a r g o mming l e g a u g n a s r e f f o four k y e words ( r t y, a c tch, throw, and
i f na l l y) t a h t
l b o r p em at hand is a class der v i d e r f om Syst m e . x E cep i t on r o ( a e d s t n e d n e c r e h t eof) .
e v i G n i h t s , t c a f lets c e h ck u o t e h t r l o e of

. 5 3 The S t s y em. e c x E ption B s a e Class
A l l s u er- and system-defi e n d x e cep i t ons u m i t l ately derive from the
S s y tem E . x e c p i t on base cl s a s (which in u t rn e d r v i es from s y S te b O . m j c e t ). e t o N h t at
m o s e f o e s e h t me b m e s r e r a vir a u t l n a d a m y
c i l b u p s s a l c n o i t p e c x E a i r e S I : n o i t p e c x E _ , e l b a z i l
{
c i l b u p l a u t r i v y r a n o i t c i D I a t a D { ; t e g }
d e t c e t o r p f n I n o i t a z i l a i r e S ( n o i t p e c x E o , o f n i t S e r n i m a g t n o C t x e ; ) t x e t n o c
c i l b u p g n i r t s ( n o i t p e c x E , e g a s s e m n o i t p e c x E ; ) n o i t p e c x E r e n n i
c i l b u p g n i r t s ( n o i t p e c x E ; ) e g a s s e m
c i l b u p ; ) ( n o i t p e c x E
i l b u p c l a u t r i v n o i t p e c x E ; ) ( n o i t p e c x E e s a B t e G
c i l b u p l a u t r i v d i o v o f n I n o i t a z i l a i r e S ( a t a D t c e j b O t e G , o f n i
t S r i m a e C g n t n o t x e t n o c ex ) t ;
e p y T . m e t s y S c i l b u p
d e t c e t o r p t n i

e D pt. f o I B J S , E S I T
h t an a nu e m rical val e u , d n a is far from be n i g a help u f l a n e g t rega d r n i g
T e h ob i v ous problem w t i h t s e h e pr v e o i us t h c e i n u q es is t e h r t em n e d s u o a l ck of
w o l l a u o y o t h t row and ha d n le c x e e t p i n o s. T e h type that represents th e
h t is except n o i -c t n e ric base class.
h t us e b e v o rri d d en by deri d e v types:
e G t e p y T ();
t l u s e R H { ; t e g ; t e s }

e g a P 4 0 1
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

c i l b u p l a u t r i v g n i r t s k n i L p l e H { ; t e g ; t e s }
c i l b u p n o i t p e c x E . m e t s y S n o i t p e c x E r e n n I { ; t e g }
c i l b u p l a u t r i v g n i r t s e g a s s e M { ; t e g }
c i l b u p l a u t r i v g n i r t s e c r u o S { ; t e g ; t e s }
c i l b u p l a u t r i v g n i r t s e c a r T k c a t S { ; t e g }
c i l b u p e s a B d o h t e M e t i S t e g r a T { ; t e g }
c i l b u p e d i r r e v o g n i r t s ; ) ( g n i r t S o T
}
The Simp e l st Pos i s l b e E a x l p m e
To u l l i str t a e h t e useful s e n s of st u r ct r u ed x e e c p i t on h n a d i l ng, we n e e d to r c eate a type
th t a a m y h t o r w n a n o i t p e c x e r e d n u t e h t c e r r o c s e c n a t s m u c r i c . s A s m u e e w v a h e
pr j o c e t (nam d e SimpleE e c x i t p on) h t at defi s e n tw o
class t p y es ( a C r and i d a R o) d e t a i c o s s a g n i s u e h t s a h - a p i h s n o i t a l e r . T e h Ra i d o e p y t
s e n i f e d a e l g n i s d o h t e m t a h t s n r u t e h t o i d a r s r e w o p n o r o : f f o
c i l b u p s s a l c o i d a R
{
c i l b u p d i o v l o o b ( n O n r u T ) n o
{
if( n o )
Con l o s e r W . t i e e n i L ( a J " m i m ng . . .");
e s l e
o C n e l o s r W . e t i e n i L (" u Q i t e m i t e . . . ; ) "
}
}
. 5 4 Throwing a Gen c i r e Exce i t p on
w o N h t at we h v a e a u f nctional a C r p y t , e I l l l i u l str e t a the simplest a w y to throw a n
x e ception. The u c r r e t n l p m i e n e m tat o i n of c c A e a r e l t ( e ) i d p s l s y a an err r o me s s a e g if
t e h ca l l er attempts to p s e d e p u h t e a C r d n o y e b s t i up e p r i l i m t .
o t w o r h t n a e n o i t p e c x f i e h t r e s u s t p m e t t a o t d e e p s p u h t e u a o t mobi e l after t i h s a me t
t i s m e k a r, o y u wa t n to cr a e te n a d o c nf g i ure a new i s n t n a ce of the y S st E . m e x i t p e c on
l c a s s , setting the val e u of h t e read-o l n y s e M sage r p y t r e p o a i v e h t cla s s r t s n o c u t c or.
n e h W u o y wi h s to s d n e the e r r or ob e j ct back to the calle , r make use of the C#
throw r o w y e k d. r e H e is t e h rel n a v e t o c de p u d e t a o t t e h Ac e l e c r ( e t a ) e m t d o h :
/ / s i h T , e m i t w o r h t n a n o i t p e c x e f i e h t r e s u s d e e p s p u d n o y e b p S x a m . d e e
c i l b u p d i o v t n i ( e t a r e l e c c A
{
f i ) d a e D s I r a c (
} 0 { " ( e n i L e t i r W . e l o s n o C s i t u o f o r e d r o ... , " ; ) e m a N t e p
e s l e
{
d e e p S r r u c = + ; a t l e d
f i
{
d a e D s I r a c = ; e u r t
d e e p S r r u c = ; 0
/ / e s U " w o r h t "

e D pt. f o I B J S , E S I T
t a e r c ed a w e n c n o s l o e p p a l a c i tion
To re t i f o r t i h t s d o h t e m
de a t l )
d e e p S r r u c ( = > ) d e e p S x a m

e g a P 5 0 1
d r o w y e k o t e s i a r n a . n o i t p e c x e
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

w o r h t n w e } 0 { " ( t a m r o F . g n i r t s ( n o i t p e c x E s a h , " ! d e t a e h r e v o ; ) ) e m a N t e p
}
e s l e
> = " ( e n i L e t i r W . e l o s n o C d e e p S r r u C = , " } 0 { ; ) d e e p S r r u c
}
}
e B f r o e e a x n i n i m g o h w a caller would c h c t a t i h s e c x p e tion, a few po n i ts of n i te e r st . r i F st
of l l a , n e h w u o y e r a h t r g n i w o n a i t p e c x e , n o t i s i s y a w l a p u o t u o y o t e d i c e d e y l t c a x t a h w
s e t u t i t s n o c e h t r o r r e in u q es i t on, and w n e h t i h s ould be thrown. , e r e H o y u r a e m k a i g n
a t t e t p m s to in r c ea e s t e h s e p ed of a car that has
r i p x e ed, a e t s y S m c x E . e o i t p n y t pe h s ou d l be thrown to i d n icate the Acc t a r e l e e() met d o h
can t o n con i t u n e ( h w ich m y a or may not be a valid s a s m u ption). l A te a n r i t e v ly, y u o
u o c ld m i plem n e t Acce e l r t a e() o t recover a m o t u a i t cal y l wi u o h t t nee n i d g to h t row an
n o i t p e c x e n i h t e r i f st p e c a l . y B a d n l r a ge, ex p e c o i t ns u o h s ld e b t o r h n w n o ly w n e h a
m e r o te n i m r al n o c d n o i t i s a h be n e met r o f ( e a x m l p e, not find n i g a ne s e c r a s y i f l , e
g n i l i a f o t co e n n ct to
a , e s a b a t a d d n a ) t o n t a h w . e D cid g n i x e ac l t y what c n o s i t tutes h t ro i w ng an x e ception is a
e d sign s i sue you u m st a w l s y a cont n e d t i w h. r o F u o r curre t n u p r e s o p s, ass m u e that
as g n i k a o o d e m d au b o m o t ile to increa e s t i s d e e p s ju t s if e i s a s u a c e o t t w o r h n a
i t p e c x e on.

5 . 5 Cat h c ing Exce o i t p ns
Because the Ac l e c r e a e t () d o h t e m n w o s w o r h t n a e e c x p i t o , n h t e cal r e l ne d e s to e b
y d a e r to h n a d e l


extract t e h e d ta l i s of the b o r p lem. t a h W o y u o d w t i h s i h t
a m y s i w h to log h t is information to a repo t r file, w t i r e the data to the W n i dows e v e t n
l g o , e-ma l i a y s stem a i m d nistrator, or dis l p y a h t e problem to t e h n e d user . , e r e H u o y
w l l i simply du p m t e h n o c t n e ts to h t e o c ns l o e i w ndo : w
/ / e l d n a H e h t n w o r h t n o i t p e c x e .
c i t a t s d i o v ] [ g n i r t s ( n i a M
{
g n i t a e r C * * * * * " ( e n i L e t i r W . e l o s n o C a r a c d n a g n i p p e t s n o t i ; ) " * * * * *
r a C r a C y m = w e n , " y p p i Z " ( r a C ; ) 0 2
myC . r a C n a r T k u ( s e n t e u r ; )

/ / d e e p S p u t s a p e h t s ' r a c x a m d e e p s o t
/ / r e g g i r t e h t n o i t p e c x e .
y r t
{
t n i ( r o f i = ; 0 i < ; 0 1 ) + + i
myCar. Ac l e c e t a r ( e 1 ) 0 ;
}
n o i t p e c x E ( h c t a c ) e
{

e D pt. f o T I B J S , E S I e g a P 6 0 1
t e h s a u s mption that if t e h program
e h t e n o i t p e c x s u o h ld it c o cur. e h W n u o y are invo i k g n a met d o h t a h t
m y a h t row an x e ce i t p n o , o y u k a m e e s u of a try/ t a c ch c o l b k. n O ce you have c u a ght th e
c x e ep o i t n type, you a e r ab e l to invoke the m m e b r e s of h t e t s y S e E . m x e c t p ion t p y e to
da a t s i a l rge y l up o t u o y . Y u o
r a s g )
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

( e n i L e t i r W . e l o s n o C "\n * * * r E o r ! r ; ) " * * *
o h t e M " ( e n i L e t i r W . e l o s n o C d: , " } 0 { . e r a T g t e S t i e);
s s e M " ( e n i L e t i r W . e l o s n o C : e g a , " } 0 { e. e M s a s ge ; )
e c r u o S " ( e n i L e t i r W . e l o s n o C : , " } 0 { . e u o S rce ; )
}

/ / e h T r o r r e s a h n e e b , d e l d n a h
o C n e l o s r W . e t i e n i L ("\n * * * **
Con l o s e e R . d a L ( e n i ; )
}
5.6 CLR S s y tem-Level E e c x t p o i ns (S s y e t . m SystemException)
e h T T E N . e s a b s s a l c libraries e d f n i e ma y n cl s a ses e d r v i ed fr m o t s y S e x E . m c o i t p e n. For
x e ample, the s y S tem a n me p s a e c e d fines core e o r r r ob e j cts u s h c s a
n e m u g r A t f O t u O R o i t p e c x E e g n a n, o i t p e c x E e g n a R f O t u O x e d n I n, o i t p e c x E w o l f r e v O k c a t S n, a d n
o s h t r o f . h t O er namespa e c s de i f ne e c x p e tio s n that r l f e ect the e b ha i v or of t a h t
a n m s e pa e c (e.g , . e t s y S m a r D . wi P . g n r n i n i t g defines prin i t g n ex p e c i t on , s y S s m e t .IO
s e n i f e d IO- a b e s d e c x e s n o i t p , t s y S e t a D . m a defines a d tabase-centric exceptio , s n and o s
x E e c p i t o s n that r a e thrown by the CLR are ( p a propriately) called y s stem
ex e c p o i t ns. Th s e e exc p e i t ons a e r r g e arded as n n o rec v o era l b e, fatal errors. S s y tem
a e s a b class a n m d e y S s S . m e t y E m e t s x t p e c ion , wh c i h i n
u t n r de i r e v s r f om t s y S em c x E . e i t p on (wh c i h de s e v i r f m o r S e t s y m b O . ject):
o i t p e c x E m e t s y S s s a l c c i l b u p n : n o i t p e c x E
{
/ /
}
G a h t n e v i t h t e o i t p e c x E m e t s y S . m e t s y S n e p y t s e o d t o n d d a y n a l a n o i t i d d a y t i l a n o i t c n u f
e b y s n o c f o t e s a d n o t s r o t c u r , y u o m o w t h g i d n r e w y h s y S t x E m e c o i t p e n s t s i x e n i e h t t s r i f
c a l p e. S t u p y l p m i , w e n a n e h x t p e c n o i t p y r e d e v i f s e rom t s y S c x E m e t s y S . m e e t p i n o , u o y e r a
b a l e t r e t e d o m n i e t t t a h e h N . E r T n u i t m e is
r e h t a r than e h t e d o c a b e s f o e h t e c e x uti g n a p p l a c i t o i n.

5.7 Custom A p p i l cat o i n- e L vel Ex t p e c ions S ( e t s y m.A p p licationException)
Gi n e v t a h t a l l .NET exce i t p n o s are class y t , s e p you are fr e e to cr a e te your w o n
x e c p e i t n o s. , r e v e w o H e u d o t e h t t c a f t a h t e h t
. m e t s y S S m e t s y E t p e c x ion b e s a class s e r p e r e s t n exc p e tions h t r n w o r f m o e h t L C R, u o y
y a m y l l a r u t a n e m u s s a t a h t u o y d l u o h s e v i r e d r u o y m o t s u c s n o i t p e c x e m o r f
. m e t s y S E t p e c x ion e p y t . l i h W e u o y d l u o c do , o s t s e b e c i t c a r p i d c s e t a t t a h t u o y d a e t s n i
e v i r e d m o r f h t e Sy e t s m c i l p p A . a E n o i t x i t p e c on e p y t :
n o i t p e c x E n o i t a c i l p p A : n o i t p e c x E

s u o i r a V co r t s n t c u o s r .
}
l i u B d g n i Cu t s om c x E e o i t p n , s Ta e k e n O
Wh e l i you can al a w ys th o r w i s n tan e c s of s y S t x E . m e e c p o i t n to signal a run m i t e e r r or (a s
h s own in u o r
s e u n i t n o c g n i s s e c o r p h t i w e h t t x e n . t n e m e t a t s
t u O f o n o i t p e c x e c i g o l ; ) " * * * * *
fo t r h).
excep i t ons derive directly from
s u o i r a V co r t s n t c u o s r .
h t e y t i t n e h t at s a h n w o r h t e h t e c x e t p i n o ,
p p a c i l a i t n o -sp c e ific
th e
c i l b u p l c s s a
{
/ /
f r i st x e amp e l ), it s i some m i t es v d a n a tageous to build a r t s on l g y typed

e D pt. f o T I B J S , E S I e g a P 7 0 1
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

c x e eption t a h t re e r p s s t n e t e h u e u q i n de s l i a t f o your cu e r r nt l b o r p em. F r o a x e mple,
a m u s s e you w h s i to bu l i d a custom e p e c x i t on (n d e m a a C r a e D s I d e c x E pt o i n) t o
n e s e r p e r t e h t e r o r r f o g n i d e e p s p u a doo e m d t u a m o ob e l i . T e h f r i st t s ep is to de i r ve a
new class f o r m y S s e t m c i l p p A . a E n o i t x i t p e c on y b ( , n o i t n e v n o c l l a e c x e o i t p n l c s a ses
i w th e h t E c x e t p i n o suffix . )
/ / s i h T m o t s u c n o i t p e c x e s e b i r c s e d e h t s l i a t e d f o e h t r a c - s i - d a e d n o i t i d n o c .
c i l b u p s s a l c i t p e c x E d a e D s I r a C on : n o i t p e c x E n o i t a c i l p p A
} {
k i L e any cla , s s you are fr e e to inclu e d any n m u ber of custom members h t at can be
e h t t a c ch o l b ck of h t e ca l l i g n logic. You are also free to override a y n vi t r ual
memb r e s defined by your parent a l c sses. For x e ampl , e we c u o ld m i plem n e t
C r a IsD a e d x E c p e t o i n by overriding the i v r u t al a s s e M ge proper y t :
n o i t p e c x E d a e D s I r a C s s a l c c i l b u p : n o i t p e c x E n o i t a c i l p p A
{
e t a v i r p g n i r t s ; s l i a t e D e g a s s e m

c i l b u p { ) ( n o i t p e c x E d a e D s I r a C }
c i l b u p r a C D s I a e p e c x E d t ( n o i s n i r t g m a s s e e g )
{
s l i a t e D e g a s s e m = ; e g a s s e m
}

/ / e h t e d i r r e v O e g a s s e M . n o i t p e c x E . y t r e p o r p
c i l b u p e d i r r e v o g n i r t s e g a s s e M
{
t e g
{
n r u t e r r a C " ( t a m r o F . g n i r t s r o r r E : e g a s s e M , " } 0 { ; ) s l i a t e D e g a s s e m
}
}
}
He e r , h t e a C r s I D a e dException type ma n i tains a i r p va e t data membe r
(m s e sageDe a t ils ) a h t t
s t e usi g n a custom cons u r t c o t r. h T o r i w g n this error from the A e l e c c r ( e t a ) is
strai h g tforw r a d. S m i ply a l l c o ate, c n o fi u g re, and t w o r h a Ca s I r D d a e x E c i t p e on e p y t
r e h t a r h t an a n e g e i r c y S s . m e t Ex t p e c ion :
/ / w o r h T e h t t s u c m o . n o i t p e c x E d a e D s I r a C
c i l b u p d i o v t n i ( e t a r e l e c c A
{
. . .
n o i t p e c x E d a e D s I r a C x e = s a h } 0 { " ( t a m r o F . g n i r t s ( n o i t p e c x E d a e D s I r a C w e n
, " ! d e t a e h r e v o ; ) ) e m a N t e p
k n i L p l e H . x e = h " / : p t t w w / . w R s r a C U ; " m o c . s
ex. t a D a d A . ( d " e m i T S m a t p",
; ) ) w o N . e m i T e t a D
ex. t a D a d A . ( d " s u a C e",

e D pt. f o I B J S , E S I T
end
l l a c ed within
represe t n s data rega d r ing the c r u r n e t excepti n o , which can be
de a t l )
e h T " ( t a m r o F . g n i r t s r a c d e d o l p x e t a , " } 0 {
u o Y " e v a h a d a e l ; ) " . t o o f

e g a P 8 0 1
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

w o r h t ; x e
. . .
}
o T c t a ch t i h s incomi g n exc p e tion explicitly, your c t a c h sc e p o n a c w o n be up t a d ed to
c t a c h
a spe i c fic s I r a C D E d a e x i t p e c on ty e p h ( owever, given th t a e D s I r a C adE i t p e c x on s i -a
m e t s y S .E t p e c x ion, t i is i t s ll p r e missible to catch a g n e eric Sy . m e t s x E i t p e c on s a well : )
] [ g n i r t s ( n i a M d i o v c i t a t s ) s g r a
{
. . .
cat h c ( E d a e D s I r a C xc t p e n o i ) e
{
/ / s s e c o r P g n i m o c n i . n o i t p e c x e
}
. . .
}
So, now that you under t s and the basic process of bui d l i g n a u c stom x e c p e tion, you a m y
o w n e d r h w n e you r a e r q e u r i d e o t do o s . , y l l a c i p y T u o y on y l e n ed to e t a e r c custom
is g i t h l t y n u o b d o t t e h cl s a s i s s n i u g e h t r e ror f ( or e e l p m a x , a
m o t s u c F l i e class t a h t th o r ws a num e b r of f l i e-re t a l ed er o r r , s aCar cla s s t a h t thr w o s a
m u n e b r of c r a -related errors, and o s forth). n I g n i o d s , o you pro i v de h t e caller i w th h t e
t i l i b a y to ha d n le u n mer u o s exceptions n o n a e r r or-by- r r e or basis.

5 . 8 H n a d i l ng Multiple E e c x t p o i n s
In its i s mpl t s e form, a try bl k c o has a s g n i le catch l b ock. n I rea i l ty, you of n e t r n u
t n i o a situa i t on where the t s a e t n e m ts wi h t in a t y r o l b k c o c d l u trigger n m u r e o s u
o p ssible e c x eptions. For example, s s a ume the cars Accelerat ( e ) me h t od l a so
th o r ws a a b e s -cla s s -libra y r prede n i f ed Argum n e tOutOf a R n e g E c x e t p ion if you pass an
invalid a p rameter (which we will s a sume is n a y
/ / t s e T r o f d i l a v n i t n e m u g r a e r o f e b g n i d e e c o r p .
c i l b u p d i o v t n i ( e t a r e l e c c A
{
a t l e d ( f i < ) 0
w o r h t w e n d e e p S " ( n o i t p e c x E e g n a R f O t u O t n e m u g r A t s u m e b r e t a e r g n a h t ; ) " ! o r e z
. . .
}
The a c tch l g o ic could o n w s e p cif c i ally p s e r ond to each y t pe of e c x eption:
c i t a t s d i o v ] [ g n i r t s ( n i a M
{
. . .
/ / , e r e H e w e r a n o e h t t u o k o o l r o f e l p i t l u m . s n o i t p e c x e
y r t
{
t n i ( r o f i = ; 0 i < ; 0 1 ) + + i
myC . r a A e c c e l r ( e t a 1 ) 0 ;
}

e D pt. f o T I B J S , E S I e g a P 9 0 1
c x e t p e o i ns when the error
value le s s h t an zero):
de a t l )
r a s g )
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

a c t C ( h c I r a e D s x E d a ce t p i n o e)
{
/ / s s e c o r P . n o i t p e c x E d a e D s I r a C
}
a c t A ( h c u g r n e m u O t tO a R f n e g x E c i t p e on e)
{
/ / s s e c o r P e m u g r A u O t n f O t a R x E e g n c . n o i t p e
}
. . .
}
h W en you are authoring mult p i le c t a c h b o l c , s k you must be aw r a e that h w en a n
c x e eption i s
r h t own, it w l i l be proce s s ed by t e h first av i a lable catch. o T t a r t s u l l i e l t c a x e y wh t a the
c t a c h a e m n , s s a s e m u you re r t o t i f ted the e r p vi u o s logic i w th an d a d t i i n o
c t a c h s o c pe t a h t a t t e p m ts to h n a d e l a l l ex e c ptio s n be o y d n Ca e D s I r a t p e c x E d ion an d
n e m u g r A tO f O t u R E e g n a x o i t p e c n y b catchi g n a gene i r c t s y S e c x E . m ept o i n as f w o l l o s:
/ / s i h T e d o c l l i w t o n ! e l i p m o c
c i t a t s d i o v ] [ g n i r t s ( n i a M
{
. . .
y r t
{
t n i ( r o f i = ; 0 i < ; 0 1 ) + + i
myC . r a A e c c e l r ( e t a 1 ) 0 ;
}
n o i t p e c x E ( h c t a c ) e
{
/ / s s e c o r P l l a r e h t o ? s n o i t p e c x e
}
a c t ( h c C I r a e D s x E d a ce t p i n o e)
{
/ / s s e c o r P . n o i t p e c x E d a e D s I r a C
}
a c t A ( h c u g r n e m u O t tO a R f n e g x E c i t p e on e)
{
/ / s s e c o r P e m u g r A u O t n f O t a R x E e g n c . n o i t p e
}
. . .
}

, s u h T f i u o y h s i w a e n i f e d o t c t a c h st n e m e t a t d n a h l l i w t a h t l a e n e y r s r o r y e b o d n
C r a s I e D d a x E e c pt o i n d n a Arg e m u ntO f O t u R E e g n a x t p e c ion , you wo l u d wri e t the following:
/ / s i h T e d o c s e l i p m o c t s u j e n i f .
c i t a t s d i o v ] [ g n i r t s ( n i a M
{
. . .
y r t
e D pt. f o T I B J S , E S I e g a P 0 1 1
t s r i f v a a l i b a e l
r a s g )
r a s g )
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

{
t n i ( r o f i = ; 0 i < ; 0 1 ) + + i
myC . r a A e c c e l r ( e t a 1 ) 0 ;
}
a c t C ( h c I r a s e D x E d a ce t p i n o e)
{
/ / s s e c o r P . n o i t p e c x E d a e D s I r a C
}
a c t A ( h c u g r n e m u O t tO a R f n e g x E c i t p e on e)
{
/ / s s e c o r P e m u g r A u O t n f O t a R x E e g n c . n o i t p e
}
n o i t p e c x E ( h c t a c ) e
{
/ / s i h T l l i w w o n e l d n a h l l a r e h t o e l b i s s o p s n o i t p e c x e
/ / n w o r h t m o r f s t n e m e t a t s n i h t i w e h t y r t p o c s e.
}
. . .
}

e G e n c i r t a c h c State s t n e m
# C l a so suppor s t a g e e n i r c ca c t h sc p o e t a h t d e o s not explic t i ly r c e i e ve the e c x ep i t on
ob e j t c h t o r wn








}
h c t a c
{
W . e l o s n o C r e t i e n i L ( o S " t e m i h g n d a b d e n e p p a h . . " . ; )
}
. . .
}
O v b i s u o ly, this is not the o m st informa v i t e way o t h n a l d e x e c p e o i t ns, g v i n e h t at u o y
a h ve no way to b o t n i a mea f g n i n ul data about the error h t at occ r u r d e (such as the
me d o h t n m a e, call t s ack, or s u c o t m e m s a s g ) e . e v e N r h t eless, C# d e o s al o l w for such a
n o c struct.

r h t e R o s n o i t p e c x E g n i w
Be aw r a e t a h t it is per i m s i s ble for log c i in a try blo k c to t e r h w o r n a ex p e c i t on up h t e
ca l l t s ack to t e h previous caller. To do s , o s m i l p y make use of t e h r h t ow e k y d r o w i h t i w n


e D pt. f o T I B J S , E S I e g a P 1 1 1
by a g v i n e mem e b r:
/ / A c i r e n e g h c t a c .
c i t a t s d i o v ] [ g n i r t s ( n i a M ) s g r a
{
. . .
y r t
{
t n i ( r o f i = ; 0 i < ; 0 1 ) + + i
myC . r a A e c c e l r ( e t a 1 ) 0 ;
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

a c c t a h block. This s a p s s e the c x e eption up the chain of a c i l l ng l c i g o , h w c i h a c n be
e h lpful if y u o r c t a c h bl k c o s i on y l ab e l t o parti l a ly
/ / g n i s s a P e h t k c u b .
c i t a t s d i o v ] [ g n i r t s ( n i a M ) s g r a
{
. . .
y r t
{
/ / d e e p S p u r a c c i g o l . . .
}
a c t C ( h c I r a e D s x E d a ce t p i n o e)
{
/ / o D y n a l a i t r a p g n i s s e c o r p f o s i h t r o r r e d n a s s a p e h t k c u b .
/ / , e r e H e w e r a g n i w o r h t e r e h t g n i m o c n i n o i t p e c x E d a e D s I r a C t c e j b o .
/ / , r e v e w o H u o y e r a o s l a e e r f o t w o r h t a t n e r e f f i d n o i t p e c x e f i d e e n . e b
w o r h t ; e
}
. . .
}

5.9 e h T Fin y l l a Block
Atry/c c t a h scope may also e d f n i e an o i t p n o al a n i f lly c o l b k. e h T mo i t a v o i t n be i h d n
a a n i f lly blo k c
r t a , e t a s s ume y u o i w sh o t y a w l a s e w o p r w o d n the a c r s
a r dio bef r o e ex t i ing i a M n(), l d r a g e r e s s of y n a
] [ g n i r t s ( n i a M d i o v c i t a t s ) s g r a
{
. . .
r a C r a C y m = w e n , " y p p i Z " ( r a C ; ) 0 2
myC . r a C n a r T k u ( s e n t e u r ; )

y r t
{
/ / d e e p S p u r a c . c i g o l
}
a c t C ( h c I r a e D s x E d a ce t p i n o e)
{
/ / s s e c o r P . n o i t p e c x E d a e D s I r a C
}
a c t A ( h c u g r n e m u O t tO a R f n e g x E c i t p e on e)
{
/ / s s e c o r P e m u g r A u O t n f O t a R x E e g n c . n o i t p e
}
n o i t p e c x E ( h c t a c ) e
{
/ / s s e c o r P y n a r e h t o . n o i t p e c x E

e D pt. f o T I B J S , E S I e g a P 2 1 1
han l d e h t e error at n a h : d
is to s n e r u e h t at a set of co e d statements l i w l always e x e cu , e t exception
o ( f a y n ty e p ) or n t o . T s u l l i o
ha d n e l d ex e c p o i t : n
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

}
y l l a n i f
{
/ / s i h T l l i w s y a w l a r u c c o . n o i t p e c x E r o . t o n
myC . r a C n a r T k u ( s e n f s l a e);
}
. . .
}

5.10 Ex t p e c o i ns U i s ng V s i ual u t S d o i 0 2 05
o T p a r w th g n i s , p u do e b a r a w e that V s i ual t S d u io 2005 pr v o i e d s a number of too s l that
g u b e d d e l d n a h n u m o t s u c c x e e s n o i t p . A a g i , n as u s me you have in r c eased the
e p s ed of aCar e j b o ct be n o y d h t e ma m u m i x . f I you e w e r to t r a t s a d b e ugging s e s sio n
s u ( ing the Debug Start me u n sel c e o i t n), Visual t S d u io u a toma i t ca l l y breaks at t e h
t m i e t e h u a c n ug t h ex p e c i t n o is thro n w . Better
5.11 U r e d n a t s d n ing t c e j b O Li t e f ime
e h W n you e r a u b l i ding yo r u C# app i l a c i t s n o , u o y a e r c r o r c e t o t as u s me t a h t t e h
m d e g a n a heap will take care of t i self h t i w out your dir c e t t n i er n e v i t on. In fact, h t e
golden u r le of E N . T me o m ry ma a n geme t n is s m i ple:
R e l u : o l l A a c e t n a o j b c e t o o t n t e h m n a a d e g he p a usi g n h t e e n w k y e word n a d forget
b a out t i . Once n w e - d e , r e g n o l o n s i t i n e h w t c e j b o e h t y o r t s e d l l i w r o t c e l l o c e g a b r a g e h t
e n d e d e . e h T t x e n
t e d er e n i m n e h w n a t c e j b o s i o n r e g n o l n e e d ? d e e h T sh t r o ( , . e . i n i c m o ple e t ) a s n e w r is
th t a e h t a g r a b ge collec o t r rem s e v o an b o e j ct from t e h e h ap e h w n t i is e r n u achable y b
y n a a p rt of u o y r e d o c b e s a . Assume you have a method that alloca s e t
c i l b u p c i t a t s i o v d ) ( r a C A e k a M
{
/ / f I r a C y m s i e h t y l n o e c n e r e f e r o t e h t r a C , t c e j b o
/ / t i y a m e b d e y o r t s e d n e h w e h t d o h t e m s n r u t e r .
r a C r a C y m = w e n ; ) ( r a C
. . .
}
e h T 2 1 . 5 CIL of e n w
n e h W e h t # C r e l i p m o c s r e t n u o c n e e h t new k w y e ord, it w l i l m e t i a L I C b o w e n j inst u r t c i n o
n i h t o t e met d o h
bly u g n i s x e . m s a d l i e, u o y d l u o w d n i f t e h f g n i w o l l o I C L
s t n e m e t a t s n i h t i w e h t M ( r a C A e k a ) h t e m od :
. d o h t e m c i l b u p g i s y b e d i h c i t a t s d i o v ) ( r a C A e k a M l i c d e g a n a m
{
/ / e d o C e z i s 7 ) 7 x 0 (
.m s x a t k c a 1
. s l a c o l t i n i ] 0 [ ( s s a l c r a C . e z i l a n i F e l p m i S ) c
IL_ 0 0 0 0: j b o w e n a t s n i c n e o v d i p m i S F e l ina e z i l . : : r a C . r o t c ) (
: 5 0 0 0 _ L I 0 . c o l t s
: 6 0 0 0 _ L I t e r
} / / d n e f o d o h t e m r a C A e k a M : : m a r g o r P
e D pt. f o T I B J S , E S I e g a P 3 1 1
help you
, t e y u o y a e r p e s e r d e t n wi h t a w . w o d n i
s u o i v b o , n o i t s e u q f o , e s r u o c , s i w o H s e o d e h t r o t c e l l o c e g a b r a g
a local a C r object:
o i t a t n e m e l p m i n. f I u o y e r e w o t l i p m o c e h t e t n e r r u c e l p m a x e e d o c d n a
e t a g i t s e v n i e h t g n i t l u s e r m e s s a
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

e B f r o e we e a x m n i e the x e c a t rules that deter i m ne e h w n an obj c e t is e r moved m o r f th e
, p a e h s t e l k c e h c t u o e h t e l o r f o e h t L I C b o w e n j n i struc i t n o in a b t i m e r o e d ta l i .
r i F s , t d n u erstand h t t a e h t managed h a e p is o m e r t a h n just a ra m o d n c n u h k o f
me o m ry acce s s ed by t e h C R L . T e h . T E N a g r g a b e e l l o c c o t r is u q ite a t d i y h u o e s e k eper
f o h t e p a e h , e v i g n a h t t it i w ll a p m o c t c emp y t k c o l b s of memory (wh n e neces a s ry) for
r u p po e s s of op i m i t zati n o . To aid in this n e d v a e r o , the m a n a g d e he p a ma t n i ai s n a
o p inter o c ( mm n o ly e r f r e red o t as t e h n t x e b o je t c poin e t r or n w e ob t c e j po n i ter) t a h t
f i t n e d i ies e a x c l t y h w r e e the n x e t o j b e t c will be lo a c ted.
b o w e n j i s n t t c u r i n o i o f n rm s t e h CLR o t p f r e orm t e h fo l l owing o c re t s a ks:
Cal u c la e t t e h total am n u o t f o memory requir d e f r o t e h b o e j ct to e b a l l o a c t d e
i ( c n lu i d g n t e h necessary mem y r o requir d e by t e h t p y e s mem e b r vari b a e l s and the
e p y t s b s a e clas e s s).
E a x i m e n h t e m g a n a ed heap to ensu e r t a h t h t r e e is inde d e enou h g room to host h t e
t c e j b o o t e b d e t a c o l l a . f I s i h t s i e h t , e s a c e h t s e p y t r o t c u r t s n o c s i , d e l l a c d n a e h t r e l l a c s i
e t a m i t l u y l d e n r u t e r a re e r e f nce o t h t e e n w ob t c e j n i me , y r o m h w ose a e r d d ss u j st
n e p p a h s to be e d i n l a c i t to e h t t s a l o p siti n o of the t x e n e j b o ct r e t n i o p .
Fi a n l l y, before re u t rning the referen e c o t the ca l l e , r adv n a ce t e h x e n t b o ject p t n i o er
to o p int o t h t e next av a l i a b e l l s t o n o h t e man d e g a p a e h .
As you r a e busy n i t a c o l l a g t c e j b o s in u o y r ap i l p c o i t a n, the a p s ce on e h t mana e g d h a e p
y l l a u t e b come f l l u . h W en proc s e n i s g the n o w e bj n i s r t uc o i t n, if h t e L C R
de e t r i m n s e h t at t e h managed e h ap o d s e not ha e v su f f t n e i c i me r o m y o t c o l l a ate e h t
d e t s e u q e r , e p y t t i l l i w m r o f r e p a g a b r a g e i t c e l l o c on in n a a t t e t p m to r f ee up memory.
, s u h T e h t t x e n e l u r f o e g a b r a g n o i t c e l l o c s i o s l a e t i u q e l p m i s .
R e l u : f I the man e g a d heap do s e n t o a h ve sufficie t n me o m ry to allocate a r q e u s e t d e
b o e j c , t a a g r g a b e co l l e o i t c n wi l l u c c o r. n e h W a co e l l c n o i t o d s e e k a t c a l p e, h t e g r a e g a b
r o t c e l l o c l i r a r o p m e t y s d n e p s u s l l a c a t e v i thr a e ds t i w h n i t e h u c r n e r t p c o r ess to ensure
t a h t t e h app i l ca i t n o does not c c a e s s t e h heap dur n i g t e h c l o le t c ion p s e c o r s. o H e w ve , r
o f r the ti e m being, si l p m y re a g rd a h t r a e d as a p t a h of execution t i w hin a running
e x e cutable. c n O e e h t e g a b r a g o i t c e l l o c n l c y c e s a h c , d e t e l p m o t e h s u s d n e p ed d a e r h t s
e r a t t i m r e p ed o t y r r a c n o r i e h t w k r o . Tha k n f y l l u , the .NET garbage c l o lector is hi h g ly
p o timize ; d y u o will e s ldom i ( f v e er) no i t ce this brief i t n erru t p ion in y u o r appli a c t n o i .

5.13 e h T b i s a cs of Ga b r a e g c l l o t c e ion
e h W n t e h L C R is attempting to locate r n u ea h c able obj c e ts, is d e o s o n t t i l era l l y
x e i m a ne each and e e v ry j b o e t c l p aced n o h t e managed h a e p. b O vi u o sly, o d n i g so
o w uld involve consi e d rable m i t , e s e pecially in larger (i.e., real-world) applic i t a s n o .
o T help z i m i t p o e the s e c o r p s, a e ch obj c e t on the e h ap is s a si n g d e o t a spe i c fi c
e g n r e a i t n o .The a e d i d n i h e b s n o i t a r e n e g s i s m i p : e l e h T r e g n o l n a o t c e j b s a h i x e s d e t n o
to st y a h t e e r . r o F ex m a pl , e t e h b o j t c e m i pl m e e i t n g n
( n i a M ) l l i w e b n i m m e ory t n u il t e h prog a r m e t m r in s e t a . , y l e s r e v n o C s t c e j b o t a h t e v a h
n e e b y l t n e c e r d e c a l p n o e h t p a e h e r a y l e k i l o t e b e l b a h c a e r n u r e h t a r y l k c i u q h c u s ( s a n a
e j b o ct c t a e r ed n i h t i w a meth d o c s o ) e p . G n e v i h t s e e ass o i t p m u ns, e c a h obje t c on h t e
a e h p belo g n s to n o e of h t e follo i w ng ge e n ra i t n o s:
e G n r e i t a on 0: I e d n i t f e i s a newly alloca e t d ob e j ct that h s a e n ver be n e marked for
l o c lect o i n


e D pt. f o T I B J S , E S I e g a P 4 1 1
m n a aged
e h T se thi g n s e b i s g n aid, t e h
y a m n e v e
e h t , p a e h e h t e r o m k i l e y l it s i
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

e G ner n o i t a 1: Ide i t n f e i s an o j b e t c that a h s su v r ived a gar a b ge colle i t c on . i ( e., it w s a
a m rk d e for co e l l ction, but was not removed d e u to the fact t a h t the su f f ic n e i t hea p
c a p s e was acq i u red)
Gener t a i n o 2: I e d t n i i f s e an e j b o ct t t a h has survived mo e r t a h n o e n sweep of the
r a g bage c l l o c e tor The gar a b ge co l l ector i w l l i s e v n i t a g te a l l ge e n o i t a r n 0 ob e j t c s i f t s r . f I
n i k r a m g n a d s g n i p e e w e s e h t ob e j cts e r u s lts in the required a o m unt of fr e e memory, a y n
r u s v n i v i g obje t c s are promoted to ge e n ra n o i t 1. o T e t a r t s u l l i w o h n o i t a r e n e g s t c e j b o n a
s t c e f f a e h t n o i t c e l l o c s s e c o r p .

4 1 . 5 e h T n o i t a z i l a n i F a , e p y T e h T n o i t a z i l a n i F s s e c o r P
Not to a e b t a a e d d horse, but alw y a s remember t a h t t e h o r le of t e h Fi i l a n z ( e ) d o h t e m
s i to ensure


d l u o h s n g i s e d r u o y s e p y t o t d i o v a g n i t r o p p u s a ( e z i l a n i F )
n o s a e r t a h t n i f a n o i t a z i l k a t s e ti e m . h W en you a o l l ca e t n a b o j c e t n o to the m d e g a n a
he p a , the u r m i t n e automa i t a c l l y e d ter n i m es w e h t e h r your b o j c e t supports a cus o t m
z i l a n i F e() method. If , o s t e h object is marked as i f n i l a zable,
s i stored on n a inter l a n qu e u e named t e h i f n i l a z i t a n o qu u e e. T e h i f na z i l ati n o u q eue is a
ta l b e t n i a m ained by e h t ga b r age c l o lec r o t h t at po n i ts to c a e h a d n every j b o ect that
u m st be f a n i i l zed b f e o e r t i is removed r f om t e h e h ap. Wh n e t e h ga a b r ge collect r o
e d termi e n s t i is i t me o t free an ob c e j t r f om memory, t i x e a i m nes each e t n ry n o h t e
i f na i l a z i t on u q eu , e and c i p o s e e h t obje t c off h t e h a e p o t e y t a o n ther managed
e r u t c u r t s d e m r e t e h t n i f a z i l ati n o reachable ta l b e (of n e t a e r b b viated as r f a e chable, a d n
f f e -re c a h b a e l ). t A i h t s , t n i o p a r a p e s a e t a e r h t d is spa n w ed o t invo e k t e h
a e ch t c e j b o on t e h r f a h c a e b e l ta l b e t a e h t next r a g bage
l o c l i t c e n o . n e v i G t , s i h t i i w l l t e k a at e v ry le t s a w t o g b r a age collect o i ns to u r t ly final z i e
n a obje t c . The o b t o t m l n i e is h t at w l i h e fi i l a n a z i t on f o n a c e j b o t does ensure an
obj c e t a c n cl a e n up unma a n ged resources, it is s i t ll nondeterministic n i nature, and
due to the extra behind-the-

5.15 u B l i ding Ad Hoc e D str c u ti onM t e hod
p u s e h T rem e a b se cla s s o . f NET, c e j b O . m e t s y S t, s e n i f e d a i v r l a u t d o h t e m named
Fin i l a z ( e ). e h T de t l u a f l p m i eme n o i t a t n f o s i h t m t e h d o e o d s n t o i h g n h w atsoe e v r:
/ / t c e j b O . m e t s y S
c i l b u p s s a l c t c e j b O
{
. . .
d e t c e t o r p l a u t r i v d i o v l a n i F ) ( e z i } {
}
n e h W u o y e v o r r d i e n i F a z i l ( e ) o f r your custom c s a l se , s you estab h s i l a s e p cific
location to per
is e d fi d e n as prot c e ted, t i is n t o o p s i s b e l o t d r i ec y l t c l a l an j b o c e t s Fin z i l a e() t e m hod.
Ra , r e h t h t e a g g a b r e o c l t c e l r o w l l i ca l l an o j b c e s t F l a n i iz ( e ) d o h t e m f i ( r o p p u s d e t )
e r o f e b g n i v o m e r e h t t c e j b o m o r f y r o m e m . Of c u o rse, a ca l l o t Fi a n l ( e z i ) l l i w

e D pt. f o T I B J S , E S I e g a P 5 1 1
h t at a T E N . ob e j ct can e l c an up unm n a aged reso r u ces h w n e a g r g a b e
e t c e l l o c d. h T u , s if u o y e r a b l i u d n i g a t p y e h t at do s e o n t make use of unm n a aged e t n i i t es
y b ( r a f e h t t s o m n o m m o c , ) e s a c z i l a n i f t a n o i is f o i l t l t e u e s . n I , t c a f f i t a l l a i s s o p b , e l u o y
me o h t d for h t e very s m i pl e
and a o p inter to i h t s obj c e t
r p u o n o n e c d
Fin i l a z ( e ) d o h t e m r o f
curta n i s r p c o s s e in , g c n o sider y l b a slowe . r
fo m r any c e n essary c e l an p u logic for your type . v i G en h t at this mem e b r
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

) y l l a u t n e v e ( r u c c o g n i r u d a l a r u t a n e g a b r a g n o i t c e l l o c or h w en o y u pr g o a r m m a a c i t l y l
e c r o f a c l l o e n o i t c v a i G e l l o C . C c ( t ). n I i t i d d a o , n a ty e p s fin e z i l a r method
y l l a c i t a m o t u a e b ca l l d e h w en h t e a p p i l a c i t on do n i a m hos i t ng r u o y ap i l p a c i t n o is
unl a o d d e fr m o y r o m e m . d e s a B n o r u o y t n e r r u c d n u o r g k c a b n i , T E N . o y u y a m kn w o t a h t
o i t a c i l p p a n doma s n i (or i s mply p A pDo i a m ns) e r a used to ho t s n a exe u c tab e l a s s embly
d n a any ne e c s a s ry x e ternal o c de i l brari s e .

6 1 . 5 g n i l i a t e D e h t n o i t a z i l a n i F s s e c o r P
Not to a e b t a a e d d horse, but alw y a s remember t a h t t e h o r le of t e h Fi i l a n z ( e ) d o h t e m
s i to ensure


d l u o h s n g i s e d r u o y s e p y t o t d i o v a g n i t r o p p u s a ( e z i l a n i F )
n o s a e r t a h t n i f a n o i t a z i l k a t s e ti e m . h W en you a o l l ca e t n a b o j c e t n o to the m d e g a n a
he p a , the u r m i t n e automa i t a c l l y e d ter n i m es w e h t e h r your b o j c e t supports a cus o t m
z i l a n i F e() method. If , o s t e h object is marked as i f n i l a zable,
s i stored on n a inter l a n qu e u e named t e h i f n i l a z i t a n o qu u e e. T e h i f na z i l eti n o u q eue is a
ta l b e t n i a m ained by e h t ga b r age c l o lec r o t h t at po n i ts to c a e h a d n every j b o ect that
u m st be f i l a n i zed b f e o e r t i is removed r f om the e h ap. Wh n e t e h ga a b r ge collect r o
e d termi e n s t i is i t me o t free an ob c e j t r f om memory, t i x e a i m nes each e t n ry n o h t e
i f na i l a z i t on u q eu , e and c i p o s e e h t obje t c off h t e h a e p o t e y t a o n ther managed
e r u t c u r t s d e m r e t e h t n i f a z i l ati n o reachable ta l b e (of n e t a e r b b viated as r f a e chable, a d n
f f e -re c a h b a e l ). t A i h t s , t n i o p a r a p e s a e t a e r h t d is spa n w ed o t invo e k t e h
a e ch t c e j b o on t e h r f a h c a e b e l ta l b e t a e h t next r a g bage
l o c l i t c e n o . n e v i G t , s i h t i i w l l t e k a at e v ry le t s a w t o g b r a age collect o i ns to u r t ly final z i e
n a obje t c . The o b t o t m l n i e is h t at w l i h e fi i l a n a z i t on f o n a c e j b o t does ensure an
obj c e t a c n cl a e n up unma a n ged resources, it is s i t ll nondeterministic n i nature, and
due to the extra behind-the-
5.17 The System.GC Type
The s a b e class l b i raries provide a c a l ss type named Sys m e t .GC t a h t a l l ows you to
i t n era t c w h t i h t e gar a b e g c l l o t c e or us n i g a set of sta i t c b m e m ers.
No , w do be very a a w re that you will d l e s m o (if e v e ) r d e e n to k a m e s u e f o i h t s p y t e
l t c e r i d y in u o y r c e d o . y T p c i a l l y spe k a i g n , the on y l time u o y will ma e k use of e h t
r e b m e m s of t s y S e G . m C is wh n e you r a e crea i t ng types that a m ke use of
e r o s urc s e . l b a T e 5-1 pr e d i v o s a rund n w o of o s me of the more in e t e r t s ing memb r e s
t l u s n o c ( h t e E N . T r F a k r o w e m 0 . 2 K D S o D c t n e m u t a n o i for m o c plete t e d a l i s).

Ta e l b 5-1. S l e t c e Members of t e h s y S te G . m C T e p y
g n i n a e M n i Li e f
w o l l A u o y to p s c e ify a m u n r e c i al va u l e
t a h t e r pres t n e s h t e g n i l l a c s t c e j b o
u r e g ncy e l v l e r a g e r ding the garbage
co e l l ct o i n proc s s e . e B awa e r that these
me s d o h t s o h u d l alter pressure n i t n a dem

e g a P 6 1 1
will
h t at a T E N . ob e j ct can e l c an up unm n a aged reso r u ces h w en a g r g a b e
e t c e l l o c d. h T u , s if u o y e r a b l i u d n i g a t p y e h t at do s e o n t make use of unm n a aged e t n i i t es
y b ( r a f e h t t s o m n o m m o c , ) e s a c z i l a n i f t a n o i is f o i l t l t e u e s . n I , t c a f f i t a l l a i s s o p b , e l u o y
me o h t d for h t e v r e y s m i pl e
and a o p inter to i h t s obj c e t
r p u o n o n e c d
Fin i l a z ( e ) d o h t e m r o f
curta n i s r p c o s s e in , g c n o sider y l b a slowe . r
p o r g a r mma i t a c l l y
n u man g a ed
Sys m e t .GC Mem e b r
Add m e M o P y r e r s e r u s ) (





e D pt. f o I B J S , E S I T
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

a d n h t us a d d ed.
Col c e l t() c r o F s e the C G to p r e f m r o a g r a bage
collect o i n.
Col c e l t n o i o C u ( t n ) Re u t rns a u n meri a c l a v lue represen n i t g
h w o many times a given generat n o i has
b n e e sw p e t.
Get n e G e t a r ion() s n r u t e R h t e r e n e g a n o i t o t wh h c i an
t c e j b o c y l t n e r r u be . s g n o l
Get t o T a e M l o m r ( y ) s n r u t e R e h t d e t a m i t s e t n u o m a f o y r o m e m
n e r r u c ) s e t y b n i ( t l a y l l a c o t e h t n o d e
m n a aged he p a . h T e Boolean a p rameter
e p s cif e i s h w et e h r the c l a l l u o h s d wait f r o
e g a b r a g c l l o c e t n o i to occur b f e r o e
n i n r u t e r g.
Max n e G e t a r ion system. Un e d r Microsofts N . ET 2.0,
th r e e r a e t e r h e po s s ible ge e n r s n o i t a ( , 0 1,
d n a 2).
Sup e r p s i F s a n l e z i () Se s t a flag ind c i ating that t e h sp c e i d e i f
e j b o ct should o n t have its
Fi i l a n ze() me d o h t called.
Wai o F t r n e P i d n n i F g a z i l e s r () u S s e p n s d h t e t n e r r u c e r h t ad unt l i a l l
l b a z i l a n i f e t c e j b o s e v a h e e b n i f nalized.
s i h T me d o h t s i y l l a c i p y t l l a c ed d e r i c l t y
r e t f a n i k o v n i g

e d n o P r the fo i w o l l ng a M i ( n ) method, whi h c i l l ustrates e s lect members of Sy G . m e t s C:
c i t a t s ] [ g n i r t s ( n i a M d i o v ) s g r a
{
/ / t n i r P t u o d e t a m i t s e r e b m u n f o s e t y b n o p a e h .
n o s e t y b d e t a m i t s E " ( e n i L e t i r W . e l o s n o C : p a e h , " } 0 {
m e M l a t o T t e G . C G y r o ( l a f ) e s ; )

/ / n o i t a r e n e G x a M s i o r e z , d e s a b o s d d a 1 r o f y a l p s i d s e s o p r u p .
s a h S O s i h T " ( e n i L e t i r W . e l o s n o C 0 { } t c e j b o . s n o i t a r e n e g \ , " n
n o i t a r e n e G x a M . C G ( + ; ) ) 1

r a C r a C y M o T f e r = w e n , " y p p i Z " ( r a C ; ) 0 0 1
o C n e l o s r W . e t i e n i L ( f e r M o T C y o T . r a St g n i r ( ; ) )

/ / t n i r P t u o n o i t a r e n e g f o r a C y M o T f e r t c e j b o .
r a C y M o T f e r f o n o i t a r e n e G " ( e n i L e t i r W . e l o s n o C : s i , " } 0 {
G t e G . C G e n e t a r o i ( n r T f e y M o a C ; ) ) r

; ) ( e n i L d a e R . e l o s n o C
}



e D pt. f o T I B J S , E S I e g a P 7 1 1
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

i t s e u Q d e d n e m m o c e R o : s n
1. Ex n i a l p e h t s s e c o r p f o fi e z i l a n c e j b o i t n e n . t v n e ir n o m n e t
2. e t i r W a m a r g o r p n i # c o t w o r h t d n a e l d n a h g n i w o l l o f ex p e c s n o i t n i i k n a b ng t a c i l p p a i n o
m u m i n i m e c n a l a b ex n o i t p e c u g r a m t n e t u o f o a r n e g ex t p e c i n o
3. t s i L d n a ex n i a l p h t i w o c de e r o c m s r e b m e f o sy e t s m. t n o i t p e c x E y e p
4. e n i f e D a m d o h t e t a h t wo d l u d n a t r o s n a array f o i r e g e t n
5. t s i L d n a n i a l p x e e r o c s r e b m e m f o t e h s m e t s y ex n o i t p e c typ w o . e d l u o w o y u d l i u b m o t s u c
? n o i t p e c x e
6. e t i r W # c n o i t a c i l p p a o t e t a r t s u l l i g n i l d n a h m t l u iple ex t p e c i s n o .
7. t a h W s i t n a e m by t c e j b o e f i l e m i t ? e b i r c s e D e h t e l o r f o t e N . e s a b r a g , n o i t c e l l o c
n o i t a z i l a n i f s s e c o r p d n a d A c o H r t s e d u n o i t c e m t , d o h h t i w exampl s e .






































e D pt. f o T I B J S , E S I e g a P 8 1 1
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7




T I N U - 6
s e c a f r e t n I a n o i t c e l l o C d n s

1 . 6 g n i n i f e D s e c a f r e t n I g n i s U # C
2 . 6 g n i k o v n I n I t e c a f r e s r e b m e M t a e h t t c e j b o e L , l e v
3 . 6 Ex g n i s i c r e e h t s e p a h S c r a r e i H hy
4 . 6 g n i d n a t s r e d n U t i c i l p x E e c a f r e t n I , n o i t a t n e m e l p m I
5 . 6 s e c a f r e t n I s A o P l c i h p r o m y , s t n e g A
6 . 6 g n i d l i u B e c a f r e t n I s e i h c r a r e i H , n o i t a t n e m e l p m I
7 . 6 s e c a f r e t n I g n i s U S V g n i d n a t s r e d n u , T E N . e h t e l b i t r e v n o C I n I t r e f c a e ,
8 . 6 g n i d l i u B a m o t s u C E r o t a r e m u n ( e m u n E I r e l b a d n a , ) r o t a r e m u n E
9 . 6 g n i d l i u B e n o l C b a le s t c e j b o ( , ) e l b a e n o l C I
0 1 . 6 g n i d l i u B e l b a r a p m o C s t c e j b O I l b a r a p m o C e
1 1 . 6 g n i r o l p x E e h t s m e t s y . s n o i t c e l l o C e c a p s e m a N
2 1 . 6 g n i d l i u B a m o t s u C r e n i a t n o C t t i f o r t e R ( g n i the s r a C T ) e p y .




























e D pt. f o T I B J S , E S I e g a P 9 1 1
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

6.1 Defining Interfaces in C #
o T beg n i i h t s h c a , r e t p l l a ow me to pr i v o de a o f rmal defin t i ion of the in e t f r a e c type.
An i t n e f r ace is n i h t o n g o m re h t an a named c e l l o ction of e s ma t n ica y l l e r l e t a d t c a r t s b a
r e b m e m s. h T e spec c i f i me b m e s r de d e n i f y b an i t n e e c a f r p e d end n o e h t x e a t c a h e b v r o i
t i is o m e d i l g n . , s e Y s t i e u r t . An inter a f e c x e pre s s es a h e b avior h t at a given class o r
t A a syntact c i lev l e , an interface s i defined s u ing the
C# n i e t r c a f e o w y e k rd. Un i l ke o h t er .NET ty e p s, interfaces e n ver specify a b s a e class
(not v e en S e t s y m O . e j b ct ) d n a n i a t n o c s r e b m e m t a h t o d not k a t e n a a c c ess mo i d f e i r (a s
l l a n i te f r c a e memb s r e r a e m i plic t i ly publi ) c . o T get the l a b l ro l l ing, here is a u c stom
in r e t face defi e n d in C#:
/ / s i h T e c a f r e t n i s e n i f e d e h t r o i v a h e b f o g n i v a h " " . s t n i o p
c i l b u p e c a f r e t n i y t n i o P I
{
/ / y l t i c i l p m I c i l b u p d n a . t c a r t s b a
y b te G u N t e e b m O r t n i o P f s();
}
As o y u c n a se , e h t e IPo t n i y i t n er a f ce de n i f es a g n i s e l meth d o . Ho , r e v e w . E N T
e t n i r e c a f y t p s e r a e a o s l able to def n i e any u n m e b r of pr e p o rties. o F r l p m a x e e, u o y d l u o c
cr t a e e e h t IP t n i o y t n i erface
acc s e sor e m h t o : d
/ / e h T y t n i o p r o i v a h e b s a a d a e r - y l n o . y t r e p o r p
c i l b u p e c a f r e t n i y t n i o P I
{
e t y b } ; t e g { s t n i o P
}
o D d n u erstand that n i terface types are quite usele s s on their own, as h t y e are
noth n i g mo e r than a named colle t c ion of a t s b r c a t me e b m rs. v i G en this, you a c nn t o
l l a ocate in e t rface ty e p s as you would a class or st u r cture:
/ / ! k c A g e l l I l a o t " w e n " e c a f r e t n i s e p y t .
c i t a t s d i o v ] [ g n i r t s ( n i a M
{
y t n i o P I p = w e n ; ) ( y t n i o P I / / r e l i p m o C ! r o r r e
}
In r e t f e c a s do not bri g n h c u m o t e h t e l b a t un l i t h t ey e r a l p m i eme d e t n y b a c s s a l o r
I t n i o P y s i an in r e t face h t at exp s e r ses the behavior of having points.
As u o y c n a tell, this beh i v a r o mi h g t e b usef l u n i t e h a h s p s e h e i rarchy de e v lop d e n i
h C p a ter 4. e h T e d i a is m i s p e l : Some c s a l ses in
as t e h x e H g a on), while ot e h rs (such as the C c r i le) o d n t o . If you c n o i f u g re e H a x gon n a d
Tri l g n a e to p m i leme t n h t e P I ointy inter e c a f , y u o can safely ass m u e h t at each l c as s
w o n s t r o p p u s a c o m m o n be , r o i v a h and therefo e r a common set of me b m ers .

6.2 Invoking In e t rfa e c Mem r e b s a t o e j b ct level in C#
e h W n a class (or structure) c o o h ses to x e t d n e t i s u f nc o i t a n t i l y by s p u porti g n interfa e c
t p y es, t i do s e so u i s ng a com a m -d l e im t i ed list in t e h t p y e defi i n ti n o . e B e r a w a t a h t the
e h t f r i t s item i l sted e t f a r h t e o c l n o e p o rator. When your s s a l c

e g a P 0 2 1
structure may o h c o e s to su p p ort.
o t use a r a e d- y l n o p p o r er y t rath r e t a h n a r t ad i t i onal
r a s g )
st r u t c u r e. Here,
the Shapes h e i rar y h c h e v a o p t n i s ( h c u s
t c e r i d e s a b a l c ss u m t s b e

e D pt. f o I B J S , E S I T
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

e p y t e d i r ves d r i e l t c y fr m o S e j b O . m e t s y ct, y u o are f e r e to s m i ply list the in f r e t a e c ( ) s
u s p o p rted by the c s a l , s as h t e C# co i p m ler l i w l extend y u o r
y u o do not say otherwi e s . On a related note, given h t at structures alwa s y deri e v r f om
y S s e t m.V l a ueT p y e e s ( e h C a t p er ) 3 , y l p m i s i l st h c a e t n i e a f r e c di c e r tly a t f er h t e
e r u t c u r t s defini i t on. n o P der the following e a x mp e l s:
/ / s i h T s s a l c s e v i r e d m o r f t c e j b O . m e t s y S d n a
/ / s t n e m e l p m i a e l g n i s e c a f r e t n i .
c i l b u p s s a l c s s a l C e m o S : e c a f r e t n I e m o S I
{ . . .}

/ / s i h T s s a l c o s l a s e v i r e d m o r f t c e j b O . m e t s y S
/ / d n a s t n e m e l p m i a e l g n i s e c a f r e t n i .
c i l b u p s s a l c s s a l C y M : , t c e j b o e c a f r e t n I e m o S I
{ . . .}

/ / s i h T s s a l c s e v i r e d m o r f a m o t s u c e s a b s s a l c
/ / d n a s t n e m e l p m i
s s a l C r e h t o n A s s a l c c i l b u p : , s s a l C e s a B y M e c a f r e t n I e m o S I
{ . . .}

/ / s i h T t c u r t s s e v i r e d m o r f m e t s y S e p y T e u l a V . d n a
/ / s t n e m e l p m i o w t s e c a f r e t n i .
t c u r t S e m o S t c u r t s c i l b u p , e c a f r e t n I e m o S I : y t n i o P I
{ . . .}
d n a t s r e d n U t a h t g n i t n e m e l p m i n a e c a f r e t n i s i n a l l a - r o - g n i h t o n n o i t i s o p o r p . e h T
t p y e is o n t able to e s l c e i t e v ly c o h s o e which mem e b rs t i w l l i implem n e t.
Giv n e that the n i o P I y t n i terfa e c def n i es a si g n le property, this is not t o o m h c u of a
burden . , r e v e w o H if o y u r a e m i plem n e i t ng an interface h t at defines t n e mem e b s r , the
t p y e is o n w r o p s e nsible for fleshi g n u o t h t e details of e h t ten b a s r t a t c i t i t n e es.
ca , e s here is h t e m i plem n e tation of the upda d e t sha e p s h e i r a r chy ( o n te t e h new
Tri g n a le l c a s s typ ) e :
/ / w o n n o g a x e H s t n e m e l p m i y t n i o P I .
c i l b u p s s a l c n o g a x e H : , e p a h S y t n i o P I
{
c i l b u p { ) ( n o g a x e H }
c i l b u p g n i r t s ( n o g a x e H ) e m a n : { ) e m a n ( e s a b }
c i l b u p e d i r r e v o d i o v ) ( w a r D
{ g n i w a r D " ( e n i L e t i r W . e l o s n o C } 0 { e h t , " n o g a x e H ; ) e m a N t e P }

/ / y t n i o P I . n o i t a t n e m e l p m I
c i l b u p e t y b s t n i o P
{
t e g { n r u t e r ; 6 }
}
}

/ / w e N e p a h S d e v i r e d s s a l c d e m a n . e l g n a i r T
e D pt. f o T I B J S , E S I e g a P 1 2 1
y t pes f o r m S m e t s y . c e j b O t if
a e l g n i s . e c a f r e t n i
g n i t r o p p u s
In y n a
C# d n a m m a r g o r P T E N . i g n I 6 0 S7 1 6

c i l b u p s s a l c e l g n a i r T : , e p a h S y t n i o P I
{
c i l b u p ) ( e l g n a i r T { }
c i l b u p g n i r t s ( e l g n a i r T ) e m a n : ) e m a n ( e s a b { }
c i l b u p e d i r r e v o d i o v ) ( w a r D
{ g n i w a r D " ( e n i L e t i r W . e l o s n o C } 0 { e h t , " e l g n a i r T ; ) e m a N t e P }

/ / y t n i o P I . n o i t a t n e m e l p m I
c i l b u p e t y b s t n i o P
{
e g t { n r u t e r ; 3 }
}
}

6.3 Ex c r e c i i s ng S a h e p Hi r e a h c r y








6.4U r e d n sta d n ing Exp i l cit I r e t n fa e c Implem t n e a o i t n
n I r u o r r u c e t n de n o i t i n i f f o a r D I w3D , we w e r e forced to a n me t i s o s le metho d
Dra D 3 w () n i d r o er o t a d i o v c a l s n i h g w t i h h t e a t c a r t s b Dr ( w a ) h t e m d o d e n i f e d n i e h t
p a h S e s a b e s s a l c . l i h W e e r e h t s i g n i h t o n l b i r r o h y r w o g n w t i h this inte f r c a e defi t i n , n o i a
r o m e l a r u t a n d o h t e m m a n e o w uld i s l p m y be D ( w a r ):
/ / r o t c a f e R d o h t e m e m a n m o r f " D 3 w a r D " o t . " w a r D "
c i l b u p e c a f r e t n i
{
d i o v Draw();
}
If we were to ma e k such a chan e g , i h t s w u o ld req r i u e s u o t also up a d te our
m i pleme t n a o i t n of
c i l b u p c i t a t s d i o v D 3 w a r D I ( D 3 n I w a r D ) d 3 f t i
{
" ( e n i L e t i r W . e l o s n o C -> g n i w a r D D 3 w a r D I e l b i t a p m o c ; ) " e p y t
itf d 3 . r D aw ; ) (
}
Now, ass m u e you have def n i ed a new class named n i L e a h t t v i r e d es m o r f t e h a s b t c a r t
e p a h S
s s a l c d n a imple e m nts ID w a r 3D o b ( th of which o n w define an i e d i t n ca y l l named s b a t a r ct
o h t e m ) d :

e D pt. f o T I B J S , E S I e g a P 2 2 1
D 3 w a r D I
Dra I w n D 3 ().
Dra ( w )
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

/ / ? s m e l b o r P t I s d n e p e d . . .
c i l b u p s s a l c e n i L : , e p a h S w a r D I D 3
{
c i l b u p e d i r r e v o d i o v ) ( w a r D
{
g n i w a r D " ( e n i L e t i r W . e l o s n o C a e n i l ... ; ) "
}
}

5 . 6 In r e t f e c a as Polymor h p ic A e g nt
E p x t i c i l in a f r e t ce m i l p e e m nta i t n o can also be v r e y p l e h f l u h w ene e v r o y u r a e
m i ple n i t n e m g a n r e b m u f o e t n i r a f c s e t a h t ha p p en to contain ide t n i l a c members. o F r
example, assume o y u wi h s to c e r ate a c a l ss that m i ple n e m ts a l l t e h fo l l owing new
in e t rface types:
/ / e e r h T s e c a f r e t n i h c a e e n i f e d y l l a c i t n e d i d e m a n s d o h t e m .
c i l b u p e c a f r e t n i
{
d i o v ; ) ( w a r D
}

c i l b u p e c a f r e t n i r e t n i r P o T w a r D I
{
i o v d ; ) ( w a r D
}
f I o y u wish o t b l i u d a c s a l s n m a ed S I r e p u m g a e t a h t s t r o p p u s c i s a b g n i r e d n e r ( w a r D I , ) 3D
n i r e d n e r g (ID w a r 3D , ) as l l e w s a p i r n i t ng e s r i v s e c ( P o T w a r D I r e t n i r), the n o ly way t o
o r p v d i e unique m i plemen a t
l p m i e : n o i t a t n e m
/ / t o N g n i v i r e d m o r f , e p a h S t u b l l i t s g n i t c e j n i a e m a n h s a l c .
c i l b u p s s a l c e g a m I r e p u S : D 3 w a r D I , r e t n i r P o T w a r D I , w a r D I
{
d i o v ID w a r D . r ( w a )
{ * / c i s a B g n i w a r d c i g o l . / * }

d i o v ID w a r o T P t n i r e D . r r w a ()
{ * / r e t n i r P c i g o l . / * }

d i o v ID w a r D 3 . w a r D ) (
{ * / D 3 g n i r e d n e r c i g o l . / * }
}

6.6 Bu l i ding I r e t n fa e c Hi r e a c r hies
To o c ntin e u our in e v s i t g t a o i n of crea i t g n u c stom inte f r aces, le t s examine the t p o ic
of int r e f c a e hier r a c i h s e . Just as a class a c n r e s ve s a a e s a b class to other c a l s s es (whi h c
can in u t rn u f c n o i t n s a e s a b s e s s a l c o t t e y n a h t o er s a l c s , ) t i s i s s o p i e l b o t bu l i d
h n i e t i r a e c n s p i h s n o i t a l e r g n o m a n i ter e c a f s. As you i m ght x e p c e , t the t p o most

e D pt. f o T I B J S , E S I e g a P 3 2 1
w a r D I
i t o s n for each method is to u e s explic t i in e t a f r e c
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

interface e d fines a general beh v a r o i , wh l i e the m s o t derived in e c a f r e t de s e n i f m r o e
c i f i c e p s v a h e b i s r o . o T , e t a r t s u l l i r e d n o p e h t g n i w o l l o f e c a f r e t n i : y h c r a r e i h
/ / e h T e s a b e c a f r e t n i .
e c a f r e t n i c i l b u p






{ d i o v ; ) ( r e d n e R }
Now, if a class wi h s ed to support a e ch be a h v o i r e p x ressed in th s i interface h e i a r rchy, it
e d r v i e r f om the h t n - t s o m interfa e c (I e M t l i F a e d n e R er in this case). n A y me h t ods
def n i d e y b h t e b s a e f r e t n i a ( e c s) a e r autom t a i l l a c y carr e i d into h t e d f e i i n t n o i . o F r
exa e l p m :
/ / s i h T s s a l c s t r o p p u s , e l b a w a r D I , e l b a t n i r P I d n a r e d n e R e l i F a t e M I .
m I r e p u S s s a l c c i l b u p e g a : r e d n e R e l i F a t e M I
{
c i l b u p d i o v ) ( w a r D
c i s a B " ( e n i L e t i r W . e l o s n o C { g n i w a r d ; ) " . c i g o l }

c i l b u p d i o v ) ( t n i r P
{ w a r D " ( e n i L e t i r W . e l o s n o C o t ; ) " . r e t n i r p }

c i l b u p d i o v ) ( r e d n e R
r e d n e R " ( e n i L e t i r W . e l o s n o C { o t ; ) " . e l i f a t e m }
}
e H re is some samp e l s u g a e of e e x rcisi g n e h c a n i e t f r ace m o r f aSu e p rI g a m e e c n a t s n i :
/ / e s i c r e x E e h t s e c a f r e t n i .
c i t a t s d i o v ] [ g n i r t s ( n i a M
{
e g a m I r e p u S i s = w e n ; ) ( e g a m I r e p u S

/ / t e G . e l b a w a r D I
e l b a w a r D I w a r D f t i = ; i s ) e l b a w a r D I (
itf a r D w r D . w a ();

/ / w o N t e g , r e d n e R e l i F a t e m I h c i h w s e s o p x e l l a s d o h t e m p u
/ / e h t n i a h c f o e c n a t i r e h n i .
f i w a r D f t i ( s i ) r e d n e R e l i F a t e M I
{
r e d n e R e l i F a t e M I F M f t i = ; w a r D f t i ) r e d n e R e l i F a t e M I (
itf . F M R d n e r e ();
itf . F M P n i r ( t ; )
}
Con l o s e e R . d a L ( e n i ; )
}

e D pt. f o T I B J S , E S I e g a P 4 2 1
e l b a w a r D I
{ } ; ) ( w a r D d i o v

c i l b u p e c a f r e t n i e l b a t n i r P I : e l b a w a r D I
{ d i o v ; ) ( t n i r P }

c i l b u p e c a f r e t n i r e d n e R e l i F a t e M I : e l b a t n i r P I
o w uld
r a s g )
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7


6.7 I r e t n fa e c s U i s ng V . S NET,Un r e d sta d n ing h t e IConvertable I f r e t n c a e
h g u o h t l A c a f r e t n i e-ba d e s r g o r p a g n i m m s i a y r e v w o p e l u f r progr m a i m ng t h c e i n q , e u
l p m i eme g n i t n in e t rf c a es m y a enta l i a a e h t l y h am n u o t f o ty i p ng. Giv n e h t at r e t n i a f ces
are a nam d e e s t of abstr c a t mem e b rs, o y u wi l l be requ r i d e to t e p y in t e h stub o c d e
( d n a m i plem n e ta i t ) n o for h c a e e c a f r e t n i e m t d o h n o c a e h l c s a s that o p p u s s t r h t e
be i v a h or.
s A o y u d l u o w p o h e, l a u s i V S i d u t o 5 0 0 2 o d es s o p p u rt a v r s u o i o t o s l t t a h k a m e e h t a t sk f o
me i t n ng n i ter a f c s e le s s d r u b ensome . m u s s A e y u o w s i h to implement the
a C I r in e t rf e c a on a new class named Mi n a V i n . o Y u l l i w no i t ce h w en you complete
t p y ing h t e in a f r e t c e s a n me o ( r when y u o n o i t i s o p e h t u o m se cu s r or on h t e i t n e e c a f r
e m a n n i h t e c d o e , ) w o d n i w t e h fir t s le t t er is u d n e l r i e n d Once you e l e s ct p o o i t ns, you
w l l i see that Visual t S d u o i 20 5 0 has u b t l i generated st b u o c de w ( h t i in a named c d o e
r n o i g e ) for u o y o t update ( t o n e t t a h the defau t l implemen o i t a t n t r h ow s
Sys m e t . c x E p e t o i n ).
y h c r a r e i H e c a F I e c a p s e m a n
{
p c i l b u s s a l c n a V i n i M : r a C I
{
c i l b u p ) ( n a V i n i M
{
}

n o i g e r # r a C I s r e b m e M
c i l b u p d i o v ) ( e v i r D
{
w e n e h T " ( n o i t p e c x E d o h t e m r o n o i t a r e p o s i t o n ; ) " . d e t n e m e l p m i
}
n o i g e r d n e #
}
}
Now t a h t y u o have dr l l i ed into the s c e p ifics of b l i u di g n n a d i l p m eme i t n g n s u c tom
inter c a f es, h t e rem n i a der f o t e h chapter examines a number of prede i f ned interfac s e
contain d e wit i h n h t e .NET b s a e class libra i r es.
6.8 u B l i ding u n E me a r ble Ty e p s (I n E um r e able and
IEnumerator)
o T l l i s u tra e t the p o r cess of m i plement n i g existing . E N T int r e f s e c a , e l ts first x e amine
n E I um l b a r e e d n a u n E I m t a r e or . Assume you h v a e developed a c s a l s named
Gar g a e t a h t conta s n i a set of n i d u d i v i al Car typ s e (see Chapter 6) stored withi n
a S m e t s y .A r r ay:
/ / e g a r a G s n i a t n o c
c i l b u p s s a l c e g a r a G
{
e t a v i r p ] [ r a C r a c ; y a r r A

/ / l l i F h t i w e m o s r a C s t c e j b o n o p u . p u t r a t s

e D pt. f o T I B J S , E S I e g a P 5 2 1
e l p m i
a
t e h o r le f o
a t e s f o r a C . s t c e j b o
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

c i l b u p ) ( e g a r a G


= w e n , " y t s u R " ( r a C
= w e n , " r e k n u l C " ( r a C
= w e n , " y p p i Z " ( r a C
= w e n , " d e r F " ( r a C ; ) 0 3
}
}
e d I a , y l l t i would be o c v n n e e i nt to iterate over h t e Ga g a r e e j b o c t s su t i b ems us n i g h t e # C
for c a e h
/ / s i h T s m e e s e l b a n o s a e r . . .
c i l b u p s s a l c
{
c i t a t s d i o v ] [ g n i r t s ( n i a M ) s g r a
{
e g a r a G t o L r a c = w e n ; ) ( e g a r a G

/ / d n a H r e v o h c a e r a c n i e h t ? n o i t c e l l o c
r a C ( c n i ) t o L r a c









are a e l b o t ex s o p e a t n o c i d e n s b u items to t e h
: r e l l a c
/ / s i h T e c a f r e t n i s m r o f n i e h t r e l l a c
/ / t a h t e h t s ' t c e j b o s m e t i b u s n a c e b . d e t a r e m u n e
e c a f r e t n i c i l b u p
{
E I n r e m u o t a r G n E t e r e m u at ( r o ; )
}
s A o y u a c n see, t e h Get a r e m u n E t ( r o ) d o h t e m n r u t e r s a re r e f e c n e o t t e y a o n ther t n i e c a f r e
a n m d e S m e t s y . e l l o C t c E I . s n o i n a r e m u tor . Th s i in f r e t ace prov d i es the n i r f astructure o t
w o l l a the cal e l r to tr v a erse t e h inter a n l b o e j cts n o c tai d e n by t e h E I n l b a r e m u e-
e l b i t a p m o c co a t n i e n r:
/ / s i h T e c a f r e t n i s w o l l a e h t r e l l a c o t
/ / i a t b o n a s ' r e n i a t n o c s m e t i b u s .
e c a f r e t n i c i l b u p
{
l o o b t x e N e v o M ; ) ( / / e c n a v d A e h t l a n r e t n i n o i t i s o p f o e h t . r o s r u c

e D pt. f o T I B J S , E S I e g a P 6 2 1
{
y a r r A r a c = w e n ] 4 [ r a C ;
car r r A a 0 [ y ] 3 ) 0 ;
car r r A a 1 [ y ]
car r r A a 2 [ y ]
car r r A a 3 [ y ]
) 5 5 ;
3 ) 0 ;
r t s n o c u : t c
m a r g o r P
h c a e r o f
{
} 0 { " ( e n i L e t i r W . e l o s n o C s i g n i o g } 1 { , " H P M
. c P a N t e e m , . c u C e e p S r r d);
}
}
}
S d a y l , the o c mp l i er informs you that the a G r g a e a l c ss s e o d t o n imp e l me t n a method
d e m a n e G u n E t m a r e tor() . i h T s o h t e m d is formali d e z y b h t e IE m u n e l b a r e interf e c a ,
w i h ch is found u l rking w i h t i n h t e e t s y S m.C l l o c e t n o i s a n m s e pace. j b O e t c s h t at
s o p p u rt i h t s behavior a v d er i t se h t at e h t y
e l b a r e m u n E I
r o t a r e m u n E I
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

t c e j b o t n e r r u C { } ; t e g / / t e G e h t t n e r r u c m e t i d a e r ( - y l n o . ) y t r e p o r p
d i o v e R t e s / / t e s e R e h t r o s r u c e r o f e b e h t t s r i f . r e b m e m
}
f I y u o i w sh to up a d te the a r a G ge y t e p to support the e s t n i erfa s e c , u o y u o c ld take t e h
l g n o ro d a
o r p i v de customized e v r i s o s n f o t e G E m u n r e a ( r o t ) , Mo e N e v xt(), Cur n e r t, and R e s e t(),
e r e h t s i a r e l p m i s y a w . As the Sy m e t s .A a r r y ty e p a ( s l l e w s a ma y n oth r e p y t ) s e alrea y d
t n e m e l p m i s I e m u n E rable n a d I m u n E e o t a r r, o y u can simply d e l e a g e t t e h re e u q st to e h t
Sys m e t . r r A ay as f l l o ows:
s u i g n t s y S . m e o C i t c e l l o s n ;
. . .
c i l b u p s s a l c e g a r a G : e l b a r e m u n E I
{
a y a r r A . m e t s y S / / y d a e r l s t n e m e l p m i ! r o t a r e m u n E I
e t a v i r p ] [ r a C ; y a r r A r a c




= w e n , " e e F e e F " ( r a C
] 1 [ y a r r A r a c = w e n , " r e k n u l C " ( r a C
] 2 [ y a r r A r a c = w e n , " y p p i Z " ( r a C
] 3 [ y a r r A r a c = w e n , " d e r F " ( r a C , 0 3
}

c i l b u p r o t a r e m u n E I ) ( r o t a r e m u n E t e G
{
/ / n r u t e R e h t y a r r a s ' t c e j b o e m u n E I . r o t a r
e r t n r u a c r t e G . y a r r A n E u a r e m to ) ( r ;
}
}
c n O e you have u d p ated your g a r a G e type, you can now safely s u e the type within t e h C#
h c a e r o f t s n o c c u r t . u F rthermor , e gi e v n th t a the G u n E t e m t a r e o ( r ) met o h d a h s e e b n
n i f e d d e pub i l c , y l the o e j b ct user u o c d l also interact w t i h h t e e m u n E I r o t a r type:
/ / k r o w y l l a u n a M h t i w r o t a r e m u n E I .
r o t a r e m u n E I i = ; ) ( r o t a r e m u n E t e G . t o L r a c
i.M e v o N t x e ) ( ;
r a C r a C y m = ; t n e r r u C . i ) r a C (
} 0 { " ( e n i L e t i r W . e l o s n o C s i g n i o g } 1 { , " H P M , e m a N t e P . r a C y m ; ) d e e p S r r u C . r a C y m
f I u o y o w u d l p e r e f r to e d i h e h t f c n u t a n o i l t i y of IE e m u n r b a le from the object v e l e , l
i s mply make
c i l b u p r o t a r e m u n E I ) ( r o t a r e m u n E t e G . e l b a r e m u n E I
{
/ / n r u t e R e h t y a r r a s ' t c e j b o . r o t a r e m u n E I
e r t n r u a c r t e G . y a r r A n E u a r e m to ) ( r ;
}

e D pt. f o T I B J S , E S I e g a P 7 2 1
) ( ;
d n a t n e m e l p m i h c a e e m d o h t y l l a u n a m . Wh l i e you r a e certainly e r f e t o
c i l b u p ) ( e g a r a G
{
y a r r A r a c = w e n ; ] 4 [ r a C
car r r A a 0 [ y ] 0 0 2 ,
0 9 ,
) 0 ;
) 0 ;
, 0 3 ; ) 0
0 ; )
use of e p x i l c t i interfa e c l p m i eme t n a i t n o :
C# d n a m m a r g o r P T E N . i g n 6 0 IS 1 6 7

6.9 u B l i ding C o l e n able t c e j b O s (IC o l neable)
s A u o y r a c e ll o r f m h C a t p er , 3 y S s O . m e t b c e j t s e n i f e d a r e b m e m d e m a n
( e n o l C e s i w r e b m e M ). s i h T d o h t e m
e m s i h t l l a c t o n o d s r e s u t c e j b O t t i s a ( y l t c e r i d d o h i s protected ; ) ho e w ver, g a i e v n obje t c
a c y a m l l thi s method it e s l f u d ring the cl g n i n o p o r c s e s. o T , e t a r t s u l l i s a sume you e v a h a
class named i o P nt :
/ / A s s a l c d e m a n . t n i o P
c i l b u p s s a l c t n i o P
{
/ / c i l b u P r o f y s a e . s s e c c a
c i l b u p t n i , x ; y
c i l b u p t n i ( t n i o P , x t n i ) y { x . s i h t = ; x y . s i h t = } ; y
c i l b u p n i o P } { ) ( t

/ / e d i r r e v O . ) ( g n i r t S o T . t c e j b O
c i l b u p e d i r r e v o g n i r t s ) ( g n i r t S o T
n r u t e r { X " ( t a m r o F . g n i r t s = ; } 0 { Y = , " } 1 { , x y ; ) }
}
i G ven h w at you alre d a y know about refer n e ce ty e p s and value types (Chapter 3), you
are a a w re
t a h t if u o y assi n g o e n refer n e ce v r a i b a le o t a o n ther, you h v a e two referen s e c
o p in n i t g to t e h a s me o c e j b t in mem y r o . Thu , s the fo n i w o l l g ass g i nm n e t o e p ra i t n o
P i o nt o t c e j b on h t e ; p a e h mo i d f c i t a ions s u ing
h t i e er r e f e r e e c n a t c e f f e h t sa e m ob t c e j on h t e a e h p:
] [ g n i r t s ( n i a M d i o v c i t a t s s g r a )
{
/ / o w T s e c n e r e f e r
t n i o P 1 p = w e n , 0 5 ( t n i o P ; ) 0 5
t n i o P 2 p = ; 1 p
x . 2 p = ; 0
Con l o s e r W . t i e e n i L ( ) 1 p ;
Con l o s e r W . t i e e n i L ( ) 2 p ;
}
e h W n you i w sh o t equip your cust m o t e p y s to p p u s ort h t e b a y t i l i to ret n r u an iden c i t al
y p o c of
t i self to t e h l a c e l r, u o y may i l p m em n e t t e h t s n a d d r a ICl e n o a l b e t n i e f r c a e. s i h T p y t e
s e n i f e d a e l g n i s e m t d o h n e m a d Cl e n o () :
c i l b u p e c a f r e t n i e l b a e n o l C I
{
t c e j b o ; ) ( e n o l C
}
u o i v b O s , y l h t e l p m i e e m ntation of the n o l C ( e ) me h t od varies e b w t een obj c e ts. Ho e v e w r ,
e h t
t n i o a new t c e j b o n i sta , e c n d n a n r u t e r t i o t h t e s u er. To u l l i s , e t a r t po e d n r h t e f l o lo i w ng
p u date to th e n i o P t class:
/ / e h T t n i o P w o n s t r o p p u s e n o l c " - " . y t i l i b a

e D pt. f o T I B J S , E S I e g a P 8 2 1
s i d e s u o t n i a t b o a s a h l l w o co y p of the t n e r r u c t c e j b o .
resu t l s in o w t references to t e h a s me
o t e m a s ! t c e j b o
c i s a b y t i l a n o i t c n u f s d n e t o t be h t e m a s e: y p o C h t e va s e u l of your b m e m r e v r a a i b s e l
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

c i l b u p s s a l c t n i o P : e l b a e n o l C I
{
c i l b u p t n i , x ; y
c i l b u p { ) ( t n i o P }
c i l b u p t n i ( t n i o P , x t n i ) y { x . s i h t = ; x y . s i h t = } ; y

/ / n r u t e R a y p o c f o e h t t n e r r u c . t c e j b o
c i l b u p t c e j b o ) ( e n o l C
{ n r u t e r } ; ) y . s i h t , x . s i h t ( t n i o P w e n

c i l b u p e d i r r e v o g n i r t s ) ( g n i r t S o T
n r u t e r { X " ( t a m r o F . g n i r t s = ; } 0 { Y = , " } 1 { , x y ; ) }
}
n I this way, you can create e a x ct sta d n -alone o c p e i s of the Po n i t y t pe, as illustrated
y b h t e fol
] [ g n i r t s ( n i a M d i o v c i t a t s ) s g r a
{
/ / e c i t o N ) ( e n o l C s n r u t e r a c i r e n e g t c e j b o e p y t .
/ / u o Y t s u m m r o f r e p n a t i c i l p x e t s a c o t n i a t b o e h t d e v i r e d . e p y t
t n i o P 3 p = w e n , 0 0 1 ( t n i o P ; ) 0 0 1
t n i o P 4 p = ; ) ( e n o l C . 3 p ) t n i o P (

/ / e g n a h C x . 4 p h c i h w ( l l i w t o n e g n a h c . ) x . 3 p
x . 4 p = ; 0

/ / t n i r P h c a e . t c e j b o
Con l o s e r W . t i e e n i L ( ) 3 p ;
Con l o s e r W . t i e e n i L ( ) 4 p ;
}
e l i h W e h t c t n e r r u i m e l p m e t n a i t on f o i o P nt f t i s h t e b l i l, you can t s r a e m i l ne things just a
b t i .
Because t e h i o P nt t e p y does o n t contain referen e c t e p y variables, you u o c ld s m i p i l fy
t e h m i plementa i t on of t e h n o l C e() me h t d o as fol o l ws:
c i l b u p t c e j b o ) ( e n o l C
{
/ / y p o C h c a e d l e i f f o e h t t n i o P r e b m e m y b . r e b m e m
e r t n r u h t i w r e b m e M . s i se n o l C e();
}
e B , e r a w a , r e v e w o h t a h t if e h t Po n i t d i d c n i a t n o a y n f e r ere e c n ty e p me r e b m r a v ia l b e , s
r e b m e M -
wis l C e o ( e n ) l l i w y p o c the e r r e f e c n es to t s o h e objects (aka a sh l a l w o copy). If you i w s h
to sup o p rt
a true de p e co y p , you l l i w need to r c eate a n w e n i st n a ce of any refe e r nce type variab e l s
u d ring c e h t lon n i g p o r c s s e .

6.10 u B l i ding Comparable Obj c e s t (IComparable)

e D pt. f o T I B J S , E S I e g a P 9 2 1
g n i w o l : e d o c
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

The y S s . m e t I a p m o C r b a le interf e c a p s ecifi s e a be v a h ior t a h t al o l ws n a ob e j ct to e b
sorted a b s d e n o
e m o s d e i f i c e p s y e k . He e r is the formal e d fin t i i n o :
/ / s i h T e c a f r e t n i s w o l l a n a t c e j b o o t y f i c e p s s t i
/ / n e e w t e b p i h s n o i t a l e r r e h t o e k i l s t c e j b o .
e c a f r e t n i c i l b u p { e l b a r a p m o C I
t n i t c e j b o ( o T e r a p m o C ; ) o
}
Le t s assume u o y ha e v d p u at d e t e h a C r a l c ss to a m n i t i a n an t n i ern l a ID b m u n e r
r ( epresented y b a simple integer a n m d e a c rID) h t at can be e s t via a construc o t r
par m a et r e and ma u p i n lated
i s u ng a new property a n m d e D I . r e H e e r a e h t l e r e t n a v p u d t a es to t e h a C r t p y e :
p c i l b u s s a l c r a C
{
. . .
e t a v i r p t n i ; D I r a c
c i l b u p t n i D I
{
t e g { n r u t e r ; D I r a c }
t e s { D I r a c = ; e u l a v }
}
c i l b u p g n i r t s ( r a C , e m a n t n i , p S r r u c t n i ) d i
{
d e e p S r r u c = ; p S r r u c
e m a N t e p = ; e m a n
D I r a c = ; d i
}
. . .
}
e j b O ct e s u rs i m ght create an r r a ay of a C r p y t es as fo l l o s w :
] [ g n i r t s ( n i a M d i o v c i t a t s ) s g r a
{
/ / e k a M n a y a r r a f o r a C s e p y t .
] [ r a C s o t u A y m = w e n ; ] 5 [ r a C
] 0 [ s o t u A y m = w e n , " y t s u R " ( r a C , 0 8 ; ) 1
] 1 [ s o t u A y m = w e n , " y r a M " ( r a C , 0 4 ; ) 4 3 2
] 2 [ s o t u A y m = w e n , " r e p i V " ( r a C , 0 4 ; ) 4 3
] 3 [ s o t u A y m = w e n , " l e M " ( r a C , 0 4 ; ) 4
] 4 [ s o t u A y m = w e n , " y k c u h C " ( r a C , 0 4 ; ) 5
}
s A o y u rec , l l a h t e Sy e t s m A . r r ay c s s a l defi s e n a a t s tic m t e hod named o S t r (). W e h n u o y
e k o v n i
this met d o h n o an array of intrinsic t p y es (i t n , h s ort, i r t s ng, c t e .), o y u r a e able t o
t r o s h t e items in the a r r ay n i nu e m ric l a / h p l a a e b tic o d r er as t e h se t n i rinsic data
ty e p s m e l p m i e t n p m o C I a b a r le . , r e v e w o H t a h w if u o y e r e w o t d n e s n a y a r r a f o
a C r types in o t e h t t r o S () o h t e m d s a fo l l o ? s w

e D pt. f o T I B J S , E S I e g a P 0 3 1
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

/ / t r o S y m ? s r a c
Arr . y a S t r o m ( y o t u A s);
f I u o y n u r s i h t , t s e t u o y d l u o w d n i f t a h t n a r A g x E t n e m u c o i t p e n n o i t p e c x e s i n w o r h t y b e h t
e m i t n u r ,
h t i w e h t g n i w o l l o f : e g a s s e m t A t s a e l n o e t c e j b o u m st m i p e m e l nt . e l b a r a p m o C I n e h W
tom ty e p , s u o y can i l p m e e m nt I a r a p m o C ble o t w o l l a s y a r r a f o r u o y ty s e p to
e b d e t r o s . n e h W u o y h s e l f t u o e h t s l i a t e d f o ( o T e r a p m o C ), t i l l i w e b p u o t u o y o t e d i c e d
t a h w e h t e n i l e s a b f o e h t g n i r e d r o n o i t a r e p o l l i w e b . F r o t e h a C r ty e p , h t e e t n i rna l
I r a c D s m e e s o t be e h t t s o m lo l a c i g n a c : e t a d i d
/ / e h T n o i t a r e t i f o e h t r a C n a c e b d e r e d r o
/ / d e s a b n o e h t . D I r a C
c i l b u p s s a l c r a C : e l b a r a p m o C I
{
. . .
/ / e l b a r a p m o C I . n o i t a t n e m e l p m i
t n i . e l b a r a p m o C I C p m o a t c e j b o ( o T e r b o { ) j
r a C p m e t = ; j b o ) r a C (
D I r a c . s i h t ( f i > ) D I r a c . p m e t
n r u t e r ; 1
D I r a c . s i h t ( f i < ) D I r a c . p m e t
n r u t e r - ; 1
e s l e
n r u t e r ; 0
}
}

6. 1 1 Exp o l ring T e h I f r e t n a e c s f o h t e System.Co l l t c e o i ns Na e m spac e
The o m st pr i m i tive o c n i a t e n r construct o w uld have to be u o r good r f ie d n Sy m e t s . a r r A y.
s A u o y hav e
, g n i s r e v e r , g n i t r o s c e l r a , g n i a d n enumerat g n i ). , r e v e w o H h t e s l p m i e Ar a r y class s a h a
u n m r e b of li t i m a i t ons, most o n ta y l b t i
or cl r a e t i ems. When you n e e d to c n o tain t p y es in a o m e r f i x e l ble n i a t n o c e , r o y u a m y
h s i w o t e g a r e v e l e h t ty s e p n i f e d ed with n i e h t . m e t s y S C c e l l o t n o i s namespace (or as
dis u c s s ed in Chapter 10, the Sy . m e t s C e l l o cti n e G . s n o e i r c namespace). The
Sys m e t . l o C e l c o i t ns a n m a p s e ce e d fin s e a u n mb r e of t n i erf c a es s ( m o e f o which o y u
a h v e
y t i r o j a m f o e h t n o i t c e l l o c s e s s a l c l p m i e n e m t t e h e s inte e c a f r s o t prov e d i a s e c c s o t e h t ir
t n o c e t n s. e l b a T 7-2 i g v s e a n w o d k a e r b f o h t e core o c l l ec i t on-ce t n ric r e t n i fa s e c .

Ta e l b 7-2. I e t n c a f r es f o s n o i t c e l l o C . m e t s y S
g n i n a e M n i e f i L
Defines e g n r e ic h c ar c a teris i t s c (e.g , .
n u o c t n a d th d a e r sa e f ty) o f r a co e l l ction
type.

e g a P 1 3 1
u o y u b i d l s u c
y d a e r l a n e e s n i r e t p a h C 3, s i h t c s s a l s e d i v o r p a b m u n er of e s r i v c s e ( , . g . e
does o n t n y d m a ically r s e ize it e s lf as o y u d d a
a r l a e y d m i plem n e ted duri g n the c u o rse of thi s ch p a e t ) r . s A o y u n a c s s e u g , a
Sy m e t s . e l l o C c o i t ns In e t rf c a e
IC e l l o c o i t n



e D pt. f o I B J S , E S I T
I 6 0 S 1 6 7

e n i f e D s s d o h t e m o t p p u s ort t e h
i r a p m o c s n o f o o e j b cts r o f eq a u y t i l .
w o l l A s an object to re e r p sent it s
o c nte s t n n i s u g na e u l a v / e m pairs.
s e t a r e m u n E e h t t n o c e s t n of a y t pe
i t r o p p u s ng D I ict o i nary.
Re u t rns t e h IE r e m u n ator r e t n i c a f e for a
n e v i g e j b o t c .
Genera l l y su p p orts r o f e c a h-style
t i era i t on f o b u s types.
s n r u t e R e h t a h sh c d o e o f r the
l p m i eme g n i t n e p y t g n i s u a u c o t s z i m ed
h s a h a i r o g l h t m.

( s i h T in e c a f r e t is new to N . ET . 2 0 ) .
s e n i b m o C h t e o i t c n u f n y t i l a f o
ICo a p m rer d n a h s a H I C d o e o r P vi e d r o t
w o l l a objec s t to be co p m ared in a
h s a h -co e d - p m o c a e l b i t r e n n a m . e ( g.,
f i h t e s t c e j b o a e r d e e d n i q e u l a , h t ey
u m st a o s l n r u t e r h t e sa e m s a h h c d o e
va u l e).
e d i v o r P s e b hav o i r to add, e r move, and
index ite s m in a list of b o je s t c . l A so,
this inter a f ce e d fines me b m ers o t
e d ter i m e n t e h w her the impl m e e i t n ng
l l o c ec n o i t t p y e s i read-on y l a d n / r o a
i f e x d-s z i e contai e n r.

6.11 u B l i ding a Cu o t s m co t n i a r e n Retrofi t t ing e h t Car Ty e p with
Dele a g e t s
, y l r a e l C e h t s u o i v e r p i S m D e l p el t a g e e e a x mple w s a int d e d n e to e b pu e r y l u l l i st a r i t e v
h t at e r e h t d l u o w e b o n n o s a e r o t d l i u b a l e d e t a g e i s mp y l o t add two
b m u n ers. , y l l u f e p o H h , r e v e w o i h t s exa l p m e e d y m s i t f e i s h t e pr e c o ss f o i k r o w ng t i w h
de e l gate t s e p y . o T e d i v o r p a e r o m e r a c i t s i l e s u of e t a g e l e d s e p y t , le s t t i f o r t e r r u o
a C r e p y t o t e s nd t e h p x E l e d o d d n a b A o B o T t u low s n o i t a c i f i t o n g n i s u T E N . s e t a g e l e d
r s u c a n a h t r e h t a t a c m o ll a b k c int a f r e ce. d n o y e B o n r e g n o l g n i t n e m e l p m i
E e n i g n E I ve t n s,
e n i f e D e h t A t u o b T l B o ow d n a p x E lo e d d g e l e d a e t s.
e D c r a l e member a v riables of each delegate type in the a C r s a l c s.
e t a e r C r e p l e h n u f cti n o s n o e h t Car that a l l ow t e h a c l l er to sp c e ify t e h met s d o h
e h t l e d e e t a g me b m er b a i r a v le . s

cor t c e r
d n a # C g n i m m a r g o r P T E N .

IE l a u q i m o C y t p r a er

ID i t c i o r a n y

r o t a r e m u n E y r a n o i t c i D I

IE e m u n r b a le

IE e m u n r o t a r

IH C h s a o o r P e d v d i r e



IK o C y e m e r a p r







IL s i t
in a n tu e r , giv n e
r e h e are the steps we wi l l e n ed to t k a e:



maintai d e n y b
e t a d p U t e h c c A e t a r e l e() met d o h to i e k o v n h t e e d le a g tes inv a c o i t n o i l st n u d r e t e h
c r i c m u a t s c n es. P d n o er the fo o l l w n i g u a d p t d e a C r class, wh c i h d a dresses t e h
r i f st thr e e o p ints:

e D pt. f o T I B J S , E S I e g a P 2 3 1
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

c i l b u p s s a l c r a C
{
/ / e n i f e D e h t e t a g e l e d . s e p y t
e t a g e l e d c i l b u p d i o v g n i r t s ( w o l B o T t u o b A ; ) g s m
c i l b u p e t a g e l e d d i o v d e d o l p x E g n i r t s ( ; ) g s m

/ / e n i f e D r e b m e m s e l b a i r a v f o h c a e e t a g e l e d . e p y t
e t a v i r p w o l B o T t u o b A ; t s i L d a e D t s o m l a
e t a v i r p d e d o l p x E ; t s i L d e d o l p x e

/ / d d A s r e b m e m o t e h t n o i t a c o v n i s t s i l g n i s u r e p l e h . s d o h t e m
c i l b u p d i o v o l B o T t u o b A n O w o l B o T t u o b A ( w ) d o h t e M t n e i l c
{ t s i L d a e D t s o m l a = ; d o h t e M t n e i l c }
c i l b u p d i o v d e d o l p x E ( d e d o l p x E n O ) d o h t e M t n e i l c
t s i L d e d o l p x e { = ; d o h t e M t n e i l c }
. . .
}
































e D pt. f o T I B J S , E S I e g a P 3 3 1
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

e d n e m m o c e R n o i t s e u q d s :
1. n i d e s u e r a y e h t y h w ? e c a f r e t n i n a s i t a h W # c ?.
2. a t n o c s i m a r g o r p # c a e t i r W i n inter e c a f .
3. e d s u o n o r h c n y s a d n a s u o n o r h c n y s n e e w t e b e t a i t n e r e f f i D ? e t a g e l e d a s i t a h W e l a g t w , e t i h
0 1 . s e l p m a x e m
4. c l a c o t m a r g o r p # c e t e l p m o c a e t i r W p p a g n i t i r w y b t s e r e t n i e l p m i s y a l p s i d d n a e t a l u r r p o ia e t
m a r g o r p f o d o h t e m e t a g e l e d h g u o r h t d e l l a c e b d l u o c h c i h w s d o h t e m m g n i n i .
5. a m s t i t s i L ? e c n a t i r e h n i e l p i t l u m t r o p p u s o t h c a o r p p a e t a n r e t l a e h t s i h c i h W j a e f r o tur . s e
6. a x e n a h t i w , n i a l p x e y l f e i r B n i t i c i l p x e , e l p m t a t n e m e l p m i e c a f r e t n o i
7. t p e c c a o t # C n i m a r g o r p a e t i r W e h t m r o f r e p d n a s g n i r t s o w t f l o l w o i g n a r e p o t ? s n o i
i) n i r t s o t 2 g n i r t s y p o c g 3
ii) g n i r t s k c e h c s d n e 1 i w t G G N E h or o n t. . 3 g n i r t s n i a r e t c a r a h c h c r a e s , e u r t s i t i f I
n i ) i i i S Y S . t i y a l p s i d d n a 6 n o i t i s o p t a 2 g n i t s n i U T V t r e s T M E







































e D pt. f o T I B J S , E S I e g a P 4 3 1
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

T I N U 7
t a g e l e D , s e c a f r e t n I k c a b l l a C e , s a s t n e v E d n

g n i d n a t s r e d n U 1 . 7 k c a b l l a C a f r e t n I c , s e
7 g n i d n a t s r e d n U 2 . e h t T E N . e t a g e l e D T , e p y
3 . 7 s r e b m e M f o S m e t s y . t s a c i t l u M , e t a g e l e D The t s e l p m i S P s s o ible
e t a g e l e D , e l p m a x E g n i d l i u B e r o M a e t a r o b a l E e t a g e l e D Ex e l p m a
4 . 7 g n i d n a t s r e d n U As s u o n o r h c n y a g e l e D t s e ,
5 . 7 i d n a t s r e d n U ng ( d n a s U i ) g n Ev s t n e
6 . 7 h T e s e c n a v d A Keyw s d r o f o , # C A l a t a C og f o # C y e K w d r o s
7 . 7 g n i d l i u B a m o t s u C In e d x , r e A n o i t a i r a V f o e h t s r a C e d n I x r e l a n r e t n I
n o i t a t n e s e r p e R f o T e p y r e x e d n I
8 . 7 g n i s U # C e d n I x r e from B V T E N . . g n i d a o l r e v O , s r o t a r e p o The r e t n I n l a
n o i t a t n e s e r p e R f o l r e v O o d a g n i Op t a r e o s r , e h T l a n r e t n I s n o i t a t n e s e r p e R
f o s m o t s u C r e v n o C i s n o R s e n i t u o
































e D pt. f o T I B J S , E S I e g a P 5 3 1
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

7.1 Unde s r tanding C l l a b c a k I t n erf c a es
s A u o y v a h e e s en in the r p ev u o i s h c apte , r interfaces de i f ne a beha o i v r that may b e
support d e by vari u o s ty e p s n i your y s stem. d n o y e B using n i terfa e c s to est l b a ish
m y l o p h p r o ism, n i terfa e c s may also be u e s d s a a c l l a back mec a h nism. This
technique enables objects to engage in a two-way c v n o ersa i t on us g n i a com n o m
To t s u l l i rate h t e use of ca l l b c a k f r e t n i a e c s, t e l s p u date the now
fam l i iar Car e p y t n i such a way t a h t t i is a l b e to inf r o m t e h a c l l er wh n e t i is u o b a t to
p x e lode (the curre t n p s e d e is 10 l i m s e below t e h a m i x mum p s e e d) and s a h
e d o l p x e d e h t ( r r u c e t n d e e p s is at r o v o b a e h t e x a m imum s e e p d). The
d n a r i e c e v e e s e h t e v w s t n e ill c a f e b ilitat t i w d e h a cust i m o nte f r ac e named
I i g n E n E e e v t n s:
/ / e h T k c a b l l a c . e c a f r e t n i
c i l b u p e c a f r e t n i s t n e v E e n i g n E I
{
d i o v g n i r t s ( w o l B o T t u o b A ; ) g s m
d e d o l p x E d i o v g n i r t s ( ; ) g s m
}

v E e t n t n i erfac s e are n t o ty c i p a l l y im l p emented dir c e l t y by h t e object dir c e tly
t n i e e r sted in r e c e iv n i g t e h ev n e ts, u b t a r h t er by a e h lper b o j c e t a c l l ed a sink obje t c .
Car p y t e in s i h t a c se) i w ll make ca l l s on h t e n i s k e d n u r
e h t r p o r p p a e t a i s e c n a t s m u c r i c . s s A u e m e h t i s nk s s a l c s i c l l a ed r a C E n e v t i S nk , whi h c
m i s ply r p t n i s u o t t e h c n i i m o ng mes a s ges to the o c nso e l . y e B o d n th s i point, our sin k
l l i w s l a o i a m ntain a string h t at id t n e ifies its friendly name:
/ / r a C t n e v e k n i s .
c i l b u p s s a l c C k n i S t n e v E r a : s t n e v E e n i g n E I
{
e t a v i r p g n i r t s ; e m a n
} { ) ( k n i S t n e v E r a C c i l b u p
c i l b u p g n i r t s ( k n i S t n e v E r a C a N k n i s e m )
{ e m a n = ; e m a N k n i s }

c i l b u p d i o v g n i r t s ( w o l B o T t u o b A ) g s m
{ } 0 { " ( e n i L e t i r W . e l o s n o C : g n i t r o p e r , " } 1 { , e m a n ; ) g s m }
c i l b u p d i o v g n i r t s ( d e d o l p x E ) g s m
{ } 0 { " ( e n i L e t i r W . e l o s n o C : g n i t r o p e r , " } 1 { , e m a n ; ) g s m }
}
Now t a h t u o y e v a h a s k n i e j b o ct a h t t implem t n e s h t e e n e v t i t n e a f r ce, y u o r ne t x a t sk
a referen e c to this s n i k into t e h Car p y t e. The a C r o h s d l n o to t e h reference
d n a m s e k a a c lls b c a k on t e h i s nk h w en approp t a i r e. n I r e d r o o t w o l l a h t e Car to
n i a t b o a e c n e r e f e r o t the k n i s , we w l i l e n ed o t d a d a pub i l c he p l er mem r e b to the
a C r type h t at e w will call A e s i v d () . L k i w e s i , e if the caller wishes to deta h c from t e h
ev n e t o s c r u e, t i m y a ca l l o n a th r e helper met d o h n o t e h Car e p y t nam d e d a n U v ( e s i ).
n i F a l l y, n i or e d r to allow h t e c l a ler to r g e ister mu t l ip e l ev n e t si k n s (for the u p r o p ses


e D pt. f o T I B J S , E S I e g a P 6 3 1
set of me b m ers.
y t i l i b a o t d n e s
T e h e s nder of t e h ev n e ts (the
s i to pass
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

h t e Car w o n mainta n i s an r A r i L y a st o t t n e s e r p e r h c a e s t u o g n i d n a t

/ / s i h T r a C d n a r e l l a c n a c w o n e t a c i n u m m o c
/ / g n i s u e h t s t n e v E e n i g n E I . e c a f r e t n i
c i l b u p s s a l c
{
/ / e h T t e s f o d e t c e n n o c . s k n i s
t s i L y a r r A s k n i S t n e i l c = w e n ; ) ( t s i L y a r r A

/ / h c a t t A r o t c e n n o c s i d m o r f e h t e c r u o s f o . s t n e v e
c i l b u p d i o v n E I ( e s i v d A s t n e v E e n i g ) k n i s
{ l c e i t n k n i S s d d A . s ( i ; ) k n }

c i l b u p d i o v s t n e v E e n i g n E I ( e s i v d a n U ) k n i s
{ c e i l t n k n i S s m e R . v o e i s ( k n ; ) }
. . .
}
o T y l l a u t c a d n e s e h t , s t n e v e s t e l e t a d p u e h t l e c c A . r a C e e t a r () me h t od o t t a r e t i e r e v o h t e
t s i l f o s n o i t c e n n o c d e n i a t n i a m y b e h t L y a r r A s i t and fire the co r r ect o n tifi a c i t n o w e h n
p a propriate (note the Car cl s a s now mainta n i s a e l o o B an r e b m e m e l b a i r a v d e m a n
a e D s I r a c d o t t n e s e r p e r e h t s e n i g n e : ) e t a t s
/ / e c a f r e t n I - d e s a b t n e v e ! l o c o t o r p
s s a l c
{
. . .
/ / s I e h t r a c e v i l a r o ? d a e d
l o o b a e D s I r a c ; d

c i l b u p d i o v t n i ( e t a r e l e c c A ) a t l e d
{
/ / f I e h t r a c s i , ' d a e d ' d n e s d e d o l p x E t n e v e o t h c a e . k n i s
if( r a c I e D s d a )
{
s t n e v E e n i g n E I ( h c a e r o f e n i ) s k n i S t n e i l c
, y r r o S " ( d e d o l p x E . e s i h t r a c s i
}
e s l e
{
d e e p S r r u c = + ; a t l e d

/ / d n e S w o l B o T t u o b A . t n e v e
0 1 ( f i = = d e e p S x a m - ) d e e p S r r u c
{
s t n e v E e n i g n E I ( h c a e r o f e n i ) s k n i S t n e i l c
C " ( w o l B o T t u o b A . e ar l u f e ! y d d u b n o G na b w o l ; ) " !
}


e D pt. f o T I B J S , E S I e g a P 7 3 1
of mult c i asting , )
o c n n i t c e n o :
r a C
r a C
d a e d . . .");
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

d e e p S r r u c ( f i = > ) d e e p S x a m
d a e D s I r a c = ; e u r t
e s l e
" ( e n i L e t i r W . e l o s n o C \ S r r u C t d e e p } 0 { = , " ; ) d e e p S r r u c
}
}
e r e H s i m o s e t n e i l c - e d i s , e d o c w o n g n i k a m u e s f o a a c l a b l ck in e t f r c a e o t l n e t s i o t h t e
a C r events:
/ / e k a M a r a c d n a n e t s i l o t e h t s t n e v e .
c i l b u p s s a l c p p A r a C
{
] [ g n i r t s ( n i a M d i o v c i t a t s ) s g r a
{
* * * " ( e n i L e t i r W . e l o s n o C t n e v e s a s e c a f r e t n I * * s r e l b a n e ; ) " * * * * *
r a C 1 c = w e n , " g u B g u l S " ( r a C , 0 0 1 ; ) 0 1

/ / e k a M k n i s t c e j b o .
k n i S t n e v E r a C k n i s = w e n ; ) ( k n i S t n e v E r a C

/ / s s a P e h t r a C a e c n e r e f e r o t e h t k n i s .
; ) k n i s ( e s i v d A . 1 c

/ / d e e p S p u s i h t ( l l i w r e g g i r t e h t ) s t n e v e .
( r o f t n i i = ; 0 i < ; 0 1 ) + + i
; ) 0 2 ( e t a r e l e c c A . 1 c

/ / h c a t e D m o r f t n e v e e c r u o s .
; ) k n i s ( e s i v d a n U . 1 c
; ) ( e n i L d a e R . e l o s n o C
}
}

7.2 Unde s r tanding e h t N . ET Delega e t Type
e r o f e B y l l a m r o f d f e n i i g n . E N T del g e a e t s, t e l s ga n i a b t i of e p rs e p ct e v i . i H storical y l
sp a e king, t e h Windows API m e k a s f q e r ue t n e s u of C-style n u f ction o p i t n ers to r c a e te
n e titi s e e t rmed c l l a back u f c n i t ons or simply b l l a c acks . Usi g n a c l l c a b ks, programm s r e
we e r b a le to config r u e e n o func i t n o o t report back to ( l a c l b c a k) an h t o er n u f c i t on in
h t e c i l p p a a n o i t . e h T o r p blem i w th s a t n r a d d C- t s yle ca a b l l ck f c n u tions s i t a h t y e h t
t n e s e r p e r e l t t i l e r o m n a h t a raw ad r d ess in me o m ry. , y l l a e d I ca l l b c a ks l u o c d e b
d e r u g i f n o c o t e d u l c n i a n o i t i d d al e p y t - e f a s a m r o f n i t o i n u s ch as t e h num r e b f o ( n a d ty e p s
f o ) p r a ame s r e t and h t e re u t rn va u l e (if a y n ) of the method point d e o t . , y l d a S i h t s s i t o n
the s a c e in t a r d t i ional ca l l back functio s n , and, as you a m y u s spec , t can th r e f e r o e be a
r f q e u n e t s r u o e c of g u b , s hard cras s e h , a d n ot e h r u r n e m i t disasters. Ne e v rtheless,
a c l l a b cks r a e useful enti s e i t . n I e h t N . ET r F a , k r o w e m s k c a b l l a c e r a t s ill s o p s e l b i , and
their u f nc i t onality is c a comp i l shed in a much safer and more obj c e t r o e i t n e d

e D pt. f o T I B J S , E S I e g a P 8 3 1
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

manner us n i g e d legates. n I e e s s nc , e a l e d egate is a p y t e- a s fe o j b ect t a h t n i o p ts o t
a h t o n er h t e m od (or po s s y l b i multip e l e m t o h ds) n i t e h appli a c t n o i , h w ich can be
k o v n i ed at a la e t r t m i e . i c e p S fi a c y l l s e p aking, a de g e l ate t e p y a m i t n i a ns th e e r
t r o p m i a t n s e c e i p f o n i fo a m r tion:
e h T m a n e f o the t e m o h d on h w c i h it k a m es calls
e h T argu e m nts f i ( any) of i h t s me o h t d
e h T n r u t e r u l a v e f i ( ) y n a of i h t s me o h t d

Defin n i g a D e l e gate in C#
n e h W u o y t n a w o t e r c a e t a l e d e t a g e in # C , you make s u e of h t e de g e l ate
o w y e k rd. h T e a n me of u o y r
def n i e h t e delegate o t mat h c t e h si a n g ture of the me h t d o t i wi l l o p int to. For example,
s s a ume you i w sh to u b ild a delegate a n med i B n O y r a p
n r u t e r s an n i t e g e r and takes two n i tegers as p n i ut par m a eters:
/ / s i h T e t a g e l e d n a c t n i o p o t y n a o h t e m , d
/ / g n i k a t o w t s r e g e t n i d n a g n i n r u t e r n a
/ / r e g e t n i .
c i l b u p e t a g e l e d t n i t n i ( p O y r a n i B , x t n i ; ) y
h W en h t e # C compiler processes del g e ate types, it u a t m o atical y l ge e n rates a e s aled
ing f o r m s y S t M . m e ul a c i t s D t l e e a g te . This s a l c s (in con u j nction i w th its base
l c a s s , Sys . m e t D g e l e ate) o r p i v des the n c e ess y r a in r f astructure for t e h e d legate to h l o d
n o to the i l st f o met o h ds to e b i e k o v n d
Bin O y r a p del a g e te usi g n l i a d s x e . m e

7.3 e h T System M . ulticastDe a g e l e t and System.D l e e a g e t Base C s a l ses
, o S e h w n u o y u b ild a t e p y using the C# l e d e a g te y e k w d r o , u o y i d n ir c e l t y e d clare a
cl s s a type that derives from y S m e t s . l u M t a c i s D t e l e g t a e . s i h T class p v o r id s e
des e c nd n e s t i w th access o t a list that
maintai d e n by the del g e ate t e p y , as we l l as e s veral d d a i t i onal methods (and a few
overl a o d d e ope a r tors) to interact t i w h t e h invoca i t on i l st. Here are some e s l c e t
mem e b rs of Sy e t s m. l u M t s a c i tD g e l e ate :
[Se a i r l a z i l b ] e
c i l b u p t c a r t s b a s s a l c e t a g e l e D t s a c i t l u M e t a g e l e D :
{
/ / s d o h t e M
c i l b u p d e l a e s e d i r r e v o ] [ e t a g e l e D i t a c o v n I t e G ; ) ( t s i L n o

/ / s r o t a r e p o d e d a o l r e v O
c i t a t s c i l b u p l o o b r o t a r e p o e t a g e l e D t s a c i t l u M ( = =
c i t a t s c i l b u p l o o b r o t a r e p o e t a g e l e D t s a c i t l u M ( = !

/ / s d l e i F
e t a v i r p r t P t n I ; t n u o C n o i t a c o v n i _
t c e j b o e t a v i r p ; t s i L n o i t a c o v n i _
}


e D pt. f o I B J S , E S I T
l e d egate can e b what v e er u o y des r i e. How v e er, you must
t a h t a c n n i o p t to a y n meth d o that
l c a s s e d r v i
at a later time. r o F l p m a x e e, f i u o y n i m a x e e h t e
n o c ta s n i t e h d a r d es e s s of t e h met d o h s
, 1 d e t a g e l e D t s a c i t l u M ; ) 2 d
, 1 d e t a g e l e D t s a c i t l u M ; ) 2 d







e g a P 9 3 1
C# d n a o r P T E N . m m a r g i g n I 6 0 S 1 6 7

y S stem.MulticastDel g e ate obtains i d d a tio l a n u f nct a n o i l y t i from its par t n e class,
y S stem.
Del a g e te . H r e e is a p r a i t al sn p a s o h t of t e h class defin o i t i n:
S [ e l a i r a z i e l b , I s s a l C n f r e t a l C ( e c s a n I s r e t fa y T e c p t u A . e o l a u D )]
s s a l c t c a r t s b a c i l b u p e t a g e l e D : , e l b a e n o l C I e l b a z i l a i r e S I
{
/ / s d o h t e M
c i t a t s c i l b u p s m a r a p ( e n i b m o C e t a g e l e D ] [ e t a g e l e D ; ) s e t a g e l e d
c i l b u p c i t a t s e t a g e l e D e t a g e l e D ( e n i b m o C , a e t a g e l e D ; ) b
l a u t r i v c i l b u p ] [ e t a g e l e D ; ) ( t s i L n o i t a c o v n I t e G
e t a g e l e D c i t a t s c i l b u p a g e l e D ( e v o m e R e t , e c r u o s e t a g e l e D ; ) e u l a v
e t a g e l e D c i t a t s c i l b u p e t a g e l e D ( l l A e v o m e R , e c r u o s e t a g e l e D ; ) e u l a v

/ / s r o t a r e p o d e d a o l r e v O
c i l b u p c i t a t s l o o b r o t a r e p o e t a g e l e D ( = = , 1 d e t a g e l e D ; ) 2 d
c i l b u p c i t a t s l o o b r o t a r e p o ( = ! e t a g e l e D , 1 d e t a g e l e D ; ) 2 d

/ / s e i t r e p o r P
c i l b u p o f n I d o h t e M d o h t e M { ; t e g }
c i l b u p t c e j b o t e g r a T { ; t e g }
}
No , w e r me e b m r that you i w l l n e v e r i d rectly e d r v i e r f om th s e e base cl s a ses a d n can
t p y c i ally o c ncern o y urself n o ly i w th h t e mem s r e b u c o d m n e t d e in b a T le 8-1.


Ta e l b 8-1. l e S e t c M m e b r e s of Sy m e t s . t l u M c D t s a e a g e l te /S e t s y m g e l e D . ate
d e t i r e h n I b m e M er g n i n a e M n i Li e f
Met o h d s i h T y t r e p o r p u t e r rn s
a s y S te e R . m f t c e l i e M . n o th I d o nfo e p y t
t a h t s t n e s e r p e r de s l i a t f o a s i t a t c
d o h t e m h t at s i t n i a m i a e n d by h t e
t a g e l e d e.
Tar e g t f I the t e m h d o to be called is def n i ed a t
the obj t c e level r ( at e h r h t n a a t s atic
method), r a T get r t a h t t c e j b o n a s n r u t e
r p e r s t n e s e h t e m d o h t e m a t n i a i d e n by
the de e l a g te. f I e h t value e r u t r d e n r f m o
e g r a T t l a u q e s , l l u n h t e me h t od to b e
l l a c d e is a sta i t c memb r e .
Com n i b e() n I C#, you trigger this meth d o usin g
t e h over o l ad d e += r o t a r e p o s a a
r o h s t d n a h n t o at o i n.
Get v n I o t a c o i n t s i L () This method r t e ur s n an array of
. m e t s y S D a g e l e te s e p y t , a e ch
g n i t n e s e r p e r a i t r a p cul r a h t e m od t a h t
y a m e b d e k o v n i .

e D pt. f o T I B J S , E S I e g a P 0 4 1
I 6 0 S 1 6 7

T e h se sta i t c met d o h s e r m e v o s a
met d o h (or a l l methods) f o r m t e h
Re e v o m A l l () i o v n ca i t n o i l st.
In , # C the R m e o ( e v ) me d o h t can be
d e l l a c y l t c e r i d n i n i s u g
-= o t a r e p o r.

The S m i plest Po s s ible Delegate x E ample
D l e a g e tes a c n t n e d to cause a great a e d l of o c nf s u i n o w e h n en n u o c e t red for the fi s r t
g t e t e h b l l a , g n i l l o r s t e l e k a t a l k o o t a a r e v y simp e l ex l p m a e t a h t lever s e g a
our y r a n i B Op d g e l e ate t e p y . e r e H is e h t co e t e l p m c d o e, i w h t n a a y l sis to fol o l : w
e t a g e l e D e l p m i S e c a p s e m a n
{
/ / s i h T e t a g e l e d n a c t n i o p o t y n a , d o h t e m
/ / g n i k a t o w t s r e g e t n i d n a g n i n r u t e r n a
/ / r e g e t n i .
c i l b u p e t a g e l e d t n i t n i ( p O y r a n i B , x t n i ; ) y

/ / s i h T s s a l c s n i a t n o c s d o h t e m p O y r a n i B l l i w
/ / t n i o p o t .
c i l b u p s s a l c h t a M e l p m i S
{
c i l b u p c i t a t s t n i t n i ( d d A , x t n i ) y
{ n r u t e r x + ; y }
c i l b u p c i t a t s t n i t n i ( t c a r t b u S , x t n i ) y
{ n r u t e r x ; y }
}

s s a l c m a r g o r P
{
c i t a t s d i o v ] [ g n i r t s ( n i a M ) s g r a
{
* * * * * " ( e n i L e t i r W . e l o s n o C e l p m i S e t a g e l e D e l p m a x E * * * * * \ ; ) " n

/ / e t a e r C a p O y r a n i B t c e j b o t a h t
/ / s t n i o p " " o t ) ( d d A . h t a M e l p m i S .
a n i B p O y r b = w e n ; ) d d A . h t a M e l p m i S ( p O y r a n i B
/ / e k o v n I ) ( d d A d o h t e m g n i s u e t a g e l e d .
0 1 " ( e n i L e t i r W . e l o s n o C + 0 1 s i , " } 0 { , 0 1 ( b ; ) ) 0 1
Con l o s e e R . d a L ( e n i ; )
}
}
}
g A a n i no i t ce the f r o mat of the O y r a n i B p deleg , e t a wh c i h can point to any o h t e m d n i k a t g
o w t g e t n i e s r d n a g n i n r u t e r n a i t n eger. e v i G n this, we have crea e t d a cla s s name d
Sim e l p M t a h , w i h ch e d fines o w t sta i t c methods that ( u s rprise, surprise) mat h c h t e pattern

e D pt. f o T I B J S , E S I e g a P 1 4 1
# C d n a g n i m m a r g o r P T E N .

Rem ( e v o )
e h t d e d a o l r e v o
m i t e. Th , s u to
I 6 0 S 1 6 7

Wh n e u o y w n a t to in r e s t h t e a t rget met d o h to a gi e v n



s i y l l a e r d e l l a c ! e r e h
0 1 " ( e n i L e t i r W . e l o s n o C + 0 1 s i , " } 0 { , 0 1 ( b ; ) ) 0 1
r e d n U e h t , d o o h h t e i t n u r me ac a u t l y l ca s l l t e h comp l i er- e g n r e a e t d In ( e k o v ) me h t od.
u o Y n a c y f i r e v s i h t t c a f r o f f l e s r u o y f i u o y n e p o r u o y y l b m e s s a n i l i d x e . m s a e d n a
e t a g i t s e v n i e h t L I C e d o c n i h t i w e h t a M i ( n ) : d o h t e m
. d o h t e m e t a v i r p g i s y b e d i h c i t a t s d i o v ] [ g n i r t s ( n i a M ) s g r a l i c d e g a n a m
{
. . .
. s l a c o l t i n i ] 0 [ ( s s a l c p O y r a n i B . e t a g e l e D e l p m i S ) b
2 3 t n i S l p m i e l e D a g e e t p m i S . l 3 t n i ( d d A : : h t a M e 2, n i t 2 3 )

, t c e j b o ( r o t c . : : p O y r a n i B . e t a g e l e D e l p m i S i t a n ve 0 . c o l t s ) t n i
0 1 " + 0 1 s i " } 0 {
0 . c o l d l
s . 4 i . c d l 0 1
s . 4 i . c d l 0 1
t r i v l l a c e c n a t s n i 2 3 t n i Sim D e l p e e l a g t i B . e n O y r a p v n I : : oke , 2 3 t n i ( ) 2 3 t n i
. . .
}
l l a c e R t a h t T E N . s e t a g e l e d e k i l n u ( C- e l y t s n o i t c n u f ) s r e t n i o p e r a e f a s e p y t . e h T r o f e re, if
u o y t p m e t t a o t a p ss a e d e l gate a method t a h t o d es o n t mat h c t e h a p t t e n r , u o y re e c v i e
l l i u , e t a r t s s a s m u e the Si e l p m M t a h s s a l c s e n i f e d n a l a n o i t i d d a
d o h t e m d e m a n ( r e b m u N e r a u q S ):
c i l b u p s s a l c h t a M e l p m i S
{
. . .
c i l b u p c i t a t s t n i t n i ( r e b m u N e r a u q S ) a
{ n r u t e r a * ; a }
}
i G ven h t at the Bi a n O y r p e d legate a c n n o ly t n i o p to o h t e m s d h t t a t k a e o w t in e t gers a d n
e r u t rn
/ / ! r o r r E d o h t e M s e o d t o n h c t a m e t a g e l e d ! n r e t t a p
p O y r a n i B b = w e n ; ) r e b m u N e r a u q S . h t a M e l p m i S ( p O y r a n i B

A r o M e Elaborate Del g e ate Exam l p e
o T l l i u t s r e t a a r o m e ad n a v ced s u e of d e l e gates, le t s b g e in by u d p a i t ng the Car c a l ss to
include w t o
a w s a h (is t r i D y ; ) e h t t o h r e t n e s e r p e r s e h w ther e h t c r a n i
n o i t s e u q s i n i d e e n f o a ti e r t o r ation ( o h s u o R d l t t a e). o T en b a le t e h object user t o
inte a r ct w t i h this e n w state a d ta, Car also def n i es s m o e
d e t a d p u r o t c u r t s n o c . Here s i the story so a f r:
. . .

e D pt. f o T I B J S , E S I e g a P 2 4 1
C# d n a m m a r g o r P T E N . i g n

n i f e d ed by the i B n y r a Op d t a g e l e e.
e d l a g e te, s l p m i y pass n i h t e a n me of the me o h t d to the delega e t s constru t c r o . At thi s
o p int, you are b a le to invo e k t e h mem r e b o p t n i ed to using a sy t n ax th t a lo k o s l k i e a
i d rect f n u ction inv c o ati n o :
/ / ) ( e k o v n I
n t f d l
. . .
new b o j
r t s d l
e c n a t s n i d i o v
a o c m i p le- m i t e err r o . T o
an in , r e g e t h t e l l o f o n i w g d o c e is i g e l l al a d n w l i l not c m o pi : e l
new B l o o e n a mem r e b varia s e l b . h T e first is used to e d te m r i e n h w e h t er
o y ur u a o m o t b l i e is e u d o f r
l a n o i t i d d a s e i t r e p o r p d n a n a
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

/ / e r A e w n i d e e n f o a ? h s a w d e e N o t e t a t o r ? s e r i t
e t a v i r p l o o b ; y t r i D s i
e t a v i r p l o o b ; e t a t o R d l u o h s

/ / a r t x E s m a r a p o t t e s . s l o o b
c i l b u p g n i r t s ( r a C , e m a n t n i , x a m t n i , r r u c
l o o b , r a C h s a w l o o b ) s e r i T e t a t o r
{
. . .
y t r i D s i = ; r a C h s a w
e t a t o R d l u o h s = ; s e r i T e t a t o r
}
c i l b u p l o o b y t r i D
{
{ t e g n r u t e r } ; y t r i D s i
{ t e s y t r i D s i = ; e u l a v }
}
c i l b u p l o o b e t a t o R
{
n r u t e r u o h s o R d l t t a e;
e t a t o R d l u o h s = ; e u l a v }
}
}
, w o N s l a o s a u s me the a C r type e n sts a new d l e egate, r a C De a g e l te:
/ / r a C s e n i f e d t e y r e h t o n a e t a g e l e d .
c i l b u p s s a l c
{
. . .
/ / n a C l l a c y n a d o h t e m g n i k a t a r a C s a
/ / a r e t e m a r a p d n a g n i n r u t e r g n i h t o n .
c i l b u p e t a g e l e d d i o v r a C ( e t a g e l e D r a C ; ) c
. . .
}
He e r , o y u have cr a e t d e a del a g e te nam d e CarD g e l e ate. e h T a C rD e l e g t a e p y t e
e r p e r s s t n e some f c n u i t n o t k a i g n a a C r as a parameter a d n re r u t n n i g i o v d.
7.5 Understanding C# E e v nts
Dele a g tes a e r fair y l interes i t ng constructs in t a h t th y e en b a le two objects in mem y r o to
n e g e g a n i a o w t -way conver a s i t on. s A u o y y a m , e e r g a w o h e , r e v wor g n i k w t i h de t a g e l es
n i e h t w a r o d es ent l i a a good amount of o b l i erplate c d o e (d f e ining t e h e d l a g e t , e
declar n i g neces a s ry mem r e b vari b a les, a d n c g n i t a e r m o t s u c
n o i t a r t s i g e r n u / n o i t a r t s i g e r ) s d o h t e m . B a c e u e s the ab t i l i y for one b o ject o t ca l l k c a b o t
o n a ther o e j b ct is u s ch a e h lpful o c nstruc , t C#
d r u b en f o us g n i de s e t a g e l n i t e h w a r . h W n e the com l i p er p o r ces e s s t e h
event ke , d r o w y u o y a e r au m o t atica l l y r p ov d i ed wi h t e r gistrat n o i n a d n u e r gist a r o i t n
met s d o h s a we l l as y n a n c e e s s ary mem r e b vari b a le o f r yo r u del a g e te t p y es. In this
i l gh , t the ev t n e
u o y o s me ty n i p g time. e D f n i ing an e e v nt is a t o w - e t s p proce s s . r i F s , t u o y d e e n to
e D pt. f o T I B J S , E S I e g a P 4 1 3
get{
set{
}
r a C
s e d i v o r p h t e n e v e t d r o w y e k to e l s s en t e h
y e k word is t t i l le more h t n a n y s tac i t c u s ga , r whi h c c n a e b u d e s o t v a s e
C# d n a m m a r g o r P T E N . i g n

e n i f e d a de g e l a e t t t a h t n o c a n i s h t e
o y u e d cl r a e h t e eve t n s ( s u ing h t e C# n e v e t e k y
a nutshe l l , def n i n i g a p y t e t a h t a c n send events n e ta l i s h t e fo o l l wi g n a p t t e n r ( h s own in
pseudo- d o c e):
s s a l c c i l b u p s t n e v E f O r e d n e S
{
c i l b u p e t a g e l e d l a v t e r A s s o t a i c ed e l e D g t a e( a g r s);
c i l b u p t n e v e e t a g e l e D d e t a i c o s s A Nam f O e E e v nt;
. . .
}
e h T t n e v e s of h t e a C r p y t e l i w l take t e h same a n me as h t e p e r vi u o s del g e ate s
(Ab t u o To o l B w and Ex o l p ded). h T e w e n d e l e gate to wh c i h t e h events are a o s s cia e t d l l i w
e b cal e l d a C rE n e v t d n a H e l r. e r e H a e r e h t n i i i t al e t a d p u s o t h t e a C r typ : e
c i l b u p s s a l c r a C
{
/ / s i h T e t a g e l e d s k r o w n i n o i t c n u j n o c h t i w e h t
/ / s ' r a C s t n e v e .
e t a g e l e d c i l b u p d i o v g n i r t s ( r e l d n a H t n e v E r a C ; ) g s m

/ / s i h T r a c n a c d n e s e s e h t . s t n e v e
c i l b u p v e t n e r e l d n a H t n e v E r a C ; d e d o l p x E
c i l b u p t n e v e r e l d n a H t n e v E r a C ; w o l B o T t u o b A
. . .
}
e S n n i d g an event o t the cal e l r is as s m i ple as e p s ci i y f ng h t e e e v nt y b na e m s a we l l as
y n a d e r i u q e r para t e m e s r as de d e n i f y b e h t ass t a i c o ed de g e l a e t . o T e r u s n e t a h t e h t
l l a c er a h s d e e d n i i g e r s d e r e t h t i w , t n e v e u o y l l i w n a w t o t e h c ck e h t v e e t n t s n i a g a a nu l l
e m o h t d set. h T ese things bei g n said, e h re is th e
new e t i ration of the r a C s c c A e a r e l t ( e ) e m t d o h : c i l b u p d i o v t n i ( e t a r e l e c c A ) a t l e d
{
/ / f I e h t r a c s i , d a e d e r i f d e d o l p x E e v e . t n
f i
{
f i d e d o l p x E ( = ! ) l l u n
s i h t , y r r o S " ( d e d o l p x E r a c s i d a e d . . . ; ) "
}

e s l e
{
d e e p S r r u c = + ; a t l e d

/ / t s o m l A ? d a e d
f i
& &
{
u f e r a C " ( w o l B o T t u o b A l ! y d d u b n o G a n o l b " ! w ; )
}
e D pt. f o T I B J S , E S I e g a P 4 4 1
I 6 0 S 1 6 7

meth s d o o t e b a c l l ed w n e h e h t e t n e v is fi d e r . Next,
wor ) d n i t r e ms f o h t e related delega e t . In
e u l a v e r o f e b g n i k o v n i e h t s e t a g e l e d
a c ( I r s d a e D )
0 1 ( = = d e e p S x a m - d e e p S r r u c
w o l B o T t u o b A = ! ) l l u n
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7



d e e p S r r u c ( = > ) d e e p S x a m
d a e D s I r a c = ; e u r t
e s l e
" ( e n i L e t i r W . e l o s n o C - d e e p S r r u C > = , " } 0 { ; ) d e e p S r r u c
}
}
W t i h this, o y u have o c nfi u g r d e h t e car to d n e s w t o custom n e v e ts t i w hout t e h n e e d to
def n i e cus m o t regis a r t t n o i func o i t ns. u o Y l l i w see the s u age of this new autom b o ile in
u j st a m o m n e t, but f r i s , t lets c e h k c h t e event archi e t cture in a bit mo e r deta l i .

7. 6 e h T Advanced e K ywo d r s of C# A , Ca a t lo o g C f # y e k r o w s d
o T c e s o l th s i , r e t p a h c y l l u o e n i m a x e so e m of e h t o m e r s e ot r e c i C# ke o w y rds:
e h c cked/ d e k c e h c n u
uns f a e/sta l a k c loc/fi e x d/ f o e z i s
T e l , t r a t s o t h t u o k c e h c s o r p # C w o c i t e m h t i r a f o n o i t c e t e d c i t a m o t u a s e d i v o f r e v o l d n a w
s n o i t i d n o c i s u ng h t e ch k c e ed d n a unc c e h ked . s d r o w y e k

The d e k c e h c e K yword
As y u o are o n doubt we l l awar , e each numeri a c l data ty e p h s a a fix d e u p p er and lo e w r
y a m e b d e n i a t b o r g o r p a i t a m m c y l l a u i s g n e h t M V x a a u l e d n a
Min u l a V e s e i t r e p o r p ). , w o N n e h w u o y e r a g n i m r o f r e p t i r a h c i t e m p o e s n o i t a r on a c i f i c e p s
, e p y t t i s i y r e v e l b i s s o p t a h t u o y y a m y l l a t n e d i c c a r e v o - w o l f h t e m i x a m um s r o t age of th e
e p y t , . e . i ( n g i s s a a e u l a v t a h t is r e t a e r g n a h t e h t m u m i x a m ) e u l a v r o w o l f r e d n u h t e
m i n i m um s g a r o t e f o the p y t e e . i ( ., s a s g i n a va u l e a h t t is less h t n a h t e i m i n m m u v l a u ) e .
To k e e p n i step w t i h the L C R, I i w l l refer to both of e h t se p s o si i b l e i t i s coll c e ti y l e v as
l l i w , e e s d e k c e h c w o l f r e v o d n a w o l f r e d n u s n o i t i d n o c t l u s e r n i
. m e t s y S O l f r e v o e c x E w p o i t n p y t e. e r e h T is no S U . m e t s y n l f r e d o e c x E w p o i t n e p y t n i e h t b s a e
s s a l c ) . s e i r a r b i l o T e t a r t s u l l i e h t , e u s s i e m u s s a u o y e v a h d e t a e r c o w t t y B . m e t s y S e ty e p s a (
# C , ) e t y b h c a e f o h c i h w has been ass g i e n d a a v lue t a h t is safely below t e h maximum
2 ( 55) . f I u o y e r e w to a d d h t e e u l a v s of t e h se e p y t s (c t s a i g n e h t e r u s t l i g n i t n e r e g as a
t y b e , ) u o y d l u o w e m u s s a h t at h t e e r s l u t wou d l e b h t e e a x ct sum f o a e ch me b m : r e
a n m s e a p e c h C e d e k c h c n U e e k c d
{
s s a l c m a r g o r P
{
c i t a t s d i o v ] [ g n i r t s ( n i a M ) s g r a
{
/ / w o l f r e v O e h t x a m e u l a v f o a . e t y B . m e t s y S
x a M " ( e n i L e t i r W . e l o s n o C e u l a v f o e t y b s i , " . } 0 { ; ) e u l a V x a M . e t y b
n i M " ( e n i L e t i r W . e l o s n o C e u l a v f o e t y b s i , " . } 0 { ; ) e u l a V n i M . e t y b
1 b = ; 0 0 1
2 b = ; 0 5 2
m u s = 1 b ( ) e t y b ( + ; ) 2 b


e D pt. f o T I B J S , E S I e g a P 5 4 1
/ / l l i t S ! K O
f i
u d n erflow
l i m i t (wh c i h
o l f r e v o w u o y s A ( .
y b e t
e t y b
e t y b
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

/ / m u s d l u o h s d l o h e h t e u l a v , 0 5 3 r e v e w o h . . .
m u s " ( e n i L e t i r W . e l o s n o C = , " } 0 { ; ) m u s
Con l o s e e R . d a L ( e n i ; )
}
}
}
If you w r e e to i v ew the u o tp t u of this p p a i l a c ti n o , you g i m ht e b s r u pris d e o t f n i d t a h t
ta n i s t e h a v l e u 4 9 ( a r th r e h t n a t e h x e pect d e 0 5 3 ). T e h re n o s a is s m i ple. i G ve n
that a Sy t s m e . t y B e a c n hold a v l a ue n o ly be w t e e n 0 and 255 (inclusiv , e for a grand total
of 256 l s o s t ), u s m o n w co t n a s n i h t e
e s e , n if y u o take o n corre i t c ve course of a t c ion, ove f r w o l u c c o rs with t u o e p e c x t n o i . t A
, s e m i t this e d d i h n r e v o f o l w a m y cau e s no ha m r h w a v e o s t r e in u o y r e j o r p ct. t O he r
m i t es, this loss of a d ta is o c mpletely u e c c a n t p a l b e. o T e l d n a h w o l f r e v o r o w o l f r e d n u
n o c d n o i t i s in yo r u appl c i ation, you have two o t p io s n . r u o Y f r i st e c i o h c s i o t v e l r e e g a
r u o y s t i w d n a g n i m m a r g o r p s l l i k s o t n a h d e l a l l w o l f r e v o n o c d s n o i t i y l l a u n a m . u s s A m g n i
u o y e r e w d e e d n i e l b a o t d n i f e h c a e v o f r w o l c i d n o t n o i n i u o y r pr g o a r m, u o y l u o c d
e v l o s e r t e h p e r vio s u ove w o l f r o r r e r as fo l l o s w :
/ / e r o t S m u s n i n a r e g e t n i o t t n e v e r p w o l f r e v o .
e t y b 1 b = ; 0 0 1
e t y b 2 b = ; 0 5 2
t n i m u s = 1 b + ; 2 b
Of c s r u o e, the probl m e with this e t chnique is the s m i ple fact that you e r a m u h a , n and
best a t t empts m y a resu t l in errors h t at h v a e es p a c ed your ey s e . v i G n e t , s i h
C# pro i v des t e h k c e h c ed k y e o w rd. h W en you wr p a a statem n e t (or a block o f
t s ate e m n s t ) i w thin the o c s pe of the e k c e h c d key- word, e h t C# com i p ler emits e p s cific
CIL instructi s n o that test o f r overflow condi i t o s n h t at may resu t l w e h n addi g n ,
mu l p i t l y n i g, subtra i t c ng, or dividing w t o numeric l a a d ta ty e p s. f I n a w o l f r e v o s a h
c o c r u red, the run i t me w l l i throw a e t s y S m.O w o l f r e v E t p e c x ion type. e s b O rve h t e
n i w o l l o f g d p u a : e t
s s a l c m a r g o r P
{
c i t a t s d i o v ] [ g n i r t s ( n i a M
{
/ / w o l f r e v O e h t x a m e u l a v f o a . e t y B . m e t s y S
x a M " ( e n i L e t i r W . e l o s n o C e u l a v f o e t y b s i , " . } 0 { ; ) e u l a V x a M . e t y b
e t y b 1 b = ; 0 0 1
e t y b 2 b = ; 0 5 2

y r t
{
e t y b m u s = c k c e h ed 1 b ( ) e t y b ( ( + ; ) ) 2 b
m u s " ( e n i L e t i r W . e l o s n o C = , " } 0 { ; ) m u s
}
n o i t p e c x E w o l f r e v O ( h c t a c ) e
{ o C ns . e l o i r W e t ( e n i L e M . s e s ) e g a ; }
}

e D pt. f o T I B J S , E S I e g a P 6 4 1
s m u o c n
e v o r w o l f e u l a v (350 5 2 6 = 94). As you have ju t s
n e v e your
r a s g )
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

}
e H re, you wrap h t e add t i ion of b1 a d n b2 i w thin t e h e p o c s of t e h e h c c e k d e k y o w rd. If
you wish to fo c r e e v o f r o l w e h c c n i k g to c o cur v o r e a b k c o l of c d o , e y u o can int r e t c a
h t i w e h t e h c cked e k yword
y r t
{
d e k c e h c
{
e t y b m u s = 1 b ( ) e t y b ( + ; ) 2 b
m u s " ( e n i L e t i r W . e l o s n o C = , " } 0 { ; ) m u s
}
}
a c t O ( h c r e v o l f c x E w ep i t on e)
{
Con l o s e r W . t i e e n i L ( M . e e s s g a e);
}
I i e n t e d o c e h t , e s a c r e h i t s e u q n i n o wil e e b l v u l a at p r o f d e ossibl e ove f r lo c w n o diti n o s
, y l l a c i t a m o t u a h c i h w l l i w r e g g i r t n a w o l f r e v o n o i t p e c x e f i d e r e t n u o c n e .

t t e S g n i r P j o e t c wide Overflow Che i k c ng
No , w if u o y r a e cr a e i t g n an p p a i l a c o i t n t a h t sho d l u v e n er a l l ow sil n e t overflow to
oc u c r, you m y a d n i f o y urs f l e in the anno i y ng pos i t i on of wrapp n i g u n me o r us i l e n s of
e k c e h c d k y e w r o d. As n a a t l erna i t ve, the C# comp l i er
su o p p rts t e h / c e h c ked flag. h W en enabl d e , all
of y u o r ar t i hme i t c will be e a v l a u ted o f r overflow w t i u o h t t e h e e n d to m e k a use of th e
# C d e k c e h c k y e wo d r . f I e v o w o l f r a h s e b en d s i co e v red, you wi l l s l l i t c e r e v i e a run i t me
o T e l b a n e s i h t f g a l n i s u g l a u s i V S u t d o i 5 0 0 2 , o n e p r u o y s t c e j o r p
y t r e p o r p e g a p a d n i l c ck e h t Adv e c n a d u b t t on n o t e h Bu d l i t b a . F o r m the e r su i t l ng
g o l a i d o b x, sel c e t the Check for t i r a hmet c i e v o f r e d n u / w o l f r w o l h c e k c x o b

7.7 u B l i ding a u C stom I d n exer
As r p o r g a e m m rs, we are e v ry fa i m liar with the proce s s of accessing i d s r c ete t i ems
t n o c ained i w thin a standard ar a r y s u i g n t e h in e d x p o erator, for e a x mple:
/ / e r a l c e D n a y a r r a f o s r e g e t n i .
n i ] [ t s t n I y m = { , 0 1 , 9 , 0 0 1 , 2 3 4 ; } 4 7 8 9

/ / e s U e h t ] [ r o t a r e p o o t s s e c c a h c a e . t n e m e l e
t n i ( r o f j ; 0 j < ; h t g n e L . s t n I y m ) + + j






u o y ha e v d e d d a s t r o p p u r o f an i d n e r e x e m t o h d o t h t e cu t s om co l l ection
( a r a G ge) de e v lop d e in

e D pt. f o T I B J S , E S I e g a P 7 4 1
as f l l o o s w :
e d o c t i w i h n t e h c s o e p of the
Overflo c x E w eption.
.
=
x e d n I " ( e n i L e t i r W . e l o s n o C } 0 { = } 1 { , " , j ; ) ] j [ s t n I y m
The r p ev o i s u code s i by no e m ans a ma o j r ne s w flash. w o H e , r e v h t e # C langu g a e
o r p v e d i s h t e cap b a t i l i y to u b l i d s u c tom classes d n a str c u tures t a h t may be ind e x e d just
k i l e a standard r a ray. t I d l u o h s e b o n g i b s i r p r u s e h t at e h t e m t d o h t t a h provi e d s t e h
y t i l i b a p a c o t s s e c c a t i e s m n i i h t s a m nner is termed an x e d n i er.
e B f r o e e p x o l ri g n how to create such a construct, le s t begin by seeing one in action .
s A u s e m
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7


/ / s r e x e d n I w o l l a u o y o t s s e c c a s m e t i n i n a e k i l y a r r a . n o i h s a f
c i l b u p s s a l c
{
c i t a t s d i o v ] [ g n i r t s ( n i a M ) s g r a
{
* * * * * " ( e n i L e t i r W . e l o s n o C n u F h t i w s r e x e d n I * * * * * \ ; ) " n

/ / e m u s s A e h t e g a r a G e p y t s a h n a r e x e d n i . d o h t e m
a G e g a r t o L r a c = w e n ; ) ( e g a r a G

/ / d d A e m o s s r a c o t e h t e g a r a g g n i s u . r e x e d n i
] 0 [ t o L r a c = w e n , " e e F e e F " ( r a C
] 1 [ t o L r a c = w e n , " r e k n u l C " ( r a C
] 2 [ t o L r a c = w e n , " y p p i Z " ( r a C

/ / w o N n i a t b o d n a y a l p s i d h c a e m e t i g n i s u . r e x e d n i
t n i ( i = ; 0 i < ; 3 ) + + i

i r W . e l o s n o C r a C " ( e n i L e t : r e b m u n , " } 0 { ; ) i
Con l o s e r W . t i e e n i L ( a N " me:
o C n e l o s r W . e t i e n i L ( a M " x
Con l o s e r W . t i e e n i L ();
}
Con l o s e e R . d a L ( e n i ; )
}
}
As you can see, indexers beh v a e much li e k a custom l o c e l c i t on sup o p rt n i g the
IEn e m u r o t a r and IE e m u n r l b a e interfaces. T e h y l n o a m r o j r e f f i d e e c n s i t a h t r e h t a r h t an
g n i s s e c c a t e h t n o c e t n s usi g n
e l l o c c n o i t of a t u o o m b e l i s u j st i l e k a s a t ndard r a ra . y
you configure t e h r a G age l c ass o ( r y n a c a l s s /structure) to sup-
n A d n i e e x r is r p e resented s a a s i l ghtly mang e l d C# pr e p o rty. In t i s s m i pl s e t for , m n a
i d n exer is cr a e ted using h t e th s i [] sy a t n x. e r e H s i e h t t n a v e l e r e t a d p u o t e h t
Gar g a e type:
/ / d d A e h t r e x e d n i o t e h t g n i t s i x e s s a l c n o i t i n i f e d .
c i l b u p s s a l c e g a r a G : e l b a r e m u n E I / / h c a e r o f n o i t a r e t i
{
. . .
/ / e s U t s i L y a r r A o t n i a t n o c h t e r a C . s e p y t
e t a v i r p t s i L y a r r A y a r r A r a c = w e n ; ) ( t s i L y a r r A

/ / e h T r e x e d n i s n r u t e r a r a C d e s a b n o a l a c i r e m u n . x e d n i
c i l b u p r a C t n i [ s i h t
{
/ / e t o N t s i L y a r r A s a h n a r e x e d n i s a ! l l e w
t e g { n r u t e r ; ] s o p [ y a r r A r a c ) r a C ( }
e D pt. f o T I B J S , E S I e g a P 8 4 1
m a r g o r P
20 ) 0 ;
9 ) 0 ;
0 3 ; )
r o f
{
} 0 { , " c r a [ t o L i . ] N t e P am ) e ;
: d e e p s , " } 0 { ca o L r t . ] i [ C p S r r u e ) d e ;
in a f r e t ce y t pes, o y u r a e able to a m nipulate t e h in e t rnal
w o N for the b g i que t s ion: H w o do
r o p t t i h s f n u ctiona i l y t ?
pos]
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

t e s { ] s o p [ y a r r A r a c = e u l a v }
}
}
d n o y e B h t e e s u of e h t i h t s key o w rd, h t e inde e x r lo k o s u j st l k i e any ot e h r C# property
e d cla a r
n o y e b d t e h use f o t e h subs r c i t p p o e a r t r o . In other words, the b o ject us r e o n n a c t wr t i e
code s c u h as h t e o f llo i w ng:
/ / e s U t n u o C . t s i L y a r r A ? y t r e p o r p ! e p o N
s r a C " ( e n i L e t i r W . e l o s n o C n i : k c o t s } 0 { , " . t o L r a c u o C nt ; )

o T u s pport this f c n u i t ona t i l y, y u o wo d l u d e e n to d d a o y ur w o n u o C nt p o r p e y t r o t e h t
Gar g a e
c i l b u p s s a l c : e g a r a G e l b a r e m u n E I
{
. . .
/ / i a t n o C n o i t a g e l e d / t n e m n n i n o i t c a e c n o . n i a g a
c i l b u p t n i t n u o C { t e g { n r u t e r ; t n u o C . y a r r A r a c } }
}
As y u o can ga h t er, ind x e r e s are yet ano e h t r form of syn a t ctic s g u ar, given that thi s
can al o s be h c a iev d e using o n rmal public me h t o s d . o F r exa e l p m , if the
po t r n a indexer, you w u o ld be a l b e to a l l ow t e h outsi e d world
to in e t c a r t t i w h t e h inter a n l arr y a list us g n i a d e m a n p o r p e y t r r o tr a n o i t i d a l
r o t a t u m / r o s s e c c a s d o h t e m . Neve h t r e , s s e l n e h w u o y t r o p p u s i d n exers n o y u o r custom
e l l o c c n o i t types, h t ey in e t grate we l l into the fabric of the . T E N b s a e class i l braries.

























e D pt. f o T I B J S , E S I e g a P 9 4 1
tion. o D be awa e r that i x e d n ers do o n t pro d i v e y n a arr y a - i l ke u f nc i t ona t i l y
t p y e, d n a e d l a g e te c c a ordi g n ly:
f n u ctionality
Gar g a e t p y e d d i o n t sup
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7


s n o i t s e u q d e d n e m m o c e R
1. t a h W e r a ? s e t a g e l e d n i a l p x E e h t s r e b m e m f o s e t a g e l e D t s a c i t l u M . m e t s y s . e t i r W a g o r p r m a
n i # c o t t n e m e l p m i r o t a r e p o r e v o i d a o l ng f o + d n a r o f i d d a ng c a r t b u s t g n i t o w e r a u q s
i r t a m e c s.
2. Ex n i a l p e h t o w t l a u t p e c n o c s w e i v f o t e N . y l b m e s s a h t i w a n t a e g a i d ram. t a h W e r a t e h
e r o c e b n s t i f e f o s i h t ?
3. h t i W n a ex s s u c s i D , e l p m a d e c n a v d a ke r o w y s d f o C , d e k c e h c : # un , d e k c e h c un , e f a s
, c o l l a k c a t s e l i t a l o v d n a si e z f o .
4. e t i r W a m a r g o r p n i # C o t t r o s d n a e s r e v e r n a r r a ay f o e v i f e l e m s t n e g n i s u s r o t ) ( d n a
s d o h t e m ) ( e s r e v e r .
5. t a h W o d u o y d n a t s r e d n u by ev s t n e d n a s e t a g e l e d in C #? e e v i G . x s e l p m a



































e D pt. f o T I B J S , E S I e g a P 0 5 1
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

T I N U -8
g n i d n a t s r e d n U e h t t a m r o F f o a T E N . y l b m e s s A

1 . 8 s m e l b o r P h t i w c i s s a l C M O C Bi r a n i s e ,
2 . 8 n A w e i v r e v O f o N . ET b m e s s A l , y
3 . 8 g n i d l i u B a e l p m i S i F le t s e T b m e s s A ly
4 . 8 A # C C t n e i l l p p A i , n o i t a c
A 5 . 8 l a u s i V s a B ic T E N . t n e i l C , n o i t a c i l p p A Ex g n i r o l p e h t r a C a r b i L r s y
t s e f i n a M
6 . 8 g n i r o l p x E e h t r a C a r b i L r s y T s e p y






































e D pt. f o T I B J S , E S I e g a P 1 5 1
C# d n a a r g o r P T E N . m m i g n I 6 0 S 1 6 7

8.1 Problems with Classic COM Bina i r es
. T E N i l p p a c t a i s n o a e r u r t s n o c c d e t y b e i p cing to t e g h r e a y n u n b m er of a s s emblies .
m i S ply u p , t n a a s s embly is a ve s r ione , d e s lf-d s e cribing binary f e l i ho e t s d by t e h CLR.
s e i l b m e s s a h e v a a x e c y l t e h t s m a e i f e l e n e t x s s n o i
( x e . * e or . * d l l ) s a pre i v o s u Win32 binaries (i c n lud
e v y r l t t i le in m m o c on n u der the hood. Thus, o t set t e h stage o f r the n i formati n o o t
m o c , e le t s ponder e m o s of the b e n e fits provided by h t e a s s e b m ly format.

e s s A m s e i l b e t o m o r P C e d o e s u e R
As o y u v a h e n e e b bu g n i d l i r u o y e l o s n o c n o i t a c i l p p a s e v o r h t e v e r p i u o s h c a e t p r , s it a m y
e v a h d e m e e s t a h t l l a f o e h t a i l p p c s n o i t a f n u t c n o i ality was n i a t n o c d e wi i h t n h t e
e l b a t u c e x e l b m e s s a y u o y r e w e n o c r t s g n i t c u . In a e r t i l y, your p a i l p a c i t n o s e w re
lever g a ing u n m o r e us t p y es n o c tain d e i h t i w n t e h a w l s y a
m c s orl b i . l d l (recall that the C# o c mpiler refer n e ces ms o c r i l b.dll t u a o i t a m a c l l y , ) s a
l l e w s a Sy . m e t s W w o d n i s . s m r o F . dll. s A y u o may n k w o , a o c de library (also term d e a
cl s a s i l brary) is a * l d . l t t a h t n o c ai s n t p y s e i e t n n d e d to be e s u d by t x e e n r al
i t a c i l p p a ons. W e h n you are r c eating x e ecutab e l s a sembl s e i , you l i w l o n d u o bt be
lever g a ing numerous system-supplied a d n u c s o t m code l b i raries as y u o create the
n o i t a c i l p p a t a d n a h . o D be a r a w , e howe e v r, that a c d o e l b i r r a y e n d e o n t t k a e
a * l d . l l i f e e n e t x s n o i . t I is perf c e l t y p s o s l b i e for an u c e x e t b a le assembly to m e k a u e s
of types defi d e n w t i n i h an x e ter a n l
l a so be c n o s r e d i ed a c d o e a r b i l r . y

8.2 Unde s r tanding e h t F r o mat of a .N T E A s s embly
w o N t a h t e v u o y r a e l n d e t u o b a l a r e v e s s t i f e n e b pr d i v o ed by h t e .NET s a se l b m y, t e l s
t f i h s r a e g s d n a g t e a be t t er d i a e f o how an a e s s l b m y is com o p e s d d n u er h t e d o o h .
t S ruc r u t a l l y a e p s i k g n , a T E N .
t n e m e l e s:
A i W 3 n 2 l i f e heade r
A r e d a e h e l i f R L C
CI e d o c L
Type metadata
n A y l b m e s s a i n a m fest
t p O n o i al e d d e b m ed r s e ources
e l i h W e h t t s r i f t o w l e e s t n e m e h t ( 2 3 n i W d n a R L C ) s r e d a e h e r a s k c o l b f o a t a d t a h t u o y n a c
y l l a c i p y t , e r o n g i y e h t o d e v r e s e d e m o s br f e i n o i t a r e d i s n o c . s i h T g n i e b , d i a s n a e v o r w e i v
f o h c a e e l e t n e m s w o l l o f .


T e h Win 2 3 F e l i Header
T e h Wi 2 3 n f l i e a e h d r e esta i l b shes the f c a t h t at the assembly n a c e b o l aded n a d
m n a i u p lated y b h t e Wi s w o d n fa l i m y of o i t a r e p ng sy t s e s m . Th s i r e d a e h t a d a l a so
s e i f i t n e d i e h t d n i k f o n o i t a c i l p p a e l o s n o c ( - , d e s a b I U G - a b se , d r o *.dll code l b i rary) to
e b o h s e t d by the Wi o d n s w operating syste . m


e D pt. f o T I B J S , E S I e g a P 2 5 1
, w o N de p s i e t the a f ct t a h t . E N T
i g n legacy COM e s r e v rs , ) h t y e have
l b i s s e c c a e .N T E co e d i l brar , y
a t u c e x e ble i f e l . n I i h t s li h g t, a r f e e r e nc d e . * exe can
assembly ( d . * ll or * e . xe) t s i s n o c s of h t e fol g n i w o l
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

8.3 u B l i ding a d n Consuming a Sin l g e-F l i e A s s embly
T p m o c f o s s e c o r p e h t n i g e b o r e i l b m e s s a T E N . f o d l r o w e h t g n i d n e h e o y , s ull fi s r t r c ea a e t
s s a e b m y l (named Car i L a r b ry) that contains a small set of pu l b ic t p y es.
o T d l i u b a d o c e l b i a r y r us g n i Visual t S udio 2005, i s mply se e l ct the Cla s s Library pro e j c t
o w k r s a p ce T e h desi n g of your u a tom b o l i e library e b i g ns t i w h an a s b tract a b se c a l s s
n i f e d es a b m u n er of r p ot t c e ed data me b m ers o p x e sed r h t o g u h cus m o t
propert e i s. i h T s s s a l c has a s g n i le r t s b a act me o h t d n e m a d b r u T o s o o B t() , w c i h h m s e k a
e s u f o a c s u tom e m u n a r e t o i n ( n E g S e n i t t a e) g n i t n e s e r p e r e h t cu r r e t n c n o i t i d n o f o e h t
r a c : e n i g n e s
g n i s u ; m e t s y S

e c a p s e m a n y r a r b i L r a C
{
/ / s t n e s e r p e R e h t e t a t s f o e h t . e n i g n e
c i l b u p m u n e e t a t S e n i g n E
{ , e v i l A e n i g n e d a e D e n i g n e }

/ / e h T t c a r t s b a e s a b s s a l c n i e h t . y h c r a r e i h
c i l b u p t c a r t s b a s s a l c r a C
{
d e t c e t o r p g n i r t s ; e m a N t e p
t c e t o r p d e t r o h s ; d e e p S r r u c
d e t c e t o r p t r o h s ; d e e p S x a m
d e t c e t o r p e t a t S e n i g n E e t a t S n g e = ; e v i l A e n i g n e . e t a t S e n i g n E

c i l b u p t c a r t s b a d i o v ; ) ( t s o o B o b r u T

c i l b u p } { ) ( r a C
c i l b u p g n i r t s ( r a C , e m a n t r o h s , x a m t r o h s ) r r u c
{
e m a N t e p = ; e m a n d e e p S x a m = ; x a m d e e p S r r u c = ; r r u c
}

c i l b u p g n i r t s e m a N t e P
{
t e g { n r u t e r ; e m a N t e p }
t e s { e m a N t e p = } ; e u l a v
}
c i l b u p t r o h s d e e p S r r u C
{
t e g { n r u t e r ; d e e p S r r u c }
} ; e u l a v = d e e p S r r u c { t e s
}
c i l b u p t r o h s d e e p S x a M
{ t e g { n r u t e r ; d e e p S x a m } }
e t a t S e n i g n E e t a t S e n i g n E c i l b u p
{ t e g { r u t e r n ; e t a t S n g e } }
e D pt. f o T I B J S , E S I e g a P 3 5 1
s g n i le- i f l e * d . ll
a n m d e a C r a h t t
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7

}
}
w o N s s a u e m h t at y u o v a h e two c e r i d t d s e e c n t n e d s of h t e a C r e p y t n d e m a a V i n i M n n a d
SportsCar. E h c a overrides h t e abs r t c a t Tu o B o b r o ( t s ) e m t d o h n i an p a r p opriate
e n n a m r.
s u g n i ; m e t s y S
g n i s u ; s m r o F . s w o d n i W . m e t s y S

e c a p s e m a n y r a r b i L r a C
{
c i l b u p s s a l c r a C s t r o p S : r a C
{
c i l b u p { ) ( r a C s t r o p S }
c i l b u p g n i r t s ( r a C s t r o p S , e m a n t r o h s , x a m t r o h s e s a b : ) r r u c , e m a n ( , x a m { ) r r u c }

d i o v e d i r r e v o c i l b u p ) ( t s o o B o b r u T
{
h S . x o B e g a s s e M g n i m m a R " ( w o , " ! d e e p s r e t s a F " s i r e t t e b . . . ; ) "
}
}
c i l b u p s s a l c n a V i n i M : r a C
{
c i l b u p { ) ( n a V i n i M }
c i l b u p g n i r t s ( n a V i n i M , e m a n t r o h s , x a m t r o h s e s a b : ) r r u c , e m a n ( , x a m { ) r r u c }
d i o v e d i r r e v o c i l b u p ) ( t s o o B o b r u T
{
/ / s n a v i n i M e v a h r o o p o b r u t i l i b a p a c ! s e i t
= e t a t S n g e ; d a e D e n i g n e . e t a t S e n i g n E
e m i T " ( w o h S . x o B e g a s s e M o t l l a c , " A A A r u o Y " r a c s i ; ) " d a e d
}
}
}
. 8 4 A C# Cl e i nt App a c i l tion
t A s i h t t n i o p u o y n a c l i u b d r u o y t n e i l c n o i t a c i l p p a o t e k a m e s u f o e h t l a n r e t x e s e p y t .
U a d p te r u o y n i it l a i C# l i f e as so:
g n i s u ; m e t s y S

/ / t ' n o D t e g r o f o t ' e s u ' e h t y r a r b i L r a C ! e c a p s e m a n
g n i s u ; y r a r b i L r a C

e c a p s e m a n t n e i l C r a C p r a h S C
{
c i l b u p s s a l c t n e i l C r a C
{
] [ g n i r t s ( n i a M d i o v c i t a t s ) s g r a
{
/ / e k a M a s t r o p s r a c .

e D pt. f o T I B J S , E S I e g a P 4 5 1
C# d n a P T E N . m m a r g o r i g n I 6 0 S 1 6 7

r a C s t r o p S r e p i v = w e n , " r e p i V " ( r a C s t r o p S , 0 4 2 ; ) 0 4
vip . r e T b r u B o o ( t s o ; )

/ / e k a M a . n a v i n i m
n a V i n i M v m = w e n ; ) ( n a V i n i M
mv. r u T b o B o s o t ) ( ;
Con l o s e e R . d a L ( e n i ; )
}
}
}
This o c de looks just like the other app i l a c i t s n o de e v l p o ed thus far. The only point of
in e t rest is th t a e h t # C cl e i nt application s i now making use of t e p y s e d fin d e i w thin a
r a p e s a e t u c stom s s a embly. o G e h a ad and n u r o y ur program. As o y u w u o ld e p x e , t c h t e
e x e cu o i t n of th s i program results in h t e dis l p ay of a v r s u o i me g a s s e o b x s e .

5 . 8 g n i d l i u B a l a u s i V c i s a B T E N . t n e i l C n o i t a c i l p p A
To l l i s u trate h t e l a u g n a ge- s o n g a i t c a t i t t e d u f o h t e N . ET platfor , m le t s cr a e te n a h t o er
con o s le a p p lic n o i t a (Vb e N tCar i l C ent), h t is time u i s g n Visu l a Basic .N T E (see Fi u g re 1 1 -
t e h p o r ject, e s t a refe e r n e c to Ca r b i L r ar . y dll s u ing t e h Add
Refere c n e dialog box. s t r o p m I y r a r b i L r a C

e l u d o M 1 e l u d o M

b u S ) ( n i a M
d n E b u S

d n E e l u d o M
t o N ice that the M ( n i a ) h t e m d o s i defined i w thin a Visual Basic T E N . Modu e l t p y e
c i h w ( h has noth g n i o t o d w t i h a *. e n t o m dule e l i f o f r a u m t l if l i e a e s s y l b m ) . Mod l u es a e r
si p m ly a Visual B s a ic
o c t n ain on y l static me h t ods. o T e v i r d
th s i o p int home, e h re wo d l u be t e h a s me construct n i C# :
/ / A B V T E N . ' e l u d o M ' s i y l p m i s a d e l a e s s s a l c
/ / g n i n i a t n o c c i t a t s s d o h t e m .
c i l b u p l a e s d e s s a l c 1 e l u d o M
{
c i l b u p c i t a t s d i o v ) ( n i a M
{
}
}
n I a y n case, to e x e c r i e s e h t n i M i a V n d n a s t r o p S Car s e p y t u n i s g the syn a t x o f s i V u l a B c i s a
. , T E N e t a d p u r u o y n i a M () me d o h t as so:
b u S ) ( n i a M
* * * * * " ( e n i L e t i r W . e l o s n o C n u F h t i w l a u s i V c i s a B T E N . ) " * * * * *
m i D m n a V i n i M y s A w e N ) ( n a V i n i M
myM i n i V . n a u T r o B o b o ( t s )

e D pt. f o T I B J S , E S I e g a P 5 5 1
1 ) 0 . n O ce y u o have cre t a d e
. E N T s r o h th n a d n t o a o i t n o f r de i f n i n g a e s aled c a l ss that ca n
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7


m i D r a C s t r o p S y m s A w e N ) ( r a C s t r o p S
myS r o p t a C s . r T o b r u B s o o t()
Con l o s e e R . d a L ( e n i )
d n E b u S
h W en y u o compile and u r n your p a plication, you wi l l o c n e again find a ser e i s of
me s s age b x o es i d p s l e y a d.

6 . 8 g n i r o l p x E e h t s y r a r b i L r a C s e p y T
R a c e l l t t a h h w n e you e g nerate a str g n o name for n a assembly, t e h n e tire pu i l b c e k y is
r c e ord d e n i e h t s s a e y l b m ma i n f t s e . On a relat d e o n te, w n e h a c i l n e t referenc s e a
ma i n fest records a con e d ns d e h h s a v l a ue f o the full
u p b i l c key, denoted by the . u p bl e k c i y e k o t n tag. f I u o y e r e w o t p o en e h t i n a m f t s e of
Sha d e r C L r a b i C n e i l t x e . e using l i d . m s a exe, y u o d l u o w f d n i e h t f l l o w o ing:
. n r e t x e y l b m e s s a y r a r b i L r a C
{
. e k c i l b u p n e k o t y = 1 2 ( E 9 3 F 0 8 9 C 4 3 A 8 ) 8 3
. r e v 0 : 0 : 0 : 1
}
f I you co r a p m e the v l a ue of h t e public key token e r co d r d e in the c e i l nt a m nifest wit h
the pub i l c key token value sh n w o in the GAC, you will find a e d ad-on m t a ch. l l a c e R a h t t
a i l b u p c y e k s t n e s e r p e r e n o as t c e p of h t e t s ro g n ly na d e m assem l b s y d i e t n i y t . v i G n e
, s i h t e h t CLR l l i w only load e v s r n o i . 0 . 1 0 0 . of n a a e s s y l b m na d e m a C b i L r r a r y h t at h s a a
c i l b u p y e k t a h t c n a e b ha h s ed n w o d to h t e a v lue 21 E 9 F 0 8 3 C 4 3 9 8A38. If the CLR does
not i f nd an a s s embly m e e ting this descri t p ion in the A G C (and
y l b m e s s a d e m a n y r a r b i L r a C n i h t e l c ie t n s d e r i c o t ry), a e l i F N u o F t o nd i t p e c x e on i s
h t rown.






















e D pt. f o T I B J S , E S I e g a P 6 5 1
str n o y l g m a n d e as b m e s ly, t i s
n n a c t o f d n i a pr v i a e t
C# d n a m m a r g o r P T E N . i g n I 6 0 S 1 6 7


: n o i t s e u q d e d n e m m o c e R
1. e t i r W t r o h s e t o n s n o e h t : g n i w o l l o f ) i c i s s a l c O C M s e i r a n i b s u s r e v .N t e ass m e bl s e i i i )
s s o r C e g a u g n a l e c n a t i r e h n i .
2. e t i r W t r o h s s e t o n : n o I ) i n e c a f r e t f o s m e t s y n o i t c e l l o c ) i i n I dex s r e ) i i i d e r a h S l b m e s s a i s e
l a u t u M ) v i d n a u m m i t b a le r t s i s g n .
3. e t a r t s u l l I h t i w n a m a x e , e l p e c n e r e f f i d n e e w t e b s s u o n o r h c n y d n a as s u o n o r h c n y
e l e d g t a es.









































e D pt. f o T I B J S , E S I e g a P 7 5 1

You might also like