Sie sind auf Seite 1von 25

Article

Intro to C# (vs. Objective-C and Java)


D o c u m e n t ver si o n : La s t up d a t e d : 1.0.0 6/27/20 1 1

Page | 1

CONTENTS OVERVIEW.................................................................................................................................................3 OBJECTIVES .............................................................................................................................................3 INTRODUCTION TO C#..............................................................................................................................3 Hello C#!................................................................................................................................................6 .NET Runtime........................................................................................................................................10 .NET and C# Program Basics................................................................................................................11 C# Classes............................................................................................................................................14 C# Structs..............................................................................................................................................18 C# Delegates & Events..........................................................................................................................19 Garbage Collection................................................................................................................................21 Java Programmer Point of View ............................................................................................................23 Objective-C Programmer Point of View ................................................................................................24 SUMMARY................................................................................................................................................25

Page | 2

O vervie w
C# (pron o u n c e d "C sh a r p") is a si m p l e , m o d e r n , obj e c t- ori e n t e d , an d typ e- saf e pr o g r a m m i n g lan g u a g e . It will im m e d i a t e l y be fa m i li a r to C/C+ + an d Ja v a pr o g r a m m e r s . C# co m b i n e s the hi g h pr o d u c t i v i t y of R a p i d Ap p l i c a t i o n D e v e l o p m e n t (RA D) lan g u a g e s an d the ra w po w e r of C++. Thi s arti cl e intr o d u c e s C#, an d co m p a r e s it to oth e r m o d e r n obj e c t- ori e n t e d lan g u a g e s like O bj e c t i v e- C an d Ja v a.

O b j e c t i v es
W h e n yo u v e fini s h e d the arti cl e, yo u will ha v e: A hi g h- lev el un d e r s t a n d i n g of the C# pro g r a m m i n g lan g u a g e . A hi g h- lev el un d e r s t a n d i n g of the .NE T fra m e w o r k . A cle a r vie w of the diff e r e n c e s an d si m i l a ri ti e s in the im pl e m e n t a t i o n of ba si c sc e n a r i o s in C# ver s u s Ja v a an d O bj e c t i v e- C.
.1 .2

I n t r o d u c t i o n t o C#
C# is an obj e c t- ori e n t e d lan g u a g e that en a b l e s pro g r a m m e r s to buil d qui c k l y a wi d e ran g e of ap p li c a t i o n s for the Mi c r o s o f t .N E T fra m e w o r k . Th e go al of C# an d the .NE T fra m e w o r k is to sh o r t e n de v e l o p m e n t ti m e by fre ei n g the de v e l o p e r fro m w o r r y i n g ab o u t se v e r a l lo w- lev e l pr o g r a m m i n g tas k s su c h as m e m o r y m a n a g e m e n t , typ e saf e t y iss u e s , sta n d a r d libr a ri e s , arr a y bo u n d s ch e c k i n g , an d so on. Thi s all o w s de v e l o p e r s to sp e n d the m aj o r i t y of de v e l o p m e n t ti m e w o r k i n g on thei r ap p li c a t i o n s an d bu s i n e s s logi c inst e a d . C# is a si m p l e , m o d e r n obj e c t- ori e n t e d an d typ e- saf e pr o g r a m m i n g lan g u a g e . Let us se e w h y.

Page | 3

Simple B ei n g a part of the Mi c r o s o f t .N E T platf o r m , C# ap p li c a t i o n s rec e i v e su p p o r t fro m the ru nti m e , an d the y us e ser v i c e s pro v i d e d by the ru nti m e . Th e .NE T fra m e w o r k is a m a n a g e d en vi r o n m e n t : the ru nti m e m a n a g e s m e m o r y all o c a t i o n t h i s fre e s the de v e l o p e r fro m that tas k. Pr o g r a m m e r s do not ha v e to de a l wit h the bur d e n of m e m o r y m a n a g e m e n t an d eli m i n a t i n g m e m o r y lea k s . C# su p p o r t s op e r a t o r ov e r l o a d i n g , si m i l a rl y to C++; Ja v a an d O bj e c t i v e- C do not su p p o r t it. Inh e r i t a n c e- rel at e d feat u r e s are cle a r e r an d m o r e pr e c i s e tha n in C+ +: A n abstra ct ke y w o r d is us e d inst e a d of de cl a r i n g pur e virt u a l fun c ti o n s . A b s t r a c t cla s s e s ar e de c l a r e d as su c h dire c t l y by usi n g the abstra ct ke y w o r d , not indir e c t l y by lac k of m e t h o d/f u n c t i o n im pl e m e n t a t i o n ("pure virt u a l fun c ti o n s "). A n ab s t r a c t cla s s m a y be co m p l e t e l y im pl e m e n t e d an d yet be de cl a r e d ab s t r a c t for arc h i t e c t u r a l re a s o n s . A n override ke y w o r d is us e d for ov e r ri d i n g a virt u a l m e t h o d . N e w cla s s m e m b e r typ e s , su c h as pr o p e r t i e s an d ind e x e r s , are intr o d u c e d to si m p l i f y co m m o n tas k s su c h as list iter a ti o n an d pro p e r t y getti n g an d setti n g. Less Error Prone C# is les s err o r- pr o n e tha n C+ + du e to the foll o w i n g ch a r a c t e r i s t i c s : .1 U nli k e C++, w h e r e it is po s s i b l e to de cl a r e an y typ e of poi n t e r an d as si g n it to an y allo c a t e d ad d r e s s or ca s t it to an y oth e r typ e, C# allo w s onl y typ e- saf e ca s ti n g . C a s ti n g err o r s ar e det e c t e d at eith e r co m p i l e ti m e or at ru n ti m e. H o w e v e r , in eith e r situ a t i o n , yo u will not mi s s an y err o r s. Th e us e of G e n e r i c typ e s allo w s te m p l a t e cla s s e s to re m a i n typ e- saf e at co m p i l e ti m e. Thi s all o w s the pr o g r a m m e r to det e c t err o r s duri n g co m p i l e , rat h e r tha n at run ti m e. R u n n i n g un d e r the m a n a g e d C L R , bo u n d s ch e c k i n g is perf o r m e d for m a n a g e d arr a y s aut o m a t i c a l l y w hil e ac c e s s i n g the allo c a t e d arr a y m e m b e r obj e c t. Thi s pr e v e n t s ac ci d e n t a l m e m o r y corr u p t i o n .

.2

.3

Modern C# is de si g n e d to m e e t m o d e r n ap pl i c a t i o n req u i r e m e n t s , as w ell as inte g r a t i n g m o d e r n soft w a r e co n c e p t s : .4 C#, as a .N E T lan g u a g e , pro v i d e s a larg e r set of ba si c typ e s , incl u d i n g for ex a m p l e the D e c i m a l typ e us e d to ha n d l e fina n c i a l ap p li c a t i o n s corr e c t l y.

Page | 4

.5

K n o w n co n c e p t s , su c h as en u m e r a t e d typ e s, bit flag s, first- cla s s fun c ti o n s an d oth e r s, be c o m e first- cla s s citi z e n typ e s wit h a rich set of fun c ti o n a l i ti e s that re p r e s e n t typ e- sp e c i fi c us a g e re q u i r e m e n t s . It co n t a i n s a built- in ex c e p t i o n- ha n d l i n g m e c h a n i s m . C# pro v i d e s a built- in ev e n t ha n d l e r for si m p l e r ha n d l i n g of inte g r a t e d (for ex a m p l e , G U I an d W e b) ap pl i c a t i o n re q u i r e m e n t s . C# pro v i d e s a co m p l e t e refl e c t i o n m e c h a n i s m , allo w i n g obj e c t m e t a d a t a to be ex p l o r e d during run time .

.6 .7 .8

ObjectOriented C# su p p o r t s the thr e e pill ar s of O bj e c t- Ori e n t e d Pr o g r a m m i n g : Encap sulation A cla s s ha s tool s to hi d e its im pl e m e n t a t i o n det ai l s an d be res p o n s i b l e for its obj e c t s stat e Inheritance Th e is a rel ati o n s h i p ca n be defi n e d bet w e e n cla s s e s , en a b l i n g ext e n s i b i l i t y an d re u s a b i l i t y Polymorphis m A pr o g r a m ca n ha n d l e diff e r e n t typ e s of obj e c t s ide n ti c a l l y, sh a ri n g the sa m e ba s e, ha vi n g pol y m o r p h i c be h a v i o r , ba s e d on sp e c i fi c obj e c t typ e s Ev e r y t h i n g in C# is part of a typ e (for ex a m p l e , cla s s, inte rf a c e , an d str u c t). Th e r e are no glo b a l (non- m e m b e r) fun c ti o n s or vari a b l e s . Ev e r y typ e inst a n c e is an object . All typ e s ar e deri v e d fro m a co m m o n ba s e cla s s: object . W h i l e the s e co n c e p t s ar e ver y fa m i li a r to Ja v a de v e l o p e r s , the y ar e fun d a m e n t a l l y diff e r e n t (and in so m e ca s e s ev e n co m p l e t e l y ne w) for C/C+ +/O b j e c t i v e- C de v e l o p e r s . A few words about Java Ja v a is a pro g r a m m i n g lan g u a g e ori gi n a l l y de v e l o p e d by Ja m e s G o s l i n g at S u n Mi c r o s y s t e m s an d rel e a s e d in 1 9 9 5 as a cor e co m p o n e n t of S u n Mi c r o s y s t e m s Ja v a platf o r m . Th e lan g u a g e deri v e s m u c h of its sy n t a x fro m C an d C+ + but ha s a si m p l e r obj e c t m o d e l an d fe w e r lo w- lev el faciliti e s. Ja v a ap pl i c a t i o n s typi c a l l y are co m p i l e d to byt e c o d e (clas s file) that ca n run on an y Ja v a Virt u a l M a c h i n e (JV M) reg a r d l e s s of co m p u t e r arc h i t e c t u r e . Ja v a is ge n e r a l- pur p o s e , co n c u r r e n t , cla s s- ba s e d , an d obj e c t- ori e n t e d . It is de s i g n e d sp e c i fi c a l l y to ha v e as fe w im pl e m e n t a t i o n de p e n d e n c i e s as po s s i b l e . A few words about ObjectiveC Th e O bj e c t i v e- C lan g u a g e is a si m p l e co m p u t e r lan g u a g e de si g n e d to en a b l e so p h i s t i c a t e d obj e c t- ori e n t e d pr o g r a m m i n g . O bj e c t i v e- C is defi n e d as a s m a l l but po w e r f u l set of ext e n s i o n s to Page | 5

the sta n d a r d A N S I C lan g u a g e . It is po s s i b l e to co m p i l e an y C pro g r a m wit h an O bj e c t i v e- C co m p i l e r, an d to fre el y incl u d e C co d e wit hi n an O bj e c t i v e- C cla s s. Its ad di ti o n s to C are m o s t l y ins pi r e d by S m a l l t a l k , on e of the first obj e c t- ori e n t e d pro g r a m m i n g lan g u a g e s . All of the sy n t a x for no n- obj e c t- ori e n t e d op e r a t i o n s (inclu d i n g pri m i ti v e vari a b l e s , pre p r o c e s s i n g , ex p r e s s i o n s , fun c ti o n de cl a r a t i o n s , an d fun c ti o n call s) is ide n ti c a l to C, w hil e the sy n t a x for obj e c t- ori e n t e d feat u r e s is an im pl e m e n t a t i o n of S m a l l t a l k- styl e m e s s a g i n g . To d a y , O bj e c t i v e- C is us e d pri m a r i l y on Ap p l e ' s M a c O S X an d iO S: tw o en vi r o n m e n t s ba s e d on the O p e n S t e p sta n d a r d , tho u g h not co m p l i a n t wit h it. O bj e c t i v e- C is the pri m a r y lan g u a g e us e d for A p p l e ' s C o c o a A P I, an d it w a s ori gi n a l l y the m ai n lan g u a g e on N e X T ' s N e X T S T E P O S.

Hello C#! Hi st o r i c a l l y, all arti cl e s ab o u t pr o g r a m m i n g lan g u a g e s start wit h a Hell o W o r l d! sa m p l e . Thi s arti cl e will foll o w thi s tra di ti o n h e r e is a C# ver si o n of thi s po p u l a r pro g r a m : C#
using Syste m; class HelloApp { sta ti c void Main() { Console.WriteL i n e("Hello C#!"); } }

Thi s co d e ref e r e n c e s the "Sys t e m " na m e s p a c e , w hi c h pr o v i d e s the "Co n s o l e " cla s s, de cl a r e s a cla s s an d en cl o s e d "M ai n" stati c m e t h o d , w hi c h , in tur n, call s the C o n s o l e cla s s s "Writ e L i n e " stati c m e t h o d to out p u t a stri n g. Note: R u n n i n g the ab o v e sa m p l e dir e c tl y un d e r W i n d o w s P h o n e 7 will not pro d u c e an y tan g i b l e out p u t, as the Wi n d o w s Ph o n e 7 co n s o l e is not ac c e s s i b l e . Th e co d e pr o v i d e d is onl y for co m p a r i s o n .

Let us se e the sa m e pr o g r a m writt e n in Ja v a: Java


public class HelloWor ld { publi c sta ti c void mai n(Str i n g[] args) { Syste m.o u t.p r i n t l n("Hello Java!"); }

Page | 6

An d no w in O bj e c t i v e- C: Objective- C
#impo r t <stdio.h>

in t main( int argc, const cha r *argv[] ) { pri n t f( "Hello Object ive - C!\n" ); ret u r n 0; }

Th e first diff e r e n c e is in co d e org a n i z a t i o n : w hil e bot h C# an d Ja v a ha v e a cla s s wit h on e m e t h o d , O bj e c t i v e- C ha s just a m e t h o d . Th e se c o n d diffe r e n c e lies in the sig n a t u r e of the m e t h o d : in C#, the Mai n m e t h o d m u s t start wit h ca p i t a l M, w hil e in Ja v a an d O bj e c t i v e- C, it is not a req u i r e m e n t . Th e Stri n g[] arg u m e n t for the Mai n m e t h o d in C# is opti o n a l . Th e thir d diffe r e n c e is in the M ai n m e t h o d s ret u r n typ e: in C# (and Ja v a), it ret u r n s voi d w hil e in O bj e c t i v e- C it ret u r n s int. In C#, the M ai n m e t h o d co ul d als o ret u r n an int val u e . Th e m o r e w e ex a m i n e the co d e the m o r e diff e r e n c e s w e will dis c o v e r . Defining a Class M u c h of obj e c t- ori e n t e d pr o g r a m m i n g co n s i s t s of writi n g co d e for ne w obj e c t s d e f i n i n g ne w cla s s e s . Th e cla s s defi ni ti o n s in C# an d Ja v a ar e fairl y si m il a r: C# (and Java)
class Frac t i o n { publi c int nu me r a t o r ; publi c int deno m i n a t o r ;

publi c void Prin t() { //C# Console.Wri teLi n e("{0}/{1}", nu m e r a t o r , deno m i n a t o r ); //Java //Syste m.o u t.p r i n t l n(new Prin t f F o r m a t("%d/%d").spri n t f(n u m e r a t o r , deno m i n a t o r)); }

Page | 7

In O bj e c t i v e- C, cla s s e s are defi n e d in tw o part s: .1 .2 A n inte rf a c e that de cl a r e s the m e t h o d s an d inst a n c e vari a b l e s of the cla s s an d na m e s its su p e r c l a s s A n im pl e m e n t a t i o n that act u a l l y defi n e s the cla s s (cont ai n s the co d e that im pl e m e n t s its m e t h o d s)

Ty p i c a l l y, the s e are split bet w e e n tw o files, tho u g h so m e t i m e s a cla s s defi n i ti o n m a y sp a n se v e r a l file s by usi n g a feat u r e call e d a cat e g o r y . Th e sa m e cla s s/int e r f a c e writt e n in O bj e c t i v e- C: Objective- C
#impo r t <Foun d a t i o n / NSObje c t.h>

@inte r f a c e Frac t i o n: NSObject { int nu me r a t o r ; int deno m i n a t o r ; }

- (void) pri n t; - (void) setNu m e r a t o r : (int) n; - (void) setDeno m i n a t o r : (int) d; - (int) nu me r a t o r ; - (int) deno m i n a t o r ; @end

Objective- C
#impo r t "Frac t i o n. h" #impo r t <stdio.h>

@imple m e n t a t i o n Frac t i o n - (void) pri n t { pri n t f( "%i/%i", nu m e r a t o r , deno m i n a t o r ); }

Page | 8

- (void) setNu m e r a t o r : (int) n { nu m e r a t o r = n; }

- (void) setDeno m i n a t o r : (int) d { deno m i n a t o r = d; }

- (int) deno m i n a t o r { ret u r n deno m i n a t o r ; }

- (int) nu me r a t o r { ret u r n nu m e r a t o r ; } @end

Th e us a g e of the cla s s is as foll o w s : C#


// Crea te a new insta n ce Frac t i o n frac t i o n = new Frac t i o n(); // Set the values frac t i o n. n u m e r a t o r = 5; frac t i o n.d e n o m i n a t o r = 10; // Prin t it frac t i o n.P r i n t();

Java
// Crea te a new insta n ce final Frac t i o n fra c t i o n = new Frac t i o n(); // Set the values frac t i o n. n u m e r a t o r = 5; frac t i o n.d e n o m i n a t o r = 10; // Prin t it frac t i o n.P r i n t();

Objective- C Page | 9

// Crea te a new insta n ce Frac t i o n *frac = [[Frac t io n alloc] init];

// Set the values [frac setN u m e r a t o r : 1]; [frac setDeno m i n a t o r : 3];

// Prin t it prin t f( "The fra c t i o n is: " ); [frac pri n t]; prin t f( "\n" );

// Free me m o r y [frac release];

Fr e e i n g m e m o r y is vital in O bj e c t i v e- C pr o g r a m m i n g sin c e the m e m o r y is m a n a g e d by the ap p li c a t i o n , w hil e in bot h C# an d Ja v a it m a n a g e d by the ru nti m e (that will be dis c u s s e d late r). Th e w h o l e co n c e p t of m e t h o d call s (as it exi st s in C# an d Ja v a) is diff e r e n t in the O bj e c t i v e- C w o rl d w h e n co m p a r i n g the lan g u a g e sy n t a x . To get an obj e c t to do so m e t h i n g , yo u se n d it a m e s s a g e telli n g it to ap pl y a m e t h o d . In O bj e c t i v e- C, m e s s a g e stat e m e n t s ar e en cl o s e d in br a c k e t s : Objective- C
[receive r message]

Th e rec e i v e r is an obj e c t, an d the m e s s a g e tells it w h a t to do. In so u r c e co d e, the m e s s a g e is si m p l y the na m e of a m e t h o d an d an y ar g u m e n t s that are pa s s e d to it. W h e n a m e s s a g e is se n t, the run- ti m e sys t e m sel e c t s the ap p r o p r i a t e m e t h o d fro m the rec e i v e r s re p e r t o i r e an d inv o k e s it. .NET Runtime M u c h like Ja v a ap p li c a t i o n s , C# ap pl i c a t i o n s w o r k wit hi n a m a n a g e d ru n- ti m e en vi r o n m e n t call e d the co m m o n lan g u a g e ru nti m e (CL R) , w hi c h pr o v i d e s a nu m b e r of ser vi c e s to the ap p li c a t i o n . C o m p i l e r s an d tool s ex p o s e the co m m o n lan g u a g e ru nti m e ' s fun c t i o n a l i t y an d en a b l e yo u to writ e co d e that be n e f i t s fro m this m a n a g e d ex e c u t i o n en vi r o n m e n t . C o d e de v e l o p e d wit h a lan g u a g e co m p i l e r that tar g e t s the C L R is call e d m a n a g e d co d e . It be n e f i t s fro m feat u r e s su c h as cro s s- lan g u a g e inte g r a t i o n , cro s s- lan g u a g e ex c e p t i o n ha n d l i n g , en h a n c e d se c u r i t y, ver si o n i n g an d de p l o y m e n t su p p o r t, a si m p l i fi e d m o d e l for co m p o n e n t inter a c t i o n , an d de b u g g i n g an d profili n g ser vi c e s . To en a b l e the ru nti m e to pr o v i d e ser v i c e s to m a n a g e d co d e , lan g u a g e co m p i l e r s m u s t e m i t m e t a d a t a that de s c r i b e s the typ e s, m e m b e r s , an d ref e r e n c e s in the co m p i l e d co d e . M e t a d a t a is Pa g e | 1 0

stor e d wit h the co d e . Ev e r y loa d a b l e C L R port a b l e ex e c u t a b l e (PE) file co n t a i n s m e t a d a t a . Th e ru nti m e us e s m e t a d a t a to loc a t e an d loa d cla s s e s , lay out inst a n c e s in m e m o r y , res o l v e m e t h o d inv o c a t i o n s , ge n e r a t e nati v e co d e, enf o r c e se c u r i t y, an d set ru n- ti m e co n t e x t bo u n d a r i e s . Th e ru nti m e aut o m a t i c a l l y ha n d l e s obj e c t lay o u t an d m a n a g e s ref e r e n c e s to obj e c t s , rel e a s i n g the m w h e n the y ar e no lon g e r bei n g us e d (this is call e d gar b a g e coll e c ti o n). O bj e c t s m a n a g e d by the ru nti m e are call e d m a n a g e d dat a. G a r b a g e coll e c ti o n eli m i n a t e s m e m o r y lea k s as w ell as so m e oth e r co m m o n pr o g r a m m i n g err o r s. Y o u ca n us e m a n a g e d dat a, un m a n a g e d dat a, or bot h m a n a g e d an d un m a n a g e d dat a in yo u r .N E T Fr a m e w o r k ap p li c a t i o n . Th e C L R m a k e s it ea s y to de si g n co m p o n e n t s an d ap pl i c a t i o n s , w h o s e obj e c t s inte r a c t acr o s s lan g u a g e s . O bj e c t s writt e n in diff e r e n t lan g u a g e s ca n co m m u n i c a t e wit h ea c h oth e r, an d thei r be h a v i o r s ca n be inte g r a t e d tigh tl y.

.NET and C# Program Basics A C# pr o g r a m file sh o u l d en d wit h the .cs ext e n s i o n (in Ja v a the file sh o u l d en d wit h the .java ext e n s i o n an d in O bj e c t i v e- C wit h the .m ext e n s i o n). All the co d e in C# is writt e n wit hi n cla s s defi ni ti o n s t h e r e are no glo b a l entiti e s in the .NE T obj e c t- ori e n t e d w o rl d. All cla s s co d e is writt e n wit hi n the cla s s defi ni ti o n the r e is no se p a r a t i o n bet w e e n cla s s defi n i ti o n an d im pl e m e n t a t i o n . All m e t h o d s ar e writt e n inlin e. .N E T cla s s e s are defi n e d wit hi n na m e s p a c e s . Int e r n a l l y, the na m e of the na m e s p a c e in w hi c h a cla s s is defi n e d be c o m e s part of the cla s s na m e . Th u s , diffe r e n t na m e s p a c e s ca n incl u d e ide n ti c a l cla s s na m e s sin c e the full na m e of the cla s s is diff e r e n t. Th e na m e s p a c e na m e us u a l l y de s c r i b e s the ge n e r a l fun c t i o n a l i t y topi c, w hil e the cla s s na m e de s c r i b e s the cla s s sp e c i fi c rol e in that ge n e r a l ar e a. N a m e s p a c e m e m b e r s are us e d by pr efi xi n g the m e m b e r na m e wit h the na m e s p a c e na m e foll o w e d by the dot (.) op e r a t o r . Fo r ex a m p l e , to us e the Con sole cla s s, w hi c h is defi n e d in the Sy stem na m e s p a c e , w e ca n writ e: Sy stem.Con s ole .Writ e L i n e( ). A m o r e ab b r e v i a t e d w a y is to bri n g the req u i r e d na m e s p a c e into the pro g r a m s na m e s p a c e , usi n g the using ke y w o r d : C#
using Syste m;

Wi t h this stat e m e n t , Sy stem na m e s p a c e m e m b e r s ca n be dire c t l y us e d, wit h no na m e s p a c e rel at e d pr efi x ad di ti o n s : Console .Writ e L i n e( ). W e ca n defi n e our o w n na m e s p a c e s to ser v e as an org a n i z a t i o n a l pa c k a g e for a fe w rel at e d typ e s. Yo u ca n de cl a r e yo u r o w n na m e s p a c e usi n g the namesp a c e ke y w o r d : C#
na mes p a ce Grap h i cs

Pa g e | 1 1

{ class Recta n g le{} class Circle{} }

.NET Common Type System Th e .N E T fra m e w o r k defi n e s a typ e sy st e m , co m m o n to all lan g u a g e s that tar g e t the .NE T en vi r o n m e n t , kn o w n as the C o m m o n Ty p e Sy s t e m (CT S). Th e C T S defi n e s a rich set of typ e s that ca n be ho s t e d by the C L R (for ex a m p l e , stru c t s , cla s s e s , en u m e r a t o r s , inter f a c e s , an d del e g a t e s) an d thei r ba si c defi ni ti o n rul e s. Th e C T S als o pro v i d e s a rich set of ba si c typ e s , w hi c h will be pr e s e n t e d so o n . B a s e d on this ext e n s i v e typ e sys t e m , the .NE T Fr a m e w o r k defi n e s a set of rul e s that are ter m e d the C o m m o n La n g u a g e S p e c i f i c a t i o n (CLS). Th e C L S is the mi ni m u m sp e c i fi c a t i o n that an y .N E T co m p l i a n t lan g u a g e (a lan g u a g e im pl e m e n t e d on top of the C L R) m u s t im pl e m e n t . Th e C T S pro v i d e s a rich set of pr e- defi n e d ba si c typ e s for all .N E T lan g u a g e s to us e. Thi s set co n t a i n s pri m i ti v e typ e s , su c h as Sy stem.Int32 an d Sy stem.Double , as w ell as oth e r co m m o n l y us e d typ e s, su c h as Sy stem.Deci m al an d Sy stem. String . Th e ba s e of all .N E T typ e s, incl u d i n g the .N E T pri m i ti v e typ e s , is Sy stem.Object . By defi n i n g a co m m o n ba s e , .N E T allo w s tre a ti n g all typ e s in co m m o n ter m s , su c h as defi ni n g coll e c t i o n typ e s to sto r e an y typ e of ele m e n t s , defi ni n g a ge n e r i c m e t h o d that ca n ac c e p t an y ar g u m e n t typ e, an d so on. Sy stem.Object co n t a i n s a set of m e m b e r s that ar e ap pl i c a b l e to an y .N E T typ e. So m e of the s e m e m b e r s ar e virt u a l. Th e r e f o r e , the y ca n be ov e r r i d d e n by deri vi n g typ e s to bett e r re pr e s e n t thei r ch a r a c t e r i s t i c s . In the .N E T en vi r o n m e n t , typ e s ar e divi d e d into tw o diff e r e n t cat e g o r i e s : .3 R e f e r e n c e Ty p e s R e f e r e n c e typ e obj e c t s are all o c a t e d fro m the he a p . A ref e r e n c e typ e vari a b l e co n t a i n s onl y a ref e r e n c e to the act u a l dat a, w hi c h resi d e s on the he a p . .4 A cla s s inst a n c e is an ex a m p l e of a ref e r e n c e typ e. C o n s i d e r the foll o w i n g cla s s: C#
class Emp lo yee { publi c Emplo yee(str i n g na me)// priva t e str i n g na me; }

.5 Th e inst a n c e of su c h a cla s s co ul d be cre a t e d as foll o w s : C# Pa g e | 1 2

Emp lo yee e = new Emplo ye e(John Doe);

Note: Thi s stat e m e n t all o c a t e s sp a c e for an Employee obj e c t on the he a p , as si g n s it to the re q u i r e d dat a, an d ret u r n s a ref e r e n c e to this obj e c t, to be stor e d in the ref e r e n c e vari a b l e e. .6 V al u e Ty p e s Val u e- typ e d obj e c t s are all o c a t e d inlin e, eith e r on the sta c k or as part of a co n t a i n i n g obj e c t. .7 Pri m i ti v e typ e vari a b l e s , su c h as int, are an ex a m p l e of a val u e typ e: C#
in t i = 32;

Note: Thi s stat e m e n t all o c a t e s 32-bits of sp a c e on the sta c k an d pla c e s a 32bit val u e into thi s sp a c e . Note: In C#, the obj e c t s loc a ti o n (stac k vs. he a p) is not det e r m i n e d by the obj e c t s cre a ti o n m e t h o d (assi g n m e n t op e r a t o r vs. ne w). Th e vari a b l e typ e (valu e- typ e vs. ref e r e n c e- typ e) det e r m i n e s w h e r e the vari a b l e will be cre a t e d , ex c e p t for bo x e d val u e typ e s 1 . Fr o m the .N E T fra m e w o r k cla s s hier a r c h y s per s p e c t i v e , ref e r e n c e typ e s dir e c tl y deri v e fro m Sy stem.Object ; val u e typ e s , ho w e v e r , deri v e fro m Sy stem.ValueType , w hi c h deri v e s fro m Sy stem.Object. Th e ValueType cla s s adj u s t s the O bj e c t cla s s im pl e m e n t a t i o n (overri d e s so m e of its m e t h o d s) to bett e r suit a val u e- typ e vari a b l e s be h a v i o r . As part of the saf e t y co n c e p t pre v a l e n t in .N E T, uni ni ti a li z e d vari a b l e us a g e is im p o s s i b l e : vari a b l e s are eith e r aut o m a t i c a l l y initi ali z e d wit h def a u l t val u e s (suc h as wit h cla s s m e m b e r s an d arr a y el e m e n t s), or the co m p i l e r pre v e n t s thei r us a g e (suc h as loc al m e t h o d vari a b l e s). W h e n aut o m a t i c a l l y as si g n e d to thei r def a u l t s, ref e r e n c e typ e vari a b l e s def a u l t to null , nu m e r i c val u e typ e vari a b l e s def a u l t to zer o, an d Bo o l e a n vari a b l e s def a u l t to false . Thi s pro c e s s als o do e s a rec u r s i v e initi ali z a t i o n of struct an d clas s m e m b e r s .

Bo x i n g is th e pr o c e s s of co n v e r t i n g a val u e typ e to th e typ e object or to an y inte r f a c e typ e im p l e m e n t e d by this val u e typ e. W h e n th e C L R bo x e s a val u e typ e, it wr a p s the val u e insi d e a Sy s t e m . O b j e c t an d stor e s it on th e m a n a g e d he a p . U n b o x i n g extr a c t s th e val u e typ e fro m the obj e c t. B o x i n g is im p li c i t; un b o x i n g is ex p li c i t. Th e co n c e p t of bo x i n g an d un b o x i n g un d e r l i e s th e C# unifi e d vie w of th e typ e sy s t e m , in w h i c h a val u e of an y typ e ca n be tre a t e d as an obj e c t.

Pa g e | 1 3

C# Classes C# cla s s e s su p p o r t infor m a t i o n hidi n g by en c a p s u l a t i n g fun c ti o n a l i t y in pro p e r t i e s an d m e t h o d s an d by en a b l i n g se v e r a l typ e s of pol y m o r p h i s m , incl u d i n g su b t y p i n g pol y m o r p h i s m via inh e r i t a n c e an d par a m e t r i c pol y m o r p h i s m via ge n e r i c s . Y o u defi n e cla s s e s by usi n g the ke y w o r d "cla s s" foll o w e d by an ide n ti fi e r t h e na m e of the cla s s. N e x t, yo u ca n cre a t e inst a n c e s of the cla s s by usi n g the "ne w" ke y w o r d foll o w e d by the na m e of the cla s s. As pr e v i o u s l y dis c u s s e d , cla s s e s are ref e r e n c e- typ e vari a b l e s . Bef o r e w e get de e p e r into the C# cla s s e s , let us intr o d u c e C# ac c e s s m o d i fi e r s. Th e r e ar e fou r m ai n ac c e s s m o d i fi e r s :
Accessibility Public Protected Meaning Access is not restricted Access is limited to the containing class or types derived from the containing class Access is limited to the current assembly (project) Access is limited to the containing type

Internal Private

In ad di ti o n , the protected internal ac c e s s m o d i fi e r co m b i n a t i o n ca n be us e d . Thi s m e a n s that ac c e s s is li mit e d to the curr e n t as s e m b l y or typ e s deri v e d fro m the co n t a i n i n g cla s s. Note: Y o u ca n als o ap p l y the pr e v i o u s ac c e s s m o d i fi e r s to cla s s m e m b e r s . A cla s s m a y co n t a i n a rich set of m e m b e r typ e s su c h as fiel d s, co n s t a n t s , m e t h o d s , co n s t r u c t o r s , de s t r u c t o r , pro p e r t i e s , ind e x e r s , op e r a t o r s , an d ev e n t s . Yo u ca n defi n e a cla s s as stati c by usi n g the static ke y w o r d in the cla s s defi n i ti o n . It is not po s s i b l e to cre a t e inst a n c e s of a stati c cla s s s t a t i c cla s s e s are loa d e d aut o m a t i c a l l y by the .N E T ru nti m e w h e n the pro g r a m or na m e s p a c e co n t a i n i n g the cla s s is loa d e d an d us e d . Th e onl y co n s t r u c t o r per m i t t e d in a stati c cla s s is the stati c co n s t r u c t o r . A stati c co n s t r u c t o r will be call e d on c e at m o s t, an d will be call e d bef o r e the first ti m e a stati c m e m b e r of the typ e is ac c e s s e d . An inst a n c e co n s t r u c t o r (if on e exi st s) will al w a y s run aft e r a stati c co n s t r u c t o r .

C# Class Members Fi el d s (kno w n in C/C+ + as dat a m e m b e r s) ar e initi ali z e d aut o m a t i c a l l y wit h a def a u l t val u e ac c o r d i n g to thei r typ e cat e g o r y (refer e n c e- typ e vs. val u e- typ e). By def a u l t, a fiel d is defi n e d to be an inst a n c e fiel d a fiel d that is co n t a i n e d in ev e r y inst a n c e of the cla s s, hol d i n g an inst a n c e s sp e c i fi c val u e. H o w e v e r , a fiel d ca n be m a d e stati c, in w hi c h ca s e its val u e is sh a r e d by all cla s s inst a n c e s . Y o u defi n e stati c fiel d s usi n g the static ke y w o r d . P u b l i c stati c fiel d s ca n onl y be ac c e s s e d thr o u g h the cla s s na m e usi n g the dot (.) op e r a t o r . An o t h e r cla s s m e m b e r , the m e t h o d , is a fun c ti o n a l m e m b e r of the cla s s. In C#, all m e t h o d s ar e defi n e d insi d e the cla s s de cl a r a t i o n . Cl a s s m e t h o d s ca n ac c e s s oth e r cla s s m e m b e r s : Pa g e | 1 4

C#
public void raiseSala r y(in t per ce n t a g e) { // Upda te the sala r y field sala r y += sala r y * perce n t a g e / 1 00; }

Th e obj e c t on w hi c h the m e t h o d w a s acti v a t e d is av a il a b l e insi d e the m e t h o d usi n g the ke y w o r d this . O b s e r v e the foll o w i n g sa m p l e w h e r e cla s s fiel d s ar e up d a t e d wit h the val u e of ide n t i c a l l yna m e d par a m e t e r s : C#
public Emp lo yee(str i n g na me, in t sala r y) { this.na m e = na me; this.sala r y = sala r y; }

M e t h o d s that m a n i p u l a t e stati c fiel d s onl y sh o u l d be defi n e d as stati c m e t h o d s . T he y ca n be acti v a t e d usi n g the cla s s na m e alo n e an d ha v e no ac c e s s to an y no n- stati c fiel d in the cla s s. By def a u l t, m e t h o d par a m e t e r s in C# ar e pa s s e d by val u e . .8 W h e n the par a m e t e r is of a val u e- typ e, a loc al co p y of the ar g u m e n t s val u e is cre a t e d for the m e t h o d . W o r k i n g on thi s loc a l ind e p e n d e n t val u e s co p y , the m e t h o d ha s no infl u e n c e on the ori gi n a l ar g u m e n t s val u e. W h e n the par a m e t e r is of a ref e r e n c e- typ e, a loc al co p y of the arg u m e n t s ref e r e n c e is cre a t e d for the m e t h o d . Thr o u g h thi s ref e r e n c e co p y , the m e t h o d ca n alter the ori gi n a l ar g u m e n t s val u e , tho u g h it ca n n o t ch a n g e the ref e r e n c e itsel f.

.9

So m e t i m e s w e w o u l d like the m e t h o d to w o r k dire c t l y on the ar g u m e n t pa s s e d , be it a val u e or ref e r e n c e typ e, inst e a d of w o r k i n g on an ind e p e n d e n t loc a l co p y. To get su c h be h a v i o r , C# pr o v i d e s us wit h a sp e c i a l kin d of par a m e t e r r e f e r e n c e par a m e t e r s . Note: Thi s is the sa m e as pa s s i n g the ad d r e s s of the vari a b l e in C, or a ref e r e n c e in C+ +. Yo u defi n e ref e r e n c e par a m e t e r s by ad di n g the ref ke y w o r d bef o r e the par a m e t e r typ e in the m e t h o d s sig n a t u r e , an d ag ai n in the m e t h o d call. Th e ref ke y w o r d m u s t be sp e c i fi e d in bot h. C#
// Func t i o n defin i t i o n public void Swap(ref int i1, ref int i2)

Pa g e | 1 5

{ int tm p = i1; i1 = i2; i2 = tm p; } // Func t i o n call in t i1 = 5, i2 = 10; ... Swap(ref i1, ref i2);

R ef e r e n c e par a m e t e r s sh o u l d be initi ali z e d bef o r e the y are pa s s e d to the fun c ti o n . In so m e ca s e s , this is inc o n v e n i e n t , es p e c i a l l y w h e n thei r val u e is co m p u t e d by the fun c ti o n . Fo r su c h ca s e s , C# offe r s an o t h e r kin d of par a m e t e r : out p u t par a m e t e r s . O u t p u t par a m e t e r s allo w us to pa s s no n- initi ali z e d vari a b l e s to a m e t h o d . Th e m e t h o d ca n the n defi n e the m usi n g the out ke y w o r d . O u t par a m e t e r s m u s t be as si g n e d insi d e the m e t h o d . Faili n g to as si g n out par a m e t e r s ge n e r a t e s a co m p i l e r err o r. As wit h the ref ke y w o r d , bot h the fun c ti o n defi n i ti o n an d call m u s t incl u d e the out ke y w o r d to us e out p u t par a m e t e r s . C#
// Func t i o n defin i t i o n public void getSize(out in t wid t h, out in t heig h t) { wid t h = this.wid t h; heig h t = this.heig h t; } //Fun c t i o n call in t wid t h, heigh t; ... getSize(out wid t h, out heig h t);

To en a b l e us e r s to ac c e s s cla s s fiel d s in an ele g a n t yet co n t r o l l e d m a n n e r , C# pre s e n t s pr o p e r t i e s . Pr o p e r t i e s ca n be co n s i d e r e d as smart fields . Th e y en a b l e defi n i n g sp e c i a l get an d set m e t h o d s , ter m e d getter an d setter , w hi c h ar e acti v a t e d usi n g si m p l e fiel d ac c e s s sy n t a x . Thi s w a y, w e ca n pr o v i d e the us e r s wit h an intuiti v e fiel d ac c e s s ser v i c e , yet still perf o r m vali d a t i o n (and an y oth e r re q u i r e d) op e r a t i o n s . Yo u defi n e pro p e r t i e s as foll o w s : C#
//Field to sto re the pro pe r t y value priva te in t day;

Pa g e | 1 6

// Prope r t y public in t Day { // Gette r get { // Any fun c t i o n a l i t y could be execu t e d here in orde r to alte r // the re t u r n value re t u r n day; } // Sette r set { // Any fun c t i o n a l i t y could be execu t e d here in orde r to valida te // a value befo r e it being assigne d to the priva te field // Note: new pro pe r t y value is always provid e d by the keywo r d value if (isDayOK (value)) day = value; } }

By def a u l t, pr o p e r t y gett e r an d sett e r ac c e s s is pro v i d e d by the pr o p e r t y defi n i ti o n (in the pr e v i o u s ex a m p l e the y bot h are pu bl i c). Y o u ca n ex p li ci tl y ch a n g e an ac c e s s o r s visi bilit y to diff e r fro m that of the pro p e r t y , tho u g h the ac c e s s i b i l i t y m o d i fi e r of the ac c e s s o r m u s t be m o r e restri c ti v e tha n that of the pro p e r t y . O nl y on e of the ac c e s s o r s m a y ha v e its visi bilit y alte r e d . C#
public FieldType Prope r t y N a m e { get { re t u r n field; } pro te c t e d set {

Pa g e | 1 7

field = value; } }

C# Structs W h i l e a cla s s is a ver y ge n e r a l typ e, the .N E T C T S defi n e s ad di ti o n a l , m o r e sp e c i fi c, typ e s that m a y bett e r suit sp e c i fi c ap pl i c a t i o n req u i r e m e n t s . Th o u g h w e w o u l d us u a l l y us e cla s s e s to defi n e our typ e s, cla s s e s mi g h t so m e t i m e s be too he a v y for us. Cl a s s obj e c t s ar e ref e r e n c e typ e vari a b l e s an d thei r m a n i p u l a t i o n inv ol v e s a lot of ov e r h e a d . W e m a y req u i r e a light w e i g h t typ e, for w hi c h w e w o u l d like m o r e effi ci e n t obj e c t m a n i p u l a t i o n . For su c h ca s e s , C# en a b l e s the pro g r a m m e r to de c l a r e dat a str u c t u r e s , usi n g the ke y w o r d"stru c t". Str u c t u r e s ar e val u e- typ e us e r defi n e d typ e s an d as su c h are deri v e d im pli ci tl y fro m Sy s t e m . V a l u e T y p e . Th e y ar e allo c a t e d an d m a n i p u l a t e d dire c t l y on the sta c k . Yo u defi n e str u c t u r e s usi n g the struct ke y w o r d . Str u c t u r e s , like cla s s e s , ca n ha v e fiel d s an d m e t h o d s (as w ell as pro p e r t i e s an d ev e n t s , w hi c h w e ha v e not co v e r e d yet). Str u c t u r e s ca n defi n e co n s t r u c t o r s , an d ca n ov e r ri d e virt u a l m e t h o d s deri v e d fro m Sy stem.ValueType . H o w e v e r , the y ha v e so m e li mit a ti o n s : .10 Str u c t u r e s ca n n o t be part of an inh e ri t a n c e tre e t h e y ca n n o t inh e r i t fro m oth e r cla s s e s (exc e p t for thei r def a u l t ba s e as val u e typ e s : Sy stem.ValueType ). Th e y are se a l e d , w hi c h m e a n s the y ca n n o t ser v e as ba s e cla s s e s for oth e r cla s s e s . .1 1 Th o u g h usi n g str u c t u r e s sa v e s the pro g r a m fro m ha v i n g to do he a p all o c a t i o n an d re d u c e s G C co s t s (as val u e typ e s do not ha v e to be coll e c t e d), car e l e s s us e of thi s dat a typ e m a y inc u r sig n i fi c a n t co p y i n g co s t s, as val u e typ e s ar e co pi e d w h e n e v e r tra n s f e r r e d a s a m e t h o d ar g u m e n t , to an o t h e r vari a b l e , an d so on. As val u e- typ e obj e c t s, stru c t u r e obj e c t s ca n n o t ha v e a null val u e unl e s s the y are wr a p p e d as a N ull a b l e < T > typ e s . Y o u ca n n o t defi n e yo u r o w n par a m e t e r l e s s co n s t r u c t o r for a str u c t u r e , as it alre a d y ha s a def a u l t (non- ov e r ri d e a b l e) par a m e t e r l e s s co n s t r u c t o r that initi ali z e s all fiel d s an d thei r def a u l t val u e s . Y o u ca n n o t defi n e a de s t r u c t o r for a str u c t u r e .

.12 .13

.14

Bei n g m o r e li mit e d an d acti v a t e d diffe r e n t l y fro m reg u l a r cla s s e s , str u c t u r e s sh o u l d be us e d for light w e i g h t , val u e- typ e obj e c t s . P o s s i b l e ex a m p l e s ar e Poi n t, the C o m p l e x nu m b e r typ e, the Sy s t e m s de ci m a l an d D a t e T i m e typ e s , an d so on. Su c h typ e s ar e act u a l l y a si m p l e co m b i n a t i o n of val u e- typ e s, an d us e r s ex p e c t the m to be h a v e as pri m i ti v e val u e- typ e s (that is, zer o inst e a d of null, se al e d).

Pa g e | 1 8

C# Delegates & Events D u ri n g the co u r s e of de v e l o p m e n t , w e fre q u e n t l y call fun c ti o n s that ex p e c t a function as on e of thei r ar g u m e n t s . Th e s e fun c ti o n s will call ba c k our pr o v i d e d fun c ti o n duri n g thei r ex e c u t i o n . Th e y ar e us u a l l y ter m e d callba c k functions . Th e tra di ti o n a l w a y to pa s s fun c ti o n s as par a m e t e r s w a s by se n d i n g a fun c ti o n poi n t e r that is m e r e l y an ad d r e s s wit h no typ e info r m a t i o n att a c h e d (suc h as the nu m b e r an d typ e of par a m e t e r s , the ret u r n val u e, an d so on). Th e r e f o r e , pa s s i n g fun c ti o n poi n t e r s is not typ e saf e. In the .N E T fra m e w o r k , call b a c k m e t h o d s ar e us e d for se v e r a l pur p o s e s : ev e n t s , thr e a d i n g , ite m filteri n g an d m o r e. To en h a n c e call b a c k m e t h o d su p p o r t , the .N E T fra m e w o r k co m e s wit h a ne w, typ e- saf e m e c h a n i s m ter m e d delegate . Delegate obj e c t s ref e r inter n a l l y to a m e t h o d call b a c k . H o w e v e r , co m p a r e d to fun c ti o n poi n t e r s , the y pro v i d e ric h e r fun c ti o n a l i t y in a m o r e en c a p s u l a t e d an d typ e- saf e m a n n e r . In C#, w h e n defi n i n g a m e t h o d that ac c e p t s a call b a c k fun c ti o n as a par a m e t e r , w e sh o u l d do the foll o w i n g : .15 .16 D e fi n e a del e g a t e typ e that ide n ti fi e s the re q u i r e d call b a c k fun c ti o n pr ot o t y p e . D e fi n e our m e t h o d as taki n g a par a m e t e r of the del e g a t e typ e defi n e d .

Us e r s of our m e t h o d will: .17 Cr e a t e an obj e c t of the sp e c i fi e d del e g a t e typ e, pro v i d i n g it wit h thei r call b a c k fun c ti o n as a par a m e t e r . Thi s will w o r k onl y if the call b a c k fun c ti o n foll o w s the del e g a t e s re q u i r e d pr ot o t y p e . S u p p l y the ne w l y cre a t e d del e g a t e obj e c t, w hi c h act u a l l y wr a p s thei r call b a c k fun c ti o n , as a par a m e t e r to the m e t h o d .

.18

C# allo w s yo u to defi n e a sp e c i a l call b a c k wr a p p e r typ e, usi n g the delegate ke y w o r d . As a wr a p p e r of a call b a c k , the typ e dict a t e s a sp e c i fi c sig n a t u r e , for ex a m p l e : C#
public delega te void CarOpe r a t i o n (Car c);

To us e a sp e c i fi e d del e g a t e , yo u cre a t e an obj e c t of that del e g a t e typ e an d pa s s to it the re q u i r e d call b a c k tar g e t (a m e t h o d). Thi s call b a c k m e t h o d sh o u l d m a t c h the del e g a t e s defi n e d sig n a t u r e . Usi n g the del e g a t e defi n e d ab o v e as an ex a m p l e , w e will sh o w so m e fun c t i o n s that co ul d be us e d as call b a c k fun c ti o n s by as si g n i n g the m to inst a n c e s of the ab o v e del e g a t e typ e: C#
public void WashCa r(Car c) { ... }

Pa g e | 1 9

public void FixEngi n e(Car c) { ... }

Yo u ca n the n pa s s a del e g a t e inst a n c e to a ge n e r a l m e t h o d that ex p e c t s to get su c h a del e g a t e obj e c t as a par a m e t e r : C#


public void processCa r s (CarOpe r a t i o n ope ra t i o n);

C# als o offe r s the Fu n c <[ T 1 , T 2 , T 3 ,..], T R e s u l t > an d Acti o n < T 1 [, T 2 , T 3 ,..]> ge n e r i c del e g a t e s , pr o v i d i n g the pro g r a m m e r wit h ge n e r a l pur p o s e del e g a t e s an d eli m i n a t i n g the ne e d to de cl a r e sp e c i fi c on e s for ev e r y ca s e .

Ev e n t s are a pr o g r a m m a b l e logi c a l co n c e p t . Th e y allo w an obj e c t to notif y ob s e r v e r s of inter n a l stat u s ch a n g e s , wit h o u t ha vi n g an y oth e r obj e c t poll or ac c e s s its inter n a l infor m a t i o n . For this pur p o s e , ev e n t s ov e r l o a d the "+=" an d "-=" op e r a t o r s , all o w i n g the pr o g r a m m e r to ad d or re m o v e ev e n t affe c t e d co d e (kno w n as "ev e n t ha n d l e r s "), res p e c t i v e l y , by defi ni n g del e g a t e s to ha n d l e the ev e n t. Yo u m a y se e ev e n t s as a sp e c i a l ca s e of del e g a t e s , w hi c h ca n be inv o k e d onl y fro m wit hi n the de cl a r i n g typ e, as op p o s e d to sta n d a r d del e g a t e s . For ex a m p l e , w h e n a us e r clic k s a butt o n usi n g the m o u s e , se v e r a l obj e c t s in the ap p li c a t i o n w o u l d like to be notifi e d so that the y ca n re a c t ac c o r d i n g l y . By defi ni n g an ev e n t m e m b e r in our cla s s, w e en a b l e ext e r n a l obj e c t s to rec e i v e a notifi c a t i o n w h e n e v e r the ev e n t oc c u r s . Sp e c i fi c a l l y, a typ e defi n i n g an ev e n t offe r s the foll o w i n g abiliti e s: .19 .20 Th e abilit y for ext e r n a l obj e c t s to regi s t e r (and unr e g i s t e r) thei r inter e s t in the ev e n t Th e abilit y for the defi ni n g obj e c t to m ai n t a i n the set of regi s t e r e d obj e c t s an d notif y the m w h e n the sp e c i fi c ev e n t oc c u r s

W h e n an obj e c t re gi s t e r s an inte r e s t in an ev e n t, it pro v i d e s the ev e n t obj e c t wit h a call b a c k m e t h o d to be call e d w h e n the ev e n t oc c u r s . Th e abilit y to stor e an d inv o k e a call b a c k is im pl e m e n t e d usi n g the del e g a t e m e c h a n i s m de s c r i b e d bef o r e . In its call b a c k m e t h o d , the re gi s t e r e d obj e c t ca n res p o n d to the ev e n t s oc c u r r e n c e . G e n e r a l l y , ev e n t s in .N E T us e del e g a t e s of the foll o w i n g sig n a t u r e : C#
public delega te void Delega teNa m e(objec t sende r, Even tA r gs args);

W h e r e : "Del e g a t e N a m e " w o u l d be the del e g a t e na m e defi ni ti o n an d "arg s" w o u l d be a par a m e t e r of typ e "Ev e n t A r g s " or a deri v a t i v e .

Pa g e | 20

For ex a m p l e , a del e g a t e for an ev e n t notif yi n g that a butt o n ha s be e n clic k e d w o u l d loo k like thi s: C#
public delega te void ClickH a n d le r(obje c t sende r, ClickEve n tA r g s args);

To co m p l e t e the ab o v e ex a m p l e , defi n e the ev e n t itself by ad di n g an ev e n t m e m b e r to the cla s s: C#


public even t ClickH a n d l e r Click;

Note: Ev e n t m e m b e r s are us e d like del e g a t e s , instr u c t i n g the rec e i v e r s to pro v i d e a call b a c k m e t h o d m a t c h i n g the Click H a ndler (in the ab o v e ex a m p l e) prot o t y p e . To rai s e the ev e n t defi n e d in the cla s s (this ca n be do n e at an y ti m e ac c o r d i n g to the cla s s s ne e d s), us e the foll o w i n g co d e (whic h is a be s t pr a c ti c e for firin g ev e n t s): C#
void OnClic k(ClickEve n tA r g s e) { // Make sure the r e are subsc r i b e r s registe r e d to the even t if(Click != null) Click(th is, e); //Raise the even t (call the me t h o d) }

L iste n e r s sh o u l d regi s t e r thei r inte r e s t in the sp e c i fi c ev e n t, pr o v i d i n g thei r call b a c k m e t h o d : C#


classIns t a n c eWit h Eve n tD e f i n e d.Clic k += new ClassWithEve n t D e f i n i t i o n.Clic k H a n d l e r(t h is.OnClic k);

Note: In this co d e sni p p e t , cla s s I n s t a n c e W i t h E v e n t D e f i n e d is an inst a n c e of the cla s s in w hi c h the ev e n t w a s defi n e d , an d On C l i c k is a call b a c k fun c ti o n that will be ex e c u t e d w h e n the ev e n t is rai s e d . Garbage Collection Ev e r y pro g r a m us e s res o u r c e s of on e sort or an o t h e r m e m o r y buff e r s , net w o r k co n n e c t i o n s , dat a b a s e res o u r c e s , an d so on. In fact, in an obj e c t- ori e n t e d en vi r o n m e n t , ev e r y typ e co u n t s as a res o u r c e av ai l a b l e for a pro g r a m s us e. To us e an y of the s e res o u r c e s , m e m o r y m u s t be allo c a t e d to re p r e s e n t the typ e. Th e ste p s req u i r e d to ac c e s s a res o u r c e are as foll o w s : .1 All o c a t e m e m o r y for the typ e that re p r e s e n t s the res o u r c e .

.2 Initi ali z e the m e m o r y to set the initi al stat e of the res o u r c e an d to m a k e the res o u r c e us a b l e . .3 U s e the res o u r c e by ac c e s s i n g the inst a n c e m e m b e r s of the typ e (rep e a t as ne c e s s a r y). Pa g e | 2 1

.4 Te a r do w n the stat e of the res o u r c e to cle a n up. .5 Fr e e the m e m o r y . Th e .N E T ru nti m e req u i r e s that all m e m o r y be allo c a t e d fro m the m a n a g e d he a p . Th e ap p li c a t i o n ne v e r fre e s obj e c t s fro m the m a n a g e d he a p the ru nti m e fre e s obj e c t s aut o m a t i c a l l y w h e n the y are no lon g e r ne e d e d by the ap p li c a t i o n . Th e .NE T gar b a g e coll e c t o r (GC) co m p l e t e l y ab s o l v e s the de v e l o p e r fro m tra c k i n g m e m o r y us a g e an d kn o w i n g w h e n to fre e m e m o r y . Th e gar b a g e coll e c t o r s opti m i z i n g en g i n e det e r m i n e s the be s t ti m e to recl ai m av a il a b l e m e m o r y (perf o r m a coll e c ti o n), ba s e d on the allo c a t i o n s the ap p li c a t i o n m a k e s . W h e n the gar b a g e coll e c t o r perf o r m s a coll e c ti o n , it ch e c k s for obj e c t s in the m a n a g e d he a p that ar e no lon g e r bei n g us e d by the ap p li c a t i o n an d perf o r m s the ne c e s s a r y op e r a t i o n s to recl a i m thei r m e m o r y . G a r b a g e coll e c t i o n (not ha vi n g to m a n a g e ap p li c a t i o n m e m o r y) will be ne w to O bj e c t i v e- C de v e l o p e r s (Jav a ha s a gar b a g e coll e c t i o n m e c h a n i s m si m il a r to .N E T s). Th e .N E T G C is a tra ci n g gar b a g e coll e c t o r, m e a n i n g that un u s e d obj e c t s are coll e c t e d wit h o u t ha vi n g to m ai n t a i n ref e r e n c i n g info r m a t i o n duri n g the ap p li c a t i o n s nor m a l op e r a t i o n , an d that un u s e d obj e c t s are fre e d w h e n the G C dict a t e s (on the Wi n d o w s P h o n e 7 G C, thi s oc c u r s on c e for ev e r y m e g a b y t e of all o c a t i o n s , ap p r o x i m a t e l y). Thi s m o d e l m a k e s no pr o m i s e on an obj e c t s coll e c t i o n ti mi n g . Th e r e f o r e , it is a les s- tha n- opti m a l sol u ti o n w h e n de al i n g wit h m e m o r y obj e c t s that ne e d to be rel e a s e d in a ti m e l y m a n n e r an d in a pre d i c t a b l e or d e r. Th e full .N E T m e m o r y all o c a t o r w o r k s by m a i n t a i n i n g a bl o c k of fre e sp a c e in w hi c h a sin g l e poi n t e r (next obj e c t poi n t e r) det e r m i n e s w h e r e to all o c a t e the ne x t obj e c t. Thi s m o d e l w o r k s w ell wit h co m p a c t i o n t h e pr o c e s s in w hi c h us e d m e m o r y blo c k s ar e join e d tog e t h e r to cre a t e a co m p a c t , defr a g m e n t e d fre e regi o n . H o w e v e r , on W i n d o w s P h o n e 7, the gar b a g e coll e c t o r do e s not ha v e the lux u r y of perf o r m i n g co m p a c t i o n . Th e r e f o r e , the m e m o r y allo c a t o r us e s a fre e list of av ai l a b l e m e m o r y blo c k s , si m il a r l y to cla s s i c a l m e m o r y allo c a t o r s . Furt h e r m o r e , on the full .N E T G C, the he u ri s t i c s for det e r m i n i n g w h e n a coll e c t i o n oc c u r s are fairl y so p h i s t i c a t e d , ac c o u n t i n g for obj e c t lifeti m e , coll e c ti o n fre q u e n c y , w o r k i n g set siz e, an d oth e r crit e ri a. Th e Wi n d o w s Ph o n e 7 G C, on the oth e r ha n d , ha s a ver y si m p l e trig g e r for perf o r m i n g a G C: w h e n e v e r a m e g a b y t e of m e m o r y ha s be e n all o c a t e d . W h e n a gar b a g e coll e c t i o n oc c u r s , the G C co n s t r u c t s a gr a p h of all ref e r e n c e d obj e c t s. To co n s t r u c t this gra p h , the G C start s fro m a set of ro ot s. R o o t s are ref e r e n c e s that eith e r ref e r to obj e c t s on the he a p or ar e set to null, an d incl u d e stati c fiel d s, loc al vari a b l e s in curr e n t l y acti v e m e t h o d s , an d G C ha n d l e s . Th e G C start s by tra v e r s i n g the set of acti v e roo t s, buil di n g a gra p h of all obj e c t s rea c h a b l e fro m the root s. Thi s is the "M ar k" ph a s e . At the en d of thi s pr o c e s s , the G C ha s a gr a p h of all obj e c t s that ar e so m e h o w ac c e s s i b l e fro m the ap pl i c a t i o n a l l obj e c t s that are not in the gra p h are co n s i d e r e d gar b a g e an d m a y be recl a i m e d . N e x t, the G C fre e s all m e m o r y obj e c t s for w hi c h no ro ot s w e r e dis c o v e r e d in the "M ar k" ph a s e . Thi s is the "S w e e p " ph a s e . At the ne x t sta g e , w hi c h oc c u r s oc c a s i o n a l l y , the G C co m p a c t s the m e m o r y obj e c t s wit hi n the allo c a t e d sp a c e to cre a t e a defr a g m e n t e d m e m o r y sp a c e . Thi s sta g e oc c u r s w h e n e v e r fra g m e n t a t i o n lev el ex c e e d s a defi n e d thr e s h o l d . D u ri n g this pro c e s s , the G C m o v e s obj e c t s in m e m o r y . Th e r e f o r e , it up d a t e s all ref e r e n c e s to the m . Pa g e | 22

As y n c h r o n o u s l y fro m the G C s sta n d a r d op e r a t i o n , a de di c a t e d finali z e r thr e a d ru n s in the ba c k g r o u n d an d ex e c u t e s fin ali z a t i o n co d e for obj e c t s that req u i r e it (a cla s s re q u e s t s finali z a t i o n by de cl a r i n g a fin ali z e r, ov e r r i d i n g the O bj e c t. F i n a l i z e pr ot e c t e d m e t h o d ; in C#, yo u de cl a r e a finali z e r by writi n g a m e t h o d that ha s the na m e of the cla s s pr efi x e d by a ~ sig n). N ot e that an obj e c t s fin ali z e r ru n s onl y aft e r the obj e c t ha s be c o m e unr e a c h a b l e by the ap pl i c a t i o n s co d e ; ref e r e n c e d obj e c t s ar e not en q u e u e d into the fin ali z e r thr e a d s qu e u e . W h e n the fin ali z e r thr e a d is do n e wit h an obj e c t, the last ref e r e n c e to it is re m o v e d , an d it ca n be coll e c t e d duri n g the ne x t G C cycl e. Ev e n tho u g h fin ali z a t i o n pro v i d e s an ea s y w a y to recl a i m un m a n a g e d res o u r c e s at so m e poi n t aft e r an obj e c t be c o m e s unr e a c h a b l e , yo u sh o u l d us u a l l y pre f e r the saf e r, fast e r, an d det e r m i n i s t i c disp o s e patt e r n (for m o r e infor m a t i o n ab o u t the Di s p o s e patt e r n , se e htt p://ms d n. m i c r o s o f t. c o m/ e n- us/libr a r y/fs2 x k f t w . a s p x ). Java Programmer Point of View Fr o m a Ja v a de v e l o p e r s poi n t of vie w , the C# lan g u a g e is ver y fa m i li a r. B ot h lan g u a g e s are obj e c t- ori e n t e d lan g u a g e s . Bot h are str o n g l y typ e d , an d bot h ha v e a gar b a g e coll e c t o r that m a n a g e s m e m o r y . C# diff e r s by its ability to defi n e sta c k- ba s e d obj e c t s usi n g the struct ke y w o r d , its ability to pa s s pri m i ti v e vari a b l e s by ref e r e n c e , its si m p l e ev e n t- ba s e d res p o n s e m e c h a n i s m ba s e d on del e g a t e s , an d the exi s t e n c e of pro p e r t i e s . To the Ja v a de v e l o p e r , so m e of the s e diff e r e n c e s ca n be circ u m v e n t e d . Ot h e r diffe r e n c e s ha v e no an al o g in Ja v a. Th e foll o w i n g parti al list rep r e s e n t s not a b l e diffe r e n c e s bet w e e n C# an d Ja v a : .6 .7 In Ja v a , typ e s are de cl a r e d in thei r o w n file s. Th e r e ca n be onl y on e typ e in a sin gl e Ja v a so u r c e file, an d the file na m e m u s t be the sa m e as the typ e na m e . C# na m e s p a c e s ar e m o s t l y eq u i v a l e n t to Ja v a pa c k a g e s . Ev e r y typ e is co n t a i n e d in a pa c k a g e , w hi c h is defi n e d at the top of the so u r c e file (wher e the typ e is de cl a r e d). Us u a l l y, the w h o l e ap p li c a t i o n defi n e s a root pa c k a g e su c h as "co m . m y c o m p a n y . a p p n a m e " , an d all typ e s exi st in thi s pa c k a g e or su b- pa c k a g e s (suc h as "co m . m y c o m p a n y . a p p n a m e . s u b p a c k a g e "). Th e pa c k a g e defi n i ti o n s m a y als o refl e c t the fol d e r hi er a r c h y in the pr oj e c t. H o w e v e r , ther e is no ali a si n g or ne s ti n g of pa c k a g e s . In Ja v a , inh e r i t a n c e is do n e usi n g the "ext e n d s " ke y w o r d an d inter f a c e im pl e m e n t a t i o n is do n e usi n g the "im pl e m e n t s " ke y w o r d (for ex a m p l e , "clas s A ext e n d s B im pl e m e n t s IC, ID") Th e r e is no "rea d o n l y" ke y w o r d (or eq ui v a l e n t) in Ja v a . All us e r- defi n e d typ e s are ref e r e n c e typ e s an d onl y pri m i ti v e typ e s ar e val u e typ e s . R ef e r e n c e typ e s are al w a y s pa s s e d by ref e r e n c e an d val u e typ e s ar e al w a y s pa s s e d by val u e (copi e d). Th e r e ' s no eq u i v a l e n t to the "ref" or "out" ke y w o r d in Ja v a. Th e r e is no del e g a t e co n c e p t in Ja v a . Th e onl y w a y to si m u l a t e this is usi n g refl e c t i o n . Ja v a s "sy n c h r o n i z e d " ke y w o r d en s u r e s thr e a d saf e t y bef o r e ent e ri n g a m e t h o d as do e s the [Met h o d I m p l( M e t h o d I m p l O p t i o n s . S y n c h r o n i z e d)] attri b u t e in C# . Pa g e | 23

.8

.9 .10

.1 1 .12

.13 .14 .15 .16 .17 .18 .19

In Ja v a , a pr o g r a m m e r ca n n o t im pl e m e n t tw o inte rf a c e s that de cl a r e the sa m e meth o d. Ja v a do e s n t all o w stati c co n s t r u c t o r s . Inst e a d , the r e' s a stati c blo c k. In Ja v a , ther e is no pro p e r t y co n s t r u c t p r o p e r t i e s ne e d to be defi n e d ex p li ci tl y (decl a ri n g set/get m e t h o d s in the co n t a i n i n g cla s s). In Ja v a , the "prot e c t e d " ac c e s s m o d i fi e r allo w s ac c e s s fro m an y oth e r typ e in the sa m e pa c k a g e , in ad d i ti o n to deri v e d typ e s . In Ja v a , all m e t h o d s are virt u a l by def a u l t, an d ther e are no "virtu a l" or "ov e r ri d e" ke y w o r d s . In Ja v a , a pr o g r a m m e r ca n n o t furt h e r rest ri c t ac c e s s to an ov e r ri d e (over the ac c e s s of the ov e r r i d d e n m e m b e r). Th e r e are no ind e x e r s in Ja v a.

Objective-C Programmer Point of View Ot h e r tha n the fact that bot h lan g u a g e s are obj e c t- ori e n t e d in nat u r e , O bj e c t i v e- C sy n t a x is ver y diff e r e n t fro m C#. Th e r e are a diff e r e n c e s in co di n g ap p r o a c h (like se n d i n g m e s s a g e s to obj e c t s in O bj e c t i v e- C rat h e r tha n calli n g fun c t i o n s in C#), m e m o r y m a n a g e m e n t (Obje c ti v e- C ha s no m e m o r y m a n a g e m e n t m e c h a n i s m ), the typ e sy st e m (.NE T s str o n g typ e sy st e m as op p o s e d to poi n t e r s to loo s e l y typ e d m e m o r y loc a ti o n s in O bj e c t i v e- C), etc. Th e foll o w i n g parti al list rep r e s e n t s not a b l e diffe r e n c e s bet w e e n C# an d O bj e c t i v e- C: .20 .21 O bj e c t i v e- C typ e s ar e defi n e d wit hi n tw o se p a r a t e files he a d e r an d im pl e m e n t a t i o n file s (.h an d .m, res p e c t i v e l y). In O bj e c t i v e- C, the r e ar e tw o typ e s of m e t h o d s in cla s s e s : inst a n c e an d cla s s m e t h o d s , de n o t e d by a pre p e n d e d + or - in the m e t h o d pro t o t y p e . Cl a s s m e t h o d s ar e just like static m e t h o d s in C#. In O bj e c t i v e- C, all m e t h o d s are pu bl i c, w hil e C# defi n e s 4 ac c e s s m o d i fi e r typ e s. N S O b j e c t in O bj e c t i v e- C is an eq u i v a l e n t of Sy s t e m . O b j e c t . In O bj e c t i v e- C, w h a t w e typi c a l l y thi n k of as co n s t r u c t o r s ar e init m e t h o d s . D e s t r u c t o r s/ Fi n a l i z e r s ar e de all o c m e t h o d s . Th e s e m e t h o d s are just a pro g r a m m i n g co n v e n t i o n an d are not sp e c i a l cla s s m e m b e r s . In O bj e c t i v e- C, pro p e r t i e s ar e de n o t e d wit h @ p r o p e r t y lin e s in the he a d e r file. C o r r e s p o n d i n g @ s y n t h e s i z e stat e m e n t s in the im pl e m e n t a t i o n file ge n e r a t e the pr o p e r t y co d e . Sp e c i a l dire c t i v e s in the @ p r o p e r t y de cl a r a t i o n det e r m i n e the sp e c i fi c s of the im pl e m e n t a t i o n (suc h as no n a t o m i c , ret ai n). O bj e c t i v e- Cs Protocols ar e eq ui v a l e n t to Interfaces in C#. Th e Self (in O bj e c t i v e- C) poi n t e r is the eq ui v a l e n t of this in C#. Pa g e | 24

.22 .23 .24

.25

.26 .27

.28 .29 .30 .31

#import is O bj e c t i v e- Cs ver s i o n of C#s using stat e m e n t s . Th e super ke y w o r d in O bj e c t i v e- C is ter m e d base in C#. In O bj e c t i v e- C, se n d i n g a m e s s a g e to an obj e c t is eq ui v a l e n t to calli n g m e t h o d in C#. O bj e c t i v e- Cs @selector() is eq u i v a l e n t to C# del e g a t e s .

Su m mary
C# is a si m p l e , m o d e r n , obj e c t- ori e n t e d , an d typ e- saf e pr o g r a m m i n g lan g u a g e deri v e d fro m C an d C+ +, that ai m s to co m b i n e hig h pr o d u c t i v i t y an d effi ci e n c y . C# intr o d u c e s cle a r sy n t a x , typ e saf e t y, an d aut o m a t i c m e m o r y m a n a g e m e n t . It en a b l e s a rel ati v e l y ea s y mi g r a t i o n pat h for Ja v a de v e l o p e r s an d gre a t l y si m p l i fi e s the de v e l o p m e n t eff o rt for O bj e c t i v e- C de v e l o p e r s w hil e pr e s e r v i n g the ke y co n c e p t s of obj e c t- ori e n t e d pro g r a m m i n g an d its pro g r a m m i n g po w e r .

Pa g e | 25

Das könnte Ihnen auch gefallen