Intro Toc Sharp Article 4 0

  • Upload
    funkdak

  • View
    228

  • Download
    0

Embed Size (px)

Citation preview

  • 8/2/2019 Intro Toc Sharp Article 4 0

    1/25

    Article

    Intro to C# (vs. Objective-C andJava)

    Doc u m e n t ver s i o n : 1.0.0

    La s t up d a t e d : 6/27/20 1 1

    P a g e | 1

  • 8/2/2019 Intro Toc Sharp Article 4 0

    2/25

    CONTENTS

    OVERVIEW.......................................................................................................................................... .......3

    OBJECTIVES .............................................................................................................................................3

    INTRODUCTION TO C#..............................................................................................................................3Hello 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

    P a g e | 2

  • 8/2/2019 Intro Toc Sharp Article 4 0

    3/25

    O v e r v i e w

    C# (pro n o u n c e d "C s h a r p") is a si m p l e, m o d e r n , o bj e c t- o ri e n t e d, a nd ty p e- s a f e pr o g r a mm i n g

    la ng u a g e . It wi l l i mm e d i a t e l y b e f a mi l i ar to C/C ++ a nd J av a pr og r a mm e r s . C# c om b i n e s th e hi gh

    pr od u c t iv i ty of R a pi d A pp l i c at i o n D ev e l o p m e n t (RA D) la ng u a g e s a nd th e ra w p ow e r of C ++ .

    T hi s ar t i cl e int ro d u c e s C#, a nd c om p a r e s it to ot he r m o d e r n o bj e ct- o ri e nt e d la ng u a g e s lik e

    O bj e c t i v e- C a nd J av a .

    Ob j e c t i v e s

    W h e n y ou v e fin i sh e d th e ar t i cl e, y ou wi ll h av e :

    A hi gh- l ev el u nd e r st a n d i n g of t he C# p ro g ra m m i n g la ng u a g e .

    A hi gh- l ev el u nd e r st a n d i n g of th e . NE T fra m e w o r k .

    A cl ea r vi ew of t he di ff er e n c e s a nd si mi l ar i t ie s in t he i mp le m e n t a t i o n of b as ic

    sc e na r i o s in C# ve rs u s J av a a nd O bj e c ti v e- C.

    .1

    .2

    I n t r o d u c t i o n t o C#

    C# i s a n obj ec t- o ri en te d la ng u a g e th at e na bl e s pr og r a m m e r s to bui l d q ui ck ly a wi de ran g e of

    ap pl ic a ti o n s for th e Mi cr o s of t .N ET fra m e w o r k . T he go al of C# a nd th e .NE T fra m e w o r k is t o

    s ho r t e n d ev e l o p m e n t ti m e b y fr ee i ng th e d e v el o p e r fr om w or r yi n g a bo u t s ev e r al lo w- l ev el

    pr og r a m m i n g ta sk s su ch as m e m o r y m an a g e m e n t , typ e sa f et y iss ue s, st an d a rd libr ar i e s, ar r ay

    bo un d s ch ec k i n g, an d so on. T hi s all ow s de ve l op e r s to s p en d the m aj o ri ty of d ev e l o p m e n t ti me

    w or k in g o n th ei r a pp li c at i o n s a nd b us in e s s lo gi c in st e a d.

    C# is a s im p l e, m o d e r n o bj e ct- o ri en t e d a nd ty pe- s af e pr o gr a mm i n g la ng u a g e . L et u s s e e w hy .

    P a g e | 3

  • 8/2/2019 Intro Toc Sharp Article 4 0

    4/25

    Simple

    B ei n g a p ar t of t he M ic ro s o f t .N E T pl at fo r m , C# a pp li c a ti o n s re ce i ve s up p o r t fr om th e

    ru nt i m e , an d the y us e ser vi c es pro v i d e d by the ru nt im e . T he .NE T fra m e w o r k is a

    m an a g e d e nv ir o n m e n t : th e r un ti m e m an a g e s m e m o ry all oc a t io n t h i s fre es th e

    de ve l op e r fro m th at tas k. Pr og r a m m e r s d o n ot ha ve to d e al wi t h the bu rd e n of m e m o r y

    m an a g e m e n t an d el i mi n at i ng m e m o r y lea ks .

    C# s up p o r ts o pe r at o r ov er l oa d i n g , si mi l ar l y to C + +; Ja v a a nd O bj e c ti v e- C d o n ot s up p o r t it.

    In he ri t an c e - rel at e d fe at u re s ar e cl ea r er a nd m or e pr ec i se th an in C + +:

    An abstract k ey w o r d is u s ed in st e a d of d ec la r i n g pu re vi rt u al fu nc t i o n s.

    A bs t r a c t cl as s e s ar e d ec l ar e d a s s uc h di re c tl y b y u si n g th e abstract k e y w o r d ,

    n ot i nd ir e c tl y b y la ck o f m e t h o d/ fu n c t i o n i m pl e m e n t a t i o n ("pu re vi rt u al

    f u nc t i o ns").

    A n a bs tr a ct cl as s m ay be co m p l e t el y i mp l e m e n t e d a nd ye t be de cl a r e d

    a bs tr a c t fo r a r ch i t ec t u r a l r ea s o n s .

    An override k ey w o r d is u se d fo r o v er r i d i ng a v ir t ua l m et h o d .

    N ew cl as s m e m b e r ty pe s, su ch as pr op e rt ie s an d ind ex e r s, ar e int r od u c e d to si mp l i fy

    c omm o n ta sk s s uc h a s l ist ite ra t i o n a nd pr op e r ty g et ti n g a nd s et ti n g.

    Less Error Prone

    C# i s le ss e rr o r- p ro n e th a n C ++ d ue t o th e f ol lo w i n g c ha r a c t e r i s ti c s :

    .1 Un l i k e C++, wh er e it is p os si bl e to d ec l ar e an y typ e of p oi n t er an d as s i gn it to a ny

    al l oc a t e d a dd r e s s or c as t it to a ny ot he r typ e, C# a ll o ws on ly ty pe- s af e c as ti n g. C as ti n g

    er ro rs ar e d et e ct e d at ei t he r c om p i l e ti m e or at ru n ti me . H o w e v e r, in ei th er si t ua t i o n ,

    y ou wi ll n ot m is s a ny er ro r s.

    .2 Th e us e of G en er ic typ e s al l o ws tem pl a t e cl a s se s to r em ai n typ e- s af e at c om p i le

    ti m e. T hi s al l o ws th e pr og r a mm e r to d e te c t er ro r s d ur in g c om p i l e, rat h er th an at r un

    t ime.

    .3 Ru nn i ng und er the ma n ag e d CL R, bou nd s ch e c ki ng is pe r f o r m e d for m an ag e d ar r a y s

    a ut o m a t i c a l l y w hi le a cc e s s i n g th e all oc a t e d ar ra y m e m b e r o bj e ct . T h is pr ev e n t s

    a cc id e n t a l m e m o r y c or ru p t i o n .

    Modern

    C# i s d es i g n e d to m ee t m od e r n ap pl ic a ti o n re qu ir e m e n t s , as w el l as int eg r at in g m od e r n

    s of t w a r e c on c e p t s :

    .4 C#, as a .NE T lan gu a g e, pro vi de s a lar g er se t of b as i c typ es , inc l ud i n g for e xa m p l e

    th e D ec i m a l ty pe u se d to h an d l e fi na n c i a l a pp li c a ti o n s co rr e ct l y.

    P a g e | 4

  • 8/2/2019 Intro Toc Sharp Article 4 0

    5/25

    .5 Kn ow n co nc e p t s , su ch as en um e r a t e d typ es, bi t f la g s, fir s t- cl a s s fun ct i on s an d

    ot h er s , b ec om e fi rs t- cl a ss ci ti z e n ty p es wi th a r ic h s et of fu n ct i o n a l i ti e s t ha t r ep r e s e n t

    ty pe- s p e c if i c u sa g e r eq u i re m e n t s .

    .6 It co nt ai n s a bui l t- in exc ep t i o n- ha nd li n g m ec h an i s m.

    .7 C# pr ov id e s a bu i l t- in ev en t ha nd l er for s im p le r h an dl i ng of inte gr a te d (for ex am p l e ,G U I a nd W e b) a p pl i ca t i o n re qu ir e m e n t s .

    .8 C# pro vi de s a co mp l e t e ref l ec t i on me ch a ni s m , al l o wi n g ob j e ct me t ad a t a to b e

    e x p l o r e d during run time .

    Object Oriented

    C# s u p p or t s th e thr e e pi l l ar s of O bj e c t- O r ie n t e d Pr o gr a m m i n g :

    Encapsulation

    A cl as s h as to ol s to hi de its i mp le m e n t a t i o n de ta i l s a nd b e re sp o n s i b l e for its o bj e ct s

    s ta te

    Inheritance

    T he is a rel at io n s h i p ca n b e d ef in e d b et w e e n cl as s e s, e na b li n g ex te n s i bi l i ty a nd

    r e u s a b i l i t y

    Polymorphism

    A p ro g r a m ca n h an d l e di f f er e n t ty pe s of o bj e ct s id en ti c a l l y, s ha ri n g th e s am e b as e,

    h av in g p ol y m o r p h i c b eh a v i or , b as e d o n s p ec i fi c o bj e ct ty pe s

    E ve r y t h i n g i n C# i s p ar t of a t yp e (for e x am p l e , cl a ss , i nt er f a c e , a nd st ru c t). T he r e a re n o

    gl o b a l (no n-memb e r ) f un c t i o n s o r v a ri a b l e s .

    E ve r y typ e in st a n c e is a n object . Al l ty pe s ar e d er iv e d fr om a c o mm o n b as e cl as s: object .

    W hi l e th es e c on c e p t s ar e v er y fa mi l i ar to J a va d ev e l o p e r s, th ey ar e fu nd a m e n t a l l y di ff er e n t (and

    in s om e c as e s e ve n c om p l e t e l y n e w) f or C /C ++/O bj e c t i v e- C d e v e l o p e r s .

    A few words about Java

    Ja va is a p ro gr a m m i n g la ng u a g e or i gi n al ly de ve lo p e d by Ja m e s G os li n g at S u n Mi cr os y s t e m s

    an d re l ea s e d in 1 9 95 as a c or e co m p o n e n t of S u n Mi cr o sy s t e m s Ja va pl at f or m . Th e la ng u a g e

    der i ve s m uc h of its sy nt a x fro m C an d C+ + b ut ha s a s i m pl e r o bj ec t m od e l an d fe we r lo w- lev el

    fa ci li ti e s. J av a a p pl i c a ti o n s t yp ic a l l y a re c om p il e d to b yt e c o d e (cla ss fil e) th at c an ru n o n a nyJ av a Vi rt u al M a ch i n e (JV M) r eg a rd l e s s of c om p u t e r ar ch it e c t ur e . J av a is g en e r a l- p u rp o s e ,

    c on c u r r e n t , cl a ss- b as e d , a nd o bj e c t- o ri e n t e d. It is d es i g n e d s pe c i fi c a l l y to h a v e a s f e w

    im pl e m e n t a t i o n de pe n d e n c i e s as po ss ib l e.

    A few words about Objective-C

    Th e O bj e ct iv e- C lan gu a g e is a si mp l e co m p u te r lan gu a g e de si g ne d to en ab l e so ph is t i c at e d

    o bj e ct- o ri e nt e d pr og r a mm i n g . O bj e c ti v e- C is d ef in e d a s a s m al l b ut p ow e r f u l s et of e xt e n si o n s to

    P a g e | 5

  • 8/2/2019 Intro Toc Sharp Article 4 0

    6/25

    th e st an d ar d A NS I C la ng u a g e. It is p os si b l e to c o m pi l e an y C pr og ra m wi t h an O bj e ct i v e- C

    c om p i l er , a nd to fre el y in cl u d e C co d e wi th in a n O bj e c ti v e- C cl as s. It s a d di t i o n s to C a re m o st ly

    in sp ir e d b y S m a ll t al k , on e of th e fir st o bj e ct- o ri e nt e d pr og r a mm i n g la ng u a g e s . Al l of th e sy nt a x

    fo r n o n- o b je c t- o ri e n t e d o p er a t i o n s (incl u di n g pr i mi t iv e v ar i ab l e s , p re p r o c e s s i n g , e xp r e s s i o n s ,

    fu n ct i o n d ec l a r at i o n s , a nd f un c ti o n c al ls) is i d en ti c a l t o C , w h il e t he s yn t a x fo r o b j ec t- o r ie n t e d

    fe at u re s is a n i mp le m e n t a t i o n of S m al l t a lk- s ty l e m e s s a gi n g . T od a y , O bj e c ti v e- C is u s ed

    pr i m ar i l y on A pp le 's M ac O S X a nd iO S: tw o en vi ro n m e n t s ba se d o n t he O pe n S t e p st an d ar d ,

    th ou g h n ot c om p l i a nt wi th it. Ob j ec t iv e- C is th e pr i m a r y la ng u a g e us e d fo r A p pl e 's C oc o a A PI ,

    an d it wa s ori gi na ll y the m ai n lan gu a g e on N eX T ' s Ne X TS T E P O S.

    Hello C#!

    Hi st o ri c a l l y, all ar t i cl e s a bo u t pr o gr a mm i n g la ng u a g e s st ar t wi th a H el l o W o rl d! s a m pl e . T hi s

    ar ti cl e wi ll f ol lo w t hi s tr ad it i o n h e r e is a C# v e rs i o n of t hi s p op u l a r p ro g r a m :

    C#using Sys tem;

    c lass HelloApp

    {

    s ta t i c void Main()

    {

    Console .Writ eL in e("Hello C#!");

    }

    }

    Th i s co de ref er e nc e s th e "Sy st e m" na m e s p a c e , w hi c h pr ov id e s th e "C on s ol e" cl as s, de cl a r e s a

    cl as s a nd e nc lo s e d "M ai n" st at ic m et h o d , w hi c h, in tur n, c al ls th e C on s o l e cl as s s " Wri te L i ne "

    st at ic m et h o d to o ut p ut a st r i n g.

    Note: R un n in g the ab ov e sa m p l e dir ec tl y un de r W in d o w s P ho n e 7 wi l l n o t pr od u c e a ny

    ta ng ib le ou tp u t, as th e W in d o w s P ho n e 7 c on s ol e is n ot ac ce s s i b l e. T he co de pr ov id e d is o nl y

    fo r c om p a r i s o n .

    Le t us se e th e sa m e pr og r a m wr i tt en in J av a:

    Java

    publ i c c la ss Hel l oWo r ld {

    pub l i c s ta t i c vo id ma in(S t r i ng[] args) {

    Sys tem.ou t .pr in t l n("Hel lo Java!");

    }

    P a g e | 6

  • 8/2/2019 Intro Toc Sharp Article 4 0

    7/25

    }

    A nd n ow in O b j ec t i v e- C:

    Objective-C

    #impor t

    in t ma in( in t a rgc, cons t cha r *argv[] ) {

    prin t f( "Hel lo Object ive - C!\n" );

    r et u r n 0;

    }

    T he fir st di f f er e n c e is i n co de or ga n i z at i o n: w hi le b ot h C# a n d J av a h av e a c la s s wi th o ne

    m et h o d , O bj e c ti v e- C h as ju st a m e t h o d.

    T he s ec o n d di ff er e n c e li es in t he si gn a t ur e of t he m et h o d : in C#, t he M ai n m et h o d m u st st ar t

    wi th c ap i ta l M, w hi l e i n J av a a nd O b j ec t i v e- C , i t is n o t a re q ui r e m e n t . T h e St ri n g[] ar g u m e n t

    fo r t he M ai n m e t h o d i n C# i s o p ti o n a l .

    T he t hi rd di ff er e n c e is i n t h e M ai n m e t h o d s r et u r n ty pe : i n C# (an d J av a), i t re tu r n s v oi d w hi l e

    in O b j ec t i v e- C it r et u r n s int. I n C#, t h e M a i n m e t h o d c ou l d al so r et u rn a n int v al u e .

    Th e m or e w e ex a mi n e th e co de th e m or e di f f er e nc e s w e wi l l d is co v e r.

    Defining a Class

    M uc h of ob j ec t- or i en te d pr og r a m m i n g co ns is t s of wr i t i ng co de for n ew ob j ec ts d e f i n i n g n ew

    cl a ss e s . T h e cl a ss d ef i ni t i o n s i n C# a n d J av a a re fa ir l y si m il a r:

    C# (and Java)

    c las s F rac t i on

    {

    p ub l i c i nt n ume r a t o r ;

    p ub l i c i nt d en om i n a t o r ;

    pub l i c void P r i n t()

    {

    //C#

    Console.Wr i teL i ne("{0}/{1}", nume ra to r, denomi na to r );

    //Java

    //Sys t em.ou t . p r i n t l n(new Pri n t f Fo rm a t("%d/%d").sp r i n t f(numer a t o r , denom ina t o r));

    }

    P a g e | 7

  • 8/2/2019 Intro Toc Sharp Article 4 0

    8/25

    }

    In O b j e c t i v e - C , c la s s e s a re d ef in e d i n tw o p ar t s:

    .1 An int e r f ac e tha t de c l ar e s the m et ho d s and ins t an c e var i a bl es of the cl a s s an d

    n am e s its s up e r cl a s s

    .2 An im pl e m e n t a t i o n tha t ac t ua ll y de f i ne s the cl a ss (con t ai n s the cod e tha t im pl e m e n t s

    it s m e t h o d s)

    T yp ic a l l y, th es e ar e s pl it b et w e e n tw o fil es , t ho u g h so m e t i m e s a cl as s d ef in it io n m a y s pa n

    s ev e r al fi l es b y us in g a fe at u re cal le d a cat eg o r y . T he sa m e cl as s/i nt er f ac e wr it te n in

    O b j e c t i v e- C:

    Objective-C

    #impor t

    @in te r f a c e F ra c t i o n : NSObj e ct {

    i nt n ume r a t o r ;

    i nt d en om i n a t o r ;

    }

    - (void) prin t;

    - (void) setNume ra to r: (int) n;

    - (void) setDenom in at or : (int) d;

    - (int) nume ra to r;

    - (int) denomi na to r;

    @end

    Objective-C

    #impor t "Frac t i on .h"

    #impor t

    @imp l eme n t a t i o n Fr a ct i o n

    - (void) pr in t {

    p r i n t f( "%i/% i" , nume r a t o r , denom i na t o r );

    }

    P a g e | 8

  • 8/2/2019 Intro Toc Sharp Article 4 0

    9/25

    - (void) setNume ra to r: (int) n {

    n um e ra t o r = n ;

    }

    - (void) setDenom in at or : (int) d {

    de nom i n a t o r = d ;

    }

    -(in t) denom i na t o r {

    r et u r n d en om i n a t o r ;

    }

    -(in t) nume ra t o r {

    r et u r n n um e r a t o r;

    }

    @end

    T he u sa g e of th e cl as s is a s fol l o ws :

    C#

    // Crea t e a new ins t ance

    F ra c t i o n f r ac t i o n = new F ra c t i o n();

    // Se t t he values

    fr ac t io n .n um e r a t o r = 5;

    f ra c t i o n. d e n om i n a t o r = 1 0;

    // P r in t i t

    f rac t ion.Pr in t();

    Java

    // Crea t e a new ins t ance

    f in a l F ra c t i o n f r a c t i o n = new F ra c t i o n();

    // Se t t he values

    fr ac t io n .n um e r a t o r = 5;

    f ra c t i o n. d e n om i n a t o r = 1 0;

    // P r in t i t

    f rac t ion.Pr in t();

    Objective-C

    P a g e | 9

  • 8/2/2019 Intro Toc Sharp Article 4 0

    10/25

    // Crea t e a new ins t ance

    F rac t i on *f rac = [[F rac t i on a l loc ] in i t ];

    // Se t t he values

    [f ra c se tN ume ra t o r : 1 ];

    [f ra c se tD enom i n a t o r : 3];

    // P r in t i t

    p r i n t f( "The f rac t i on i s: " );

    [ frac pr i n t ] ;

    prin t f( "\n" );

    // F ree memo r y

    [ f rac re lease];

    Fr ee in g m e m o r y is vi t al in O bj ec t i v e- C pr og ra m m i n g si nc e the m e m o r y is m an a g e d by the

    a pp li c a ti o n , w hi le in b ot h C# a nd J av a it m a n a g e d b y th e ru nt i m e (that wi l l b e di sc u s s e d lat er).

    T he w h ol e c on c e p t of m e t h o d c al ls (as it ex is t s in C# a nd J av a) i s di ff e re n t i n th e O bj e c t i v e- C

    w or ld w he n co m p a ri n g th e l an gu a g e sy nt a x.

    To ge t a n ob j ec t to do so m e th i n g, yo u se nd it a m e ss a g e tel l i ng it to a p pl y a m e th o d. In

    O bj e c ti v e- C, m e s s a g e st at e m e n t s ar e e nc lo s e d in b ra c k et s :

    Objective-C[ rece i ve r message]

    Th e rec ei v er is a n ob j ec t, a nd th e m es s a g e tel l s it wh at to do. In so ur c e co de , the m es s a g e is

    si mp l y the na m e of a m e th o d an d an y ar gu m e n t s th at are pa ss e d to i t. Wh e n a m es s a g e is se nt ,

    th e ru n- ti m e s ys t e m s el e ct s th e a pp r o p ri a t e m et h o d fro m th e re ce i ve r s re pe r to i re a nd in vo k e s it.

    .NET Runtime

    M u c h lik e J av a ap pl ic a t i o n s , C# a pp li c at i o n s w or k wi th in a m a n a g e d ru n- ti m e e nv ir o n m e n t

    c al l e d t he c om m o n la ng u a g e r un ti m e (CL R) , w hi c h pr ov i d e s a n u m b e r of se rv ic e s to th e a pp li c a ti o n .

    C om p i le r s an d to ol s ex po s e the co m m o n lan gu a g e ru nt i m e 's fun ct io n a l i t y an d en ab l e yo u to

    wr i t e co de tha t be ne f i ts fro m th is m an a g e d ex ec u t i o n en vi ro n m e n t . C od e de ve l op e d wi t h a

    lan gu a g e co m pi l er th at tar ge ts th e C LR is c al l ed m an a g e d co de . It b en e fi ts fro m fe at ur e s su ch

    a s cr os s- la ng u a g e int eg r a ti o n , cr os s- la ng u a g e e xc e p ti o n h an d li n g , e nh a n c e d s ec u ri t y,

    v er si o n i n g a nd de pl o y m e n t s up p o r t, a si m pl i f ie d m o d el for c o m p o n e n t int er a c ti o n , a nd

    d eb u g g i n g a nd pr of il in g s er vi c e s.

    To en ab le th e ru nt i m e to p ro vi d e se rv ic e s to m a n a g e d co de , la ng u a g e co m pi l er s m us t e mi t

    m et a d at a tha t d es c r ib e s th e typ es , m e m b e r s, an d ref er e n ce s in t he co m p i l ed co de . M et a d at a is

    P a g e | 1 0

  • 8/2/2019 Intro Toc Sharp Article 4 0

    11/25

    st or e d wi th th e c od e . E ve r y lo ad a b l e C L R p or ta b l e e xe c u t a b l e (PE) fil e c o nt a in s m et a d a t a . T he

    ru nt i m e us es m et a d at a to loc at e an d loa d cl as s es , lay ou t ins t an c e s in m e m o r y , res ol v e m et h o d

    in vo c a ti o n s , g en e r at e n at iv e c od e, e nf o rc e se c ur i t y, a nd s et ru n- ti m e c on t ex t b ou n d a r i e s.

    Th e ru nt im e au t o m a ti c a l l y ha nd l es ob j ec t lay ou t an d m an a g e s ref er e n c e s to ob j ec ts , re l ea s i n g

    th e m w he n th ey ar e n o lo ng e r b ei n g u se d (this is c al le d ga rb a g e c ol le c ti o n). O b je c t s m a n a g e d

    by th e ru nt i m e ar e cal l ed m an a g e d d at a. G ar b a g e col l ec ti o n el i mi n at e s m e m o r y lea ks as w el l as

    so me ot h er co m m o n pr og ra m m i n g er r o rs . Yo u can us e m an a g e d da t a, un m a na g e d da t a, or

    bo t h m an a g e d an d un m a na g e d da t a in y ou r .NE T Fra m e w o r k ap pl i ca t i o n .

    Th e C LR m ak e s it ea sy to d e si g n co m p o n e n t s a nd ap pl ic a t io n s, w ho s e ob j ec ts int er ac t ac r os s

    lan gu a g e s . O bj ec t s wr i tt en in d if f er e nt lan gu a g e s ca n co m m u n i c a t e wi t h ea ch ot he r, an d th ei r

    b eh a v i or s ca n b e int eg r a te d ti gh tl y.

    .NET and C# Program Basics

    A C# pr og r a m fil e s ho u l d e nd wi th th e .cs e xt e n si o n (in Ja v a th e fil e s h o ul d e nd wi th th e .javae xt e n si o n a nd in O b j e ct i v e- C wi th th e .m e xt e n s i o n). Al l t h e c od e i n C# i s w r it t en w it h in cl as s

    d ef in it io n s t h e r e ar e n o gl ob a l e nt it i e s in th e .N ET o bj e ct- o ri en t e d w or l d. Al l cl as s c od e is

    w ri tt e n wi th in th e cl as s de fi n i ti o n t he r e is n o s ep a r at i o n b et w e e n cl as s d ef in it io n a nd

    i m pl e m e n t a t i o n . A ll m e t h o d s a re w ri tt e n inl in e.

    .N ET cl as s es are de fi ne d wi t hi n na m e s p a c e s . Int er n al l y , th e na m e of t he n am e s p a c e in w hi c h a

    cl as s is d ef i n ed b ec o m e s pa rt of th e cl as s na m e. T hu s, di f f er e nt na m e s p a c e s ca n inc l ud e

    id en t i ca l cl a ss n am e s si n ce th e ful l n am e of t h e cl a ss is d if fe r e n t.

    Th e na m e s p a c e n am e us ua ll y de sc ri b e s the ge ne r a l fu nc ti o na l i t y to pi c, w hi le th e cl as s na m e

    d es c ri b e s th e cl as s s pe c if ic ro l e in th at g en e r a l ar e a.

    Na m e s p ac e m em b e r s are use d by pre f i xi ng the m em b e r na m e wi t h the na m es p a c e na m e

    fol lo w e d b y th e d ot (.) o p er a t o r . F or e xam p l e , to u se th e Console cl as s, w hi c h is d e fi n e d in th e

    System na m e s p a c e , w e ca n wr i te : System.Console .W r i t e L i ne().

    A m or e ab br e v i a te d w ay is to b ri ng th e req ui r ed na m e s p a c e into the pr og r a m s n am e s p a c e ,

    u si n g t he using k e y w o r d :

    C#

    using Sys tem;

    W it h t hi s st at e m e n t , System na m es p a c e m em b e r s ca n be di re ct ly us ed, wi t h no na m es p a c ere la t e d p re fi x a dd i ti o n s : Console .W r i t e L i ne().

    W e ca n de f i ne our o wn n am e s p a c e s to s er ve as an org a ni z a t i o n a l pa ck a g e for a f ew rel at e d

    typ es . Yo u ca n dec l ar e yo ur o wn na m es p a c e usi ng the namespace k e y w o r d :

    C#

    namespac e G raph i c s

    P a g e | 1 1

  • 8/2/2019 Intro Toc Sharp Article 4 0

    12/25

    {

    class Rectang le{}

    class Circl e{}

    }

    .NET Common Type System

    Th e .N ET fr am e w o r k d ef in e s a t yp e sy st e m, co m m o n to a ll l an gu a g e s th at tar ge t the .N ET

    en vi ro n m e n t , kn o wn as the C om m o n Ty pe Sy st e m (CT S). Th e C TS d ef i ne s a r ich se t of ty pe s

    th at c an b e h os te d b y t he C L R (for e xa m p l e , st r uc t s, cl as s e s, e nu m e r a t o r s , int er f ac e s , a nd

    d el e g at e s) a nd th ei r b as ic d ef in it io n rul e s. T he C T S al so pr ov i d es a r ic h s et of b as ic ty pe s,

    w hi ch wi l l be pr es e n te d so on . B as e d on thi s ext en s iv e typ e sys te m , the .N ET Fr am e w o r k

    de f i ne s a s et of ru l es th at ar e ter m e d the C om m o n L an g u a g e S pe c i f i c at io n (CLS). Th e C LS is

    th e mi ni m u m sp ec if ic a t io n tha t an y .N ET co m p l i an t la ng u a g e (a lan gu a g e i mp le m e n t e d o n to p

    of th e C L R) m u st i mp le m e n t .

    T he C T S pr ov i de s a ri ch s et of p re- d ef i n e d b as ic ty pe s for all . NE T la ng u a g e s to u s e. T hi s s et

    c on t ai n s pr i m it iv e ty pe s , s uc h a s System.Int32 an d System.Double , a s w el l a s ot h er

    co m m o n l y use d typ es , suc h as System.Decimal an d System.String .

    T he b as e of a ll . N ET ty pe s, in cl u di n g th e .N ET pri m it iv e ty pe s , is System.Object . By d ef i n i n g

    a c o m m o n ba se , .N ET all ow s tre at in g all ty pe s in c o m m o n ter m s, su ch as de fi ni n g col l ec t i o n

    typ es to sto re an y typ e of el em e n t s, de f i ni n g a g en e ri c m et h o d tha t ca n ac ce p t a ny ar gu m e n t

    ty pe , a nd s o o n. System.Object co nt a i n s a s et of m e m b e r s tha t ar e ap pl ic a bl e to a n y .N ET

    typ e. So m e of t he se m e m b e r s ar e vir t ua l. Th er e fo r e, the y ca n be ov er r i d de n by de ri vi n g ty pe s to

    b et te r re pr e s e n t th ei r c ha r a ct e r i st i cs . In t h e .N ET e nv ir o n m e n t , ty pe s ar e di vi d e d int o t wo

    di ff e r e n t c at e g o r i e s :

    .3 Re f er en c e Ty p es R e f e r e n c e typ e obj ec t s ar e al l o c at ed fro m the he ap . A ref e r en c e

    ty pe v ar ia b l e c on ta i n s o nl y a r ef er e n c e to t he a ct u al d at a, w hi c h re si d e s o n t he h ea p .

    .4 A c la ss inst an c e is a n e xa m p l e of a r ef er e nc e typ e. C on s i d e r th e fol l ow i ng cl as s:

    C#

    c las s Emp loyee

    {

    pub l i c Emp loyee(st r i ng name)//

    p ri v a te s tr i n g n ame ;

    }

    .5 Th e ins t an c e of s uc h a c la s s co ul d be cre at ed as fol l ow s:

    C#

    P a g e | 1 2

  • 8/2/2019 Intro Toc Sharp Article 4 0

    13/25

    Employee e = new Employee(John Doe);

    Note: T hi s st at e m e n t al l oc a t e s sp a c e for a n Employee o bj e ct o n th e h ea p ,

    a ss ig n s it to t he re qu ir e d da ta , a nd ret ur n s a r ef er e n c e to th i s o bj e ct , to b e

    st or e d in t he ref er e n c e v ar i a b l e e.

    .6 Va l ue Ty pe s Va l ue- t yp ed obj ec ts are al l oc at e d inl in e, ei t h er on the st a ck or a s pa r t

    of a c o nt a i n i n g o bj e c t.

    .7 Pr im i ti v e ty pe va ri a bl e s , s uc h a s int, ar e a n e x a m p l e of a v al u e ty pe :

    C#

    i n t i = 32;

    Note: T hi s st at e m e n t al l oc a t e s 32- bi ts of s p a ce o n th e st ac k a nd pl ac e s a 3 2-

    bi t v a lu e in to t hi s s pa c e .

    Note: I n C#, t h e o bj e c t s l oc a ti o n (sta ck v s. h e ap) i s n o t d et e r m i n e d b y t he o bj e c t s

    cr e at i o n m e t h o d (as si g n m e n t o pe r a t o r v s. n e w). T h e v ar i ab l e ty p e (val ue- t yp e v s.

    ref er e n c e- ty pe) d et er m i n e s w he r e th e v ar ia b le wi l l b e cr ea t e d, e xc e p t for b ox e d v al u e

    t ypes 1 .

    Fr o m th e .N ET fr am e w o r k cl as s hi er a rc h y s p er s p e c ti v e , ref er e n c e ty pe s di re ct l y d er iv e fr om

    System.Object ; v al u e ty pe s, h ow e v e r , d er iv e fr om System.ValueType , wh i c h d er iv e s

    f rom System.Object. The ValueType cl as s a dj u st s th e O bj e c t cl as s i mp le m e n t a t i o n

    (ov er ri d e s s om e o f i ts m e t h o d s) to b et t er s ui t a v a l ue- ty p e v ar i ab l e s b eh a v i o r .

    A s p ar t of t he s af et y co n c e pt pr ev a l e n t in . NE T, un in it ia li z e d v ar ia b l e u sa g e is i m po s s i b l e:

    v ar ia b le s ar e ei t he r a ut o m a t i c a l l y ini ti al iz e d wi th d ef a ul t v al u e s (suc h a s wi th cl as s m e m b e r s

    a nd ar ra y el e m e n t s), o r t he c om p i l er pr ev e n t s th ei r us a g e (suc h a s lo ca l m et h o d v ar ia b l e s).

    W h e n a ut o m a t i c a l l y a ss ig n e d to t he ir d ef a ul t s, ref er e n c e ty pe v ar ia b l es d ef a ul t to null , num e r i c

    v al u e ty pe v ar ia b le s d ef a ul t to z er o, a nd B oo l e a n v ar i a b l e s de fa u l t to false . T hi s p ro c e s s al s o

    d oe s a r ec u r si v e ini ti al i za t i o n of struct an d class m em b e r s .

    1B ox in g is t he pr oc e s s of c o nv e r ti n g a v al u e t yp e t o t h e t yp e object or to a n y int er f ac e ty pe i mp le m e n t e d b y

    th i s v a lu e ty pe . W h e n th e C L R b ox e s a v al ue ty pe , it wr a ps th e v al ue in si de a S ys t e m. O b j e c t a nd st or e s it on

    th e m a n a g e d h ea p . U n b o x i n g e xt r ac t s th e v a lu e ty p e fr o m t he o bj e ct . B o x i n g is i mp l i ci t; u nb o x i n g is e xp li c it .

    T he c on c e p t of b o xi n g a nd u nb o xi n g u nd e rl ie s th e C# u ni fi ed vi ew of t he ty pe s ys te m , in w h ic h a v al u e of a n y

    ty pe ca n b e tr ea te d a s a n o bj ec t.

    P a g e | 1 3

    http://msdn.microsoft.com/en-us/library/s1ax56ch.aspxhttp://msdn.microsoft.com/en-us/library/s1ax56ch.aspxhttp://msdn.microsoft.com/en-us/library/s1ax56ch.aspx
  • 8/2/2019 Intro Toc Sharp Article 4 0

    14/25

    C# Classes

    C# cl as s e s s up p o r t in f or m a t i o n hi di n g b y e n ca p s u l a t i n g fu nc t i o n a l i t y in p ro p e rt i e s a nd m et h o d s

    an d by en ab l i n g se ve ra l ty pe s of p ol y m or p h i s m , inc l ud i ng su bt y pi n g po l ym o r p h i s m vi a

    inh er it a nc e an d par a m e t r i c po l y m or p h i s m vi a g e ne r i c s. Y ou d ef i ne cl as s es by usi ng th e

    k ey w o r d "cl as s" fo l l o w e d b y a n id en ti fi e r t h e n am e of th e cl as s. N ex t, yo u c an cr ea t e in st a n c e s

    of t he cl as s by u si n g th e "ne w" k ey w o r d fol l o w e d b y t he n am e of t he cl as s. A s pr ev i ou s l y

    di s cu s s e d , cl as s e s a re re fe r e n c e - ty p e v ar i ab l e s .

    B ef o r e w e ge t d ee p e r int o th e C# cl as s e s, let u s int r o d uc e C# a cc e s s m o di f i e r s. T he r e ar e fo ur

    m ai n a cc e s s m o di f i e r s:

    Accessibility Meaning

    Public Access is not restricted

    Protected Access is limited to the containing class or types derived

    from the containing class

    Internal Access is limited to the current assembly (project)

    Private Access is limited to the containing type

    I n a d d i ti o n , t he protected internal ac ce s s m od i f ie r co m b i n a t i o n ca n be us ed . T hi s m ea n s

    th at a cc e s s is li mi te d to t he c ur re n t a ss e m b l y or ty pe s d er iv e d fro m th e c on t ai n i n g cl as s.

    Note: Y ou ca n als o ap pl y th e pr ev io u s ac ce s s m od if ie r s to cl as s m e m b e r s.

    A cl as s m ay co nt a i n a r ic h se t of m e m b e r typ es su ch as fiel ds , co ns t an t s, m et h o d s,

    c on s t r u c t o r s , d es t r uc t o r , p ro p e r t i e s, in de x e r s , o pe r a t o r s , a nd e ve n t s .

    Yo u ca n de fi ne a c la ss as st at i c by usi ng th e static k ey w o r d i n th e cl as s d ef in i ti o n . It is n o t

    p os s i b l e to c re at e in st a n c e s of a st at ic cl as s s t a t i c cl as s e s ar e lo ad e d au to m a t i c a l l y b y t he.N ET ru nt im e w he n th e p ro g r a m or n a m es p a c e co nt a i n in g the cla ss is loa de d an d us ed . T he

    o nl y c on s tr u c t o r p er m i tt e d in a st at ic cl as s is t he st at ic c on s tr u c t o r. A st at ic c on s tr u c t o r wi l l b e

    c al le d o nc e at m o s t, a nd wi ll b e c al le d b ef or e th e fi rs t t im e a s ta ti c m e m b e r of t he ty pe is

    a cc e s s e d . A n i ns ta n c e c on s t r u c t o r (if o ne e xi s ts) w il l a l wa y s r un af te r a s ta ti c c on s t r u c t o r.

    C# Class Members

    Fi el d s (kn ow n i n C/C++ a s d a t a m em b e r s ) a re i ni ti al i ze d a ut o m a t i c a l l y w it h a d ef a u l t v al u e

    a cc o r d i n g to th ei r ty p e c at e g o r y (ref er e n c e - ty p e v s. v al u e- ty p e). B y d ef a u l t, a f iel d is d e fi n e d t o

    b e a n in st a n c e fi el d a fie l d th at is c on t ai n e d in e ve r y in st a n c e of t he cl as s, h ol d i n g a nin st a n c e s s pe c if ic v al u e. H o w e v e r, a f iel d c an b e m a d e st at ic, in w h ic h ca s e its v al u e is s h ar e d

    b y all cl as s in st a n c e s. Y ou d ef in e st at ic fi el ds u si n g th e static k ey w o r d . P u bl i c st at i c fi el d s c an

    on l y b e a cc e ss e d thr ou g h th e c la ss na m e usi ng th e d ot (.) oper a t o r .

    An ot h e r cl as s m e m b e r, th e m et h o d, is a fu nc ti o na l m e m b e r of th e cl as s. In C#, a ll m et h o d s ar e

    de f i ne d ins i de th e cl as s de cl a r a ti o n. Cl as s m et h o d s ca n ac ce s s ot he r cla ss m e m b e r s:

    P a g e | 1 4

  • 8/2/2019 Intro Toc Sharp Article 4 0

    15/25

    C#

    publ i c v oi d r a is eSa la r y(i n t per c en t age)

    {

    // Upda t e t he s al a r y f ie l d

    s al a r y += sa la r y * pe rc en t age/ 100;

    }

    Th e ob j ec t on w hi c h the m et h o d w as ac t i va te d is a va il a bl e ins i de the m et h o d usi ng th e ke yw o r d

    this . O b se r v e th e fol l o wi n g s am p l e w h er e cl as s fi el d s ar e u pd a t e d wi th th e v al u e of i de nt ic a l l y-

    na m e d pa ra m e t e r s:

    C#

    publ i c Emp l o y ee(s t r i n g name, i n t s al a r y)

    {

    t hi s. n ame = n ame ;

    t h i s. sa l a r y = s al a r y;

    }

    M et h o ds tha t m an ip u l a t e st at i c fiel ds on l y sh ou ld be de fi ne d as st at i c m et h o d s. T h ey ca n be

    a ct i v at e d u si n g th e cl as s n am e al on e a nd h av e n o a c ce s s to a n y n on- s ta ti c fi el d in t he cl as s.

    By de f au l t , m et h o d pa ra m e t e r s in C # ar e pa ss e d by val ue .

    .8 W he n the pa r am e te r is of a v al ue- typ e, a loc a l cop y of t he arg um e n t s va l ue iscr ea te d for th e m et h o d. W or k i n g on thi s loc al ind ep e n d e n t val ue s co py , the m et h o d ha s

    n o infl u en c e on th e ori gi n al ar gu m e n t s v al u e.

    .9 W he n the pa r a me t e r is of a ref e re nc e- typ e, a loc al cop y of t he arg um e n t s ref e re n ce

    is cr ea t e d for th e m et h o d . T hr o u g h thi s ref er e n c e co p y, th e m et h o d c an al te r th e ori gi n a l

    ar gu m e n t s v al u e, th ou g h it ca n n ot c ha n g e th e ref er e n c e its el f.

    So m e t i m e s w e w ou ld lik e t he m et h o d to w o rk di r ec tl y o n t he ar gu m e n t pa ss e d , be it a va l ue or

    ref er e n c e typ e, inst ea d of w or ki n g on an in de p e n d e n t loc al co py . T o ge t su ch be ha v io r, C#

    pr ov id e s us wi t h a s pe ci a l ki nd of p ar a m e t e r r e f e r e n c e pa ra m e t e r s.

    Note: T hi s is t he sa m e as p as si n g th e ad dr e s s of the var i ab l e in C , o r a ref er e n c e in C + +.

    Yo u d ef i ne ref er e nc e pa ra m e t e r s by a dd in g th e refke yw o r d be fo re th e pa ra m e t e r typ e in t he

    m et h o d s si gn a t u re , a nd a ga in in t he m et h o d c al l. T h e refk ey w o r d m u st b e s p ec i fi e d in b ot h.

    C#

    // Func t i o n def i n i t i o n

    pub l i c void Swap(re f i n t i 1, re f i n t i2)

    P a g e | 1 5

  • 8/2/2019 Intro Toc Sharp Article 4 0

    16/25

    {

    in t t mp = i1;

    i 1 = i 2;

    i 2 = tmp ;

    }

    // Func t i o n c al l

    i n t i 1 = 5, i2 = 10;

    ...

    Swap(ref i 1, ref i2);

    R ef e re n c e p ar a m e t e r s s ho u l d be ini t i al iz e d b ef o re th ey ar e p as s e d to t he fu nc ti o n . In s o m e

    c as e s, thi s is i nc o n v e ni e n t , es p e ci a l ly w he n th ei r v al u e is c o m p u t e d b y t he fu nc t i o n. F or s uc h

    ca se s, C# o ff er s an ot h e r ki nd of pa ra m e t e r: o ut pu t par a m e t e r s . O ut p ut pa ra m e t e r s al l ow us to

    pa ss no n- i ni t i al i ze d var i ab le s to a m e th o d. T he m et h o d ca n the n de f i ne the m usi ng th e out

    ke yw o r d. O ut p ar a m e t e r s m us t b e as si g ne d ins i de th e m et h o d. Fa i l i ng to as si g n o ut par a m e t e r s

    g en e r a te s a c o m pi l e r err or . A s wi th th e refk ey w o r d , b ot h th e fu nc t i o n d ef in it io n a nd c al l m u s t

    in cl u d e t he out ke yw o r d to us e ou t pu t p ar a m e t er s .

    C#

    // Func t i o n def i n i t i o n

    pub l i c void ge tS i z e(out i n t w id t h , out i n t he igh t)

    {

    w id t h = t h is .w i d t h ;

    hei g h t = t h i s. he ig h t ;

    }

    //Func t i o n c al l

    i n t w id t h , hei g h t ;

    ...

    getSize(out width, out height);

    T o e na b l e u se r s to a c ce s s cl as s fi el d s in a n el eg a n t y et c on tr o ll e d m a n n e r, C# p re s e n ts

    pr op e r ti e s . P ro p e r t i e s c an b e c o ns i d er e d a s smart fields . T he y e na b l e d ef in in g s pe c ia l g et

    a nd se t m e t h o d s , ter m e d getter an d setter , w hi c h ar e a ct iv a t e d u si n g si m pl e fiel d a cc e s s

    sy nt a x. T hi s w ay , w e c an pr ov i d e th e u se r s wi th a n int ui ti v e fi el d a cc e s s s er vi c e, y et st i l l p er fo r m

    v al id a t i o n (an d a n y ot h er r eq u i re d ) o pe r a t i o n s . Y o u d ef i ne p ro p e r t i e s a s f ol l o w s :

    C#

    //Fi el d t o s t or e t he p ro p e r t y va lu e

    p ri v a te i nt d ay ;

    P a g e | 1 6

  • 8/2/2019 Intro Toc Sharp Article 4 0

    17/25

    // Prope r t y

    p ub l i c i nt Da y

    {

    // Get ter

    ge t

    {

    // A ny f un c t i o n a l i t y c ou l d b e exe cu t e d h er e i n o r de r t o a l te r

    // t h e r et u r n va lu e

    r et u r n d ay;

    }

    // Set ter

    se t

    {

    // A ny f un c t i o n a l i t y co u ld b e e xe cu t e d h er e i n o r de r t o v al i da t e

    // a va l ue bef o r e i t be i ng ass ig ned t o t he p r i va t e f ie l d

    // No t e: new p r ope r t y v al u e i s alway s p r o v i d ed by t he k eywo r d value

    if (isDayOK (value))

    day = value ;

    }

    }

    B y d ef a ul t, pr op e r ty g et te r a nd s et te r a cc e s s is p r ov i de d by th e pr o pe r t y d ef in it io n (in th e

    pr ev i ou s e xa m p l e th ey b ot h ar e p ub li c). Y ou c an ex pl ic it ly c ha n g e a n a cc e s s o r s vi si bi li ty to

    di ff er fr om th at of t he pr op e r ty , th ou g h th e a cc e s s i b i l i ty m o di f i e r of t he a cc e s s o r m u st b e m o r e

    re st ri c t iv e th an th at of th e pr op e r t y. O nl y o ne of th e a cc e s s o r s m a y h av e its vi si bi li ty al t er e d.

    C#

    publ i c Fi el dT ype P rope r t yN ame

    {

    ge t

    {

    r et u r n f ie l d;

    }

    p ro t e c te d se t

    {

    P a g e | 1 7

  • 8/2/2019 Intro Toc Sharp Article 4 0

    18/25

    f ie l d = value ;

    }

    }

    C# Structs

    W hi l e a c la ss is a v e ry g en e r al ty pe , th e .N ET C T S d ef in e s a dd it io n a l, m or e s pe c if ic , ty pe s th at

    m ay b et te r sui t sp ec if i c ap pl ic at i on re qu ir e m e n t s . T ho u g h w e w ou ld us ua ll y us e cla ss e s to

    d ef in e o ur ty pe s, cl as s e s m ig h t s om e t i m e s b e to o he av y f or u s. Cl as s o bj e ct s ar e ref er e n c e

    ty pe v ar ia b l e s a nd th ei r m a ni p u l a t i o n in vo l ve s a l ot o f o v er h e a d . W e m a y re qu i re a lig ht w e i g h t

    ty pe , for w hi c h w e w ou l d lik e m o re ef f i ci e nt o bj e ct m a ni p u l a t i o n .

    Fo r su ch ca se s, C# en ab l es th e pr og ra m m e r to de cl a r e da ta st r uc tu r es , usi ng the ke yw o r d-

    "st ruct".

    St r u ct u r e s ar e v al u e- ty pe us er d ef i n e d ty pe s a nd a s s u ch ar e d er iv e d i mp li ci tl y fr om

    Sy st e m . V al u e T y p e . Th e y ar e al l oc a t e d an d m an ip u l a t e d di r ec tl y o n th e st ac k. Yo u d ef i ne

    str u ct u r e s u si n g th e struct k ey w o r d . St ru c t ur e s , li ke cl as s e s, c an h av e fie l ds a nd m et h o d s (as

    w el l a s p ro p e r t i e s a nd ev e nt s , w hi c h w e h av e n ot c ov e r e d y et). Str u ct u r e s c an d ef in e

    c on s tr u c t o r s, a nd c an o ve rr id e vi rt u al m et h o d s d er iv e d fr om System.ValueType . H ow e v e r ,

    th ey h av e s om e li mi ta ti o n s:

    .10 St r uc tu r e s ca nn o t be pa r t of a n i nh er i ta n c e tre e t h e y ca nn o t inh er i t fro m oth er

    cl a ss e s (exc e pt f or th ei r d ef a u l t b as e a s v a lu e ty p es : System.ValueType ). They a re

    se al e d, w hi c h m e an s th ey ca nn o t ser ve as ba se cl as s es for ot he r cl as s es .

    .1 1 Th ou g h usi n g st r u c t ur e s sav es the pro gr a m fro m hav i ng to d o h ea p al l o c at i o n and

    re du c e s G C c os ts (as v al u e ty pe s d o n ot h av e to b e co ll ec t e d), c ar e l e s s u se of t hi s d at a

    ty pe m a y in cu r si gn if ic a n t c op y in g c os ts , a s v al u e ty pe s ar e c op ie d w he n e v e r

    tra ns f er r ed a s a m e th o d ar gu m e n t , to an ot h er var i ab l e, a nd so on.

    .12 As val ue- typ e ob j ec ts , st r u ct ur e ob j e ct s can no t hav e a null v al u e u nl e ss th ey ar e

    wr a p p e d a s a N ul la b l e< T > ty pe s .

    .13 Yo u ca nn o t de f i ne you r ow n pa r am e t e r l e s s co ns tr u c t or for a s tr uc tu re , as it al r e ad y

    h as a d e f au l t (no n- o ve r ri d e a b l e ) p ar a m e t e r l e s s c on s t r u c t o r t ha t i ni ti al i ze s al l fi el d s a nd

    t he i r d ef a u l t v al u e s .

    .14 Yo u ca n no t de f i ne a des t r uc to r for a st r u ct ur e.

    B ei n g m or e li mi te d a nd a ct iv at e d di f fe re n t ly fr om re gu l ar cl as s e s, st r uc t ur e s s ho u l d b e u s ed for

    lig ht w e i g h t, v al u e- ty pe o bj e ct s . P os s i bl e e xa m p l e s ar e P oi nt , th e C o m p l e x n u m b e r ty pe , th e

    Sy st e m s de ci m a l an d D at e Ti m e typ es , an d so on. S uc h ty pe s ar e ac t ua l l y a s im p l e co m bi n a t i o n

    of v a lu e- ty pe s , a nd u se r s e xp e c t t he m t o b eh a v e a s p ri m i ti v e v al u e- t yp e s (tha t i s, z er o i ns te a d

    o f n u ll , s e a l e d).

    P a g e | 1 8

  • 8/2/2019 Intro Toc Sharp Article 4 0

    19/25

    C# Delegates & Events

    D ur in g th e co ur s e of de ve l op m e n t , w e fre qu e n t l y cal l fun ct io n s th at ex pe c t a function a s o ne

    of th ei r ar gu m e n t s . T he s e fu nc t i o n s wi ll c al l b a ck o ur pr ov i de d fu nc t i o n d ur in g th ei r ex e c ut i o n.

    T he y ar e u su a ll y ter m e d callback functions . T h e tr ad it io n a l w a y to p as s f un c ti o n s a s

    par a m e t e r s w as by se nd i ng a f un ct io n poi nt e r tha t is m e re ly an ad dr e s s wi t h no typ e inf or m a t i o nat t ac h e d (suc h a s t he n um b e r a nd ty pe of p ar a m e t e r s , th e ret ur n v al u e, a nd s o o n). T he r ef o r e ,

    p as s i n g fu nc t i o n p oi nt e r s is n ot ty pe s af e.

    In t he .N ET fr am e w o r k , c al lb a c k m et h o d s ar e u se d for s ev e r al p ur p o s e s : e ve n t s, thr e ad i n g , ite m

    fi l t er i ng an d m or e. T o en ha n c e cal l ba c k m et h o d su pp o r t , the .N ET fra m e w o r k co m e s wi t h a

    n ew , typ e- s af e m e ch a n i s m ter m e d delegate . Delegate o bj e c t s r ef e r i nt er n a l l y t o a m e t h o d

    c al lb a c k. H o w e v e r , c om p a r e d to fu nc t i o n p oi n te r s, th ey pr ov i de ri ch e r fu nc t i o n a li t y in a m or e

    e nc a p s u l a t e d a nd ty pe- s af e m a n n e r.

    In C#, w h en de f i ni n g a m e th o d th at ac ce p t s a c al l ba c k fun ct io n as a p ar a m e t e r , w e sh ou ld do

    t he f ol lo w i n g :

    .15 De fi ne a d el eg a t e typ e tha t ide nt i fi es the req ui re d cal l ba c k fun ct i o n pro t ot yp e .

    .16 De f i ne ou r me t ho d as tak i n g a pa r a m e t e r of t he del eg a t e typ e de f i n ed .

    U se r s of o u r m et h o d wi l l:

    . 17 Cr ea te a n o bj ec t of t he sp ec if ie d de l eg a t e ty pe , p ro vi d i n g it wi t h the i r c al l ba c k

    fu nc ti o n a s a p a ra m e t e r . T hi s wi l l w o rk o nl y if th e c al lb a c k fu nc t i o n fol l o ws th e d el e g at e s

    re q ui r e d pr ot o t y p e .

    .18 Su pp ly the ne wl y cr e at ed de l eg a t e obj ec t, wh i ch ac t ua ll y wr ap s the i r c al l ba c k

    fu nc ti o n , a s a p ar a m e t e r to th e m et h o d .

    C# al l ow s y ou to d e fi n e a s pe c ia l c al lb a c k w ra p p e r ty pe , u si n g th e delegate key w or d . As a

    w ra p p e r of a c al lb a c k, th e ty pe di ct a t e s a s p ec i f ic si gn a t ur e , for e xa m p l e :

    C#

    pub l i c de lega t e void CarOpera t i on (Car c);

    To us e a s pe ci f i e d d el eg a t e, yo u cr ea te a n o bj ec t of th at de l eg a t e typ e an d p as s to it th e

    re q ui r e d c al lb a c k tar ge t (a m et h o d). T hi s ca ll b ac k m et h o d s ho u l d m at c h th e d el e g at e s d ef in e d

    s i g n a t u r e .

    Us in g the del eg a t e de fi ne d ab ov e as a n e xa m p l e , w e wi l l sh o w so m e fun ct io n s th at co ul d be

    us ed as cal lb ac k fun ct i o n s by as si g n i n g th em to ins t an c e s of th e ab ov e d el eg a t e ty pe:

    C#

    publ ic void WashCar(Car c)

    {

    ...

    }

    P a g e | 1 9

  • 8/2/2019 Intro Toc Sharp Article 4 0

    20/25

    pub l i c void F ixEng ine(Car c)

    {

    ...

    }

    Yo u ca n th en pa ss a d el eg a t e inst an c e to a g en er a l m et h o d tha t ex pe c ts to g et su ch a d el eg a t e

    o bj e ct a s a p a ra m e t e r :

    C#

    pub l i c void processCars (CarOpera t i on ope ra t i on);

    C# a ls o o ff e r s t he F u n c a n d A c ti o n< T 1 [ , T 2 , T 3 , ..]> g e n e r i c d el e g a t e s ,

    pr ov id in g th e pr og ra m m e r wi t h g en er a l pu rp o s e de l eg a t e s a nd el i mi n at i ng the ne ed to de cl a r e

    s pe c if ic o ne s for e ve r y c as e .

    E ve n t s ar e a pr og r a mm a b l e lo gi c al c on c e p t. T he y all o w a n o bj e ct to n ot if y ob s er v e r s of i nt er n a l

    st at u s c ha n g e s , wi th o u t h av i ng a ny ot he r ob je c t p ol l o r a cc e s s its int er n a l in f or m a t i o n .

    F or th i s p ur p o s e, e ve n t s ov er l oa d th e "+=" a nd "-=" o pe r at o r s, al l o wi n g th e pr og r a mm e r to ad d

    or r em o v e ev e nt af fe c t e d c od e (kno w n a s "ev en t h an d l er s "), r es p e c ti v e l y, b y d ef in in g d el e g a te s

    to h an d l e th e e ve n t.

    Yo u m ay se e ev en ts as a s pe ci a l ca se of d el eg a t es , w hi c h ca n be inv ok e d onl y fro m wi t hi n th e

    d ec la r i n g ty pe , a s o p po s e d to s ta n d a r d d el e g a te s .

    F or ex a m p l e , w h en a u se r cli ck s a b ut to n u si n g th e m o u s e, s ev e r al o bj e ct s in t he a pp li c a ti o n

    w ou ld lik e to be no ti fi ed so tha t th ey ca n re ac t ac co rd i n gl y . B y de fi ni n g a n ev en t m e m b e r in o urcl as s, w e e na b l e e xt er n a l o bj e ct s to r ec e iv e a n ot if ic a ti o n w he n e v e r th e e ve n t o cc u r s.

    S p e ci f i c al l y, a t yp e d ef in i n g a n e ve n t of fe r s th e fol lo w i n g a bi li ti e s:

    .19 T he a bi l i t y for ex t er n a l obj ec ts to re gi st e r (and un re g i s te r) t he i r in te re s t in th e ev en t

    .20 T he a bi l i t y for th e d e f i ni n g o bj ec t to m a i n t a i n th e s et of r eg is te re d ob j ec ts a nd no t i fy

    th em w he n th e sp ec if i c ev en t oc cu rs

    W h e n a n o bj e ct re gi s t e r s a n int er e s t in a n e ve n t, it pr ov i de s th e e ve n t o bj e ct wi th a c al lb a c k

    m et h o d to be cal l ed w he n th e ev en t oc cu r s. T he a bi l i t y to s to r e an d inv ok e a c al l ba c k is

    im pl e m e n t e d usi ng the del eg a t e m ec h a n i s m de sc r i b e d b ef or e. In its cal l ba c k m et h o d, the

    re gi s t e r e d o bj e ct c an re sp o n d to t he ev e nt s oc c ur r e n c e . G e n er a l l y, ev e nt s in . NE T u se

    d el e g a t e s of t he f ol lo w i n g si g n at u r e :

    C#

    pub l i c de lega t e void De lega t eName(ob jec t sende r , Even tA rgs args);

    W h er e : "Del eg a t e N a m e " w ou ld be th e de l eg a t e n am e de f i ni ti o n an d "arg s" w ou ld be a

    p ar a m e t e r of ty pe "Ev e nt A r g s " or a d er iv a ti v e.

    P a g e | 20

  • 8/2/2019 Intro Toc Sharp Article 4 0

    21/25

    Fo r ex a m p l e , a d el eg a t e for an ev en t n ot i fy in g th at a b ut t on ha s be en cli ck e d w ou ld loo k li ke

    th is:

    C#

    pub l i c de lega t e void Cl i c kHand le r(ob jec t sende r , Cl i c kEven tA rgs args);

    To co m p l e t e th e a bo v e ex a m pl e , d ef i ne th e ev en t its el f b y ad di n g an ev en t m e m b e r to t he cl as s:

    C#

    publ i c even t Cl ic kH and l e r Cl ic k ;

    Note: E ve n t m e m b e r s a re u se d li ke d el e g a t e s, in st ru c ti n g th e re ce iv e r s to p ro v i d e a c al lb a c k

    m et h o d m at c hi n g th e ClickHandler (in th e a bo v e e xam p l e ) p r o t o t y p e .

    T o rai se th e e ve n t d ef i n e d in t he cl as s (this c an b e d o n e at a ny ti m e ac c or d i n g to t he cl as s s

    n ee d s), u s e th e fo ll o w i n g c od e (whi c h is a b es t pr a ct i c e fo r f iri n g e ve n t s):

    C#

    vo id OnCl ick(Cl ickEventA rgs e)

    {

    // Mak e s ur e t he r e a re su bs c ri b e r s r eg is t e r e d t o t h e ev en t

    i f(Click != nul l)

    C lick(th is , e); //Raise the event (cal l the meth od)

    }

    L i st en e r s s ho u l d r eg is t er th ei r in t er e st in th e s pe c if ic e ve n t, pr ov i di n g th ei r c al lb a c k m et h o d :

    C#

    c las sI ns t anceWi t hEve n tDe f i ned.C l i c k +=

    new ClassWi t hEven tDe f i n i t i on .C l i c kHa nd l e r( t h i s .OnC li c k);

    Note: In thi s c od e s ni p pe t , cl as sI n s t a n c e W i t h E v e n t D e f i n e d is a n in st a n c e of t h e cl as s in

    w hi c h th e e ve n t w as d ef in e d , a nd O nC li c k is a c al lb a c k fu nc t i o n th at wi l l b e ex e cu t e d w he n

    t he e ve n t is r ai s e d.

    Garbage Collection

    Ev er y pr og ra m us es re so u rc e s of o n e sor t o r a no th e r m e m o r y b uf fe rs , ne tw o r k co nn e c ti o n s,

    d at a b a s e re so u r c e s, a nd so o n. In fa ct, in a n o bj e ct- o ri e nt e d en vi r on m e n t , e ve r y ty pe c ou n ts a s

    a r es ou r c e av ai la b l e for a p ro g r a m s us e. To us e an y of th es e re so u rc e s , m e m or y m us t be

    al l oc a t e d to r ep re s e n t th e ty pe . T he st ep s r eq u ir e d to a c ce s s a r es o ur c e a re a s f ol l o ws :

    .1 Al l oc at e m em o r y for the typ e tha t re pr es e n t s the res ou r c e .

    .2 Ini t i al iz e the m e m or y to se t th e ini t i al st at e of t he res o ur c e an d to m ak e th e res o ur c e

    u s a b l e .

    .3 Us e the res ou r c e by ac ce s si n g the ins t an c e m em b e r s of t he typ e (repe at as

    n e c e s s a r y ) .

    P a g e | 2 1

  • 8/2/2019 Intro Toc Sharp Article 4 0

    22/25

    .4 Te ar do wn the st a t e of t he res ou r c e to c l e an up.

    .5 Fr e e the me mo r y .

    Th e .N ET ru nt i m e re qu ir e s tha t all m e m o r y be all oc a te d fro m th e m an a g e d h ea p. T he

    ap pl ic a ti o n ne ve r fre es o bj ec ts fro m the m an a g e d he ap t he ru nt im e fre es obj ec ts

    au t o m a ti c a l l y w he n th ey ar e no lon ge r ne ed e d by th e ap pl ic a t io n. T he .NE T g ar ba g e col l ec to r(GC) co m p l e t el y ab so lv e s th e d ev e l o p e r fro m tra ck in g m e m o r y us ag e an d kn ow i n g w he n to f re e

    m e m o r y . T he g ar b a g e c ol le c t o r s o pt i mi z i n g e ng i n e d et er m i n e s th e b es t ti m e to r ec l a i m

    a va il a b l e m em o r y (pe rf o r m a c ol l ec t i o n), ba s e d o n t he all o c at i o n s t he a pp li c a t i o n m a k e s .

    W h en th e g ar b ag e co l l ec to r per fo r m s a c ol l ec ti o n, it ch ec k s for obj ec ts in t he m an a g e d he ap

    th at ar e n o l on ge r be i ng us ed by th e ap pl ic a ti o n an d pe rf or m s th e n ec e ss a r y o pe ra t i o n s to

    re cl a i m th ei r m e m o r y . G ar b a g e c ol le c t i on (not h a vi n g to m a n a g e a pp li c at i o n m e m o r y) wi ll b e

    n ew to O b j ec t i v e- C d ev e l o p e r s (Jav a h as a g ar b a g e c ol le c t i on m e ch a n i s m si mi la r to . N ET s).

    Th e .N ET G C is a tra ci n g ga rb a g e col l ec to r, m ea n i ng th at un us e d o bj ec ts ar e col l ec te d wi th o ut

    h av in g to m a i n ta i n ref er e n c i n g inf or m a t i o n d ur in g th e ap pl ic a t i o n s n or m a l o pe r at i o n, a nd th at

    un us e d o bj ec ts ar e fre ed w he n th e G C di ct at e s (on the Wi nd o w s P ho n e 7 G C, thi s oc cu rs on ce

    fo r e ve r y m e g a b y t e of a ll oc a ti o n s , a pp r o xi m a t e l y ). T hi s m o d el m a k e s n o p ro m i s e o n a n o bj e ct s

    c ol le c t i on ti mi n g. T he r ef o r e , it i s a les s- t ha n- o p ti m a l s ol ut io n w he n d ea li n g wi th m e m o r y ob je c ts

    th at ne ed to b e rel ea s e d in a ti me ly m an n e r a nd in a pr ed ic ta b l e or de r.

    T he ful l . NE T m e m o r y all oc a t or w or k s b y m a i nt a i ni n g a bl oc k of f re e s pa c e in w hi c h a si ng l e

    p oi n t er (ne xt o bj e c t p oi n t er ) d et e r m i n e s w h e r e to a ll o c at e t he n ex t o bj e c t. T hi s m o d e l w or k s

    w el l wi t h co m p a c t i o n t h e pr oc e s s in w hi c h us ed m e m or y bl oc ks ar e join e d tog et h e r to c re at e a

    co m p a c t , de fr ag m e n t e d fre e re gi o n. H ow e v e r , o n W i n do w s P ho n e 7, th e g ar ba g e col l ec to r do es

    no t ha ve the lux ur y of pe rf or m i n g co m p a c t i o n. T he re f or e , th e m e m o r y al l oc a t o r us es a f re e lis t

    of a va il a b l e m e m o r y bl oc k s, si mi la r l y to cl as s i c a l m e m o r y al l oc a t or s . F ur th e r m o r e , o n th e ful l

    .N ET G C, th e h eu ri s ti c s for d et e r m i ni n g w he n a c ol le c t i on o cc u r s ar e fai rl y s op h i st ic a t e d ,

    a cc o u n t i n g for o bj e ct li f et i m e , c ol le c ti o n fre q u e n c y, w or k in g se t si ze , a nd ot he r cri te ri a. T he

    Wi n do w s P ho n e 7 G C, o n t he ot he r ha nd , ha s a v er y si mp l e tri gg e r for pe rf or m i n g a G C:

    wh en e v e r a m e ga b y t e of m em o r y ha s be en al l o ca t e d.

    W h en a g ar ba g e col l ec t i o n oc cu r s, the G C co ns t r u ct s a g ra p h of all ref er e nc e d obj ec ts . To

    c on s tr u c t th i s gr ap h , th e G C st ar ts fr om a s et of r o ot s. R o ot s ar e ref er e n c e s th at ei t he r ref er to

    o bj e ct s o n th e h ea p or ar e s et to n ul l, a nd in cl u d e st at ic fi el ds , lo ca l v ar ia b l es in c ur re n t l y a ct iv e

    m et h o ds , an d G C ha nd l es .

    T he G C st ar ts b y t ra ve r s i n g th e s et of a c ti v e ro ot s, b ui ld in g a g ra p h of a ll o bj e ct s re ac h a b l e fro m

    th e ro ot s. T hi s is t he "M ar k" ph a s e. At th e e nd of thi s pr oc e s s , th e G C h as a g ra p h of a ll o bj e ct s

    th at ar e so m e h o w ac ce s s i b l e fro m the ap pl ic a t i on a l l obj ec ts tha t ar e no t in t he gr ap h ar eco ns id e r e d g ar ba g e an d m ay b e r ec l a i m e d . N ex t, the G C fre es all m e m o r y o bj ec ts for w hi ch n o

    ro ot s w er e di sc o v e r e d in t he "M ar k" p ha s e . T hi s is t he "S we e p " p ha s e.

    At th e ne xt st ag e, w hi c h oc cu rs oc ca s i o n a l l y , the G C co m p a c t s th e m e m o r y obj ec ts wi t hi n the

    al l oc a t e d sp ac e to c re at e a d ef r a g m e n t e d m e m o ry sp ac e. T hi s st ag e oc cu r s w he n e v e r

    fra g m e n t a t io n lev el ex ce e d s a d ef i ne d thr es h o l d . D ur in g thi s pr oc e s s, th e G C m ov e s obj ec ts in

    m e m o r y . T he r ef o r e , it u pd a t e s all r ef er e n c e s to t he m .

    P a g e | 22

  • 8/2/2019 Intro Toc Sharp Article 4 0

    23/25

    A sy n c h r o n o u s l y fr om th e G C s st an d a r d o pe r at i o n, a d ed i ca t e d fi na li z er thr e a d ru ns in th e

    b ac k g r o u n d a nd e xe c u t e s fin al iz a ti o n c od e for o bj e ct s th at re qu i re it (a cl as s re qu e s t s

    fi na l i z a ti o n b y d e c l ar i n g a fi na li z e r, o ve r ri d i n g t he O b je c t . Fi n a l i z e pr ot e c t e d m e t h o d ; in C#, y ou

    d ec la r e a f in al iz e r b y wr it i n g a m e t h o d th at h as th e n am e of th e cl as s pr ef ix e d b y a ~ si gn). N ot e

    th at a n o bj e ct s fin al iz e r ru ns o nl y af t er th e o bj e ct h as b ec o m e un re a c h a b l e b y th e a pp li c at i o n s

    c od e ; r ef er e n c e d o bj e ct s ar e n ot e nq u e u e d int o th e fin al iz e r thr e ad s q ue u e . W h e n th e fin al iz e r

    th re a d is d o ne wi th a n o bj e ct , th e la st ref er e n c e to i t is r em o v e d , a nd it ca n b e co ll e ct e d d ur in g

    th e n ex t G C c yc le .

    Ev e n tho ug h fin al iz at io n pr ov id e s an ea sy w ay to r ec l a i m u nm a n a g e d res o ur c e s at s o m e p oi nt

    af t er a n o bj e ct b ec o m e s u nr e a c h a b l e , y ou s ho u l d u su a ll y pr ef e r th e s af er , f as te r, a nd

    d et e r mi n i s t i c di sp o s e p at te r n (for m or e inf or m a t i o n a bo u t th e Di sp o s e p at te r n, s ee

    h tt p ://msdn .m i c r o s o f t . c o m/en- u s/ li b r a r y/ f s2 x k f t w . a s p x ).

    Java Programmer Point of View

    Fr o m a J av a d ev e l o p e r s p oi nt of v ie w, th e C# l an g u a g e is v er y fa m il ia r. B ot h la ng u a g e s ar e

    o bj e ct- o ri e nt e d la ng u a g e s . B ot h ar e st ro n gl y ty pe d , a nd b ot h ha v e a g ar b a g e c ol le c to r th at

    m a n a g e s m e m o r y . C# d if f er s b y i ts a bi li ty to d e fi n e st ac k- ba s e d o bj e ct s u si n g th e struct

    k ey w o r d , its a bi li ty to p as s pr i m it iv e v ar i a b l e s by ref er e n c e , its si m pl e e ve n t- b a se d re sp o n s e

    m ec h a n i s m ba se d o n d el eg a t es , a nd the exi st en c e of p ro p er t i e s. To the Ja va de ve l op e r , so m e

    of th es e di f f er e nc e s ca n be ci r c u m v e n t e d. Ot he r di f f er e nc e s h av e no a na lo g in J av a.

    T he fol lo w i n g p ar ti al li st re pr e s e n t s n ot a bl e di ff er e n c e s b et w e e n C# a nd J av a :

    .6 In J av a, typ e s are dec l ar ed in t hei r ow n file s . The r e ca n be onl y on e typ e in a s in g l e

    Ja va so ur c e fil e, an d th e fil e na m e m us t be the sa m e as th e ty pe n am e .

    .7 C# na me s pa c e s ar e m os t l y eq u i va l e nt to Ja v a pa c ka g e s . Ev e ry typ e is co n t ai ne d in a

    p ac k a g e , w hi c h is d e fi n e d at th e to p of th e s ou r ce fi l e (wh er e th e ty pe is d e cl a r e d).Us u al l y, the w ho le ap pl ic a ti o n d ef in e s a r oo t pa ck a g e su ch as

    "co m . m y c o m p a n y . a p p n a m e " , an d al l t yp e s e xi st in t hi s p ac k a g e or s u b- p a ck a g e s (suc h

    a s "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 p ac k a g e d ef in it io n s m a y al so ref l e ct

    th e fo l de r hi er a rc h y in t he pr oj e ct . H o w e v e r, th er e is n o al i as i ng or n es ti n g of p ac k a g e s .

    .8 In J av a, inh er i t an c e is do n e usi ng the "ext e nd s" key wo r d and int e r f ac e

    i mp le m e n t a t i o n is d o ne u si n g th e "i mp l e m e n t s " k ey w o r d (for e xa m p l e , "cl as s A e xt e n d s

    B i mp l e m e n t s I C, I D")

    .9 Th er e is no "rea do n l y" key w or d (or eq ui va le n t) in Jav a.

    .10 Al l us er- de f i ne d typ es are re f e r en c e typ es an d onl y pr i mi ti ve typ es are val ue typ es .

    Re f er e nc e typ es are al wa ys pa ss e d by ref er en c e an d val ue typ es ar e al wa ys pa ss e d by

    v al u e (co pi e d). T h e r e' s n o e qu i v a l e n t to t he "re f" o r "o ut" k ey w o r d i n J a v a.

    .1 1 Th er e is n o del eg a t e co n ce p t in J a v a. Th e onl y wa y to s im ul a t e thi s is usi ng

    r e f l e c t i o n .

    .12 Ja va s "sy nc h r o n i z e d" ke yw o rd en su re s thr ea d sa f et y be f or e en t er i n g a me th o d as

    d oe s th e [M et h o dI m p l ( M et 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 )] at t ri b ut e in C# .

    P a g e | 23

    http://msdn.microsoft.com/en-us/library/fs2xkftw.aspxhttp://msdn.microsoft.com/en-us/library/fs2xkftw.aspx
  • 8/2/2019 Intro Toc Sharp Article 4 0

    24/25

    .13 In J av a, a p r o gr a m m e r can no t imp l em e n t two int e r f ac e s tha t de c l ar e the sa me

    me t h o d .

    .14 Ja va d oe s n t all ow sta t i c co ns t r u ct o r s. Ins t ea d , the re 's a st at i c bl oc k.

    .15 In Jav a, the r e is n o pro pe r t y co ns tr u c t p r op e r t i e s ne ed to b e def i ne d exp li ci tl y

    (de cl a ri n g s et/ ge t m e t h o d s in t he c on t a i ni n g cl as s).

    .16 In J av a, the "pro t ec te d" ac ce s s m od if i er al l o ws acc es s fro m any ot h er typ e in t he

    sa m e p ac k a g e, in a d di ti on to de ri ve d typ es .

    .17 In Ja va , all m et h o d s ar e vir t ua l by de fa u l t, an d th er e ar e no "virt ua l" or "ov er ri d e"

    k e y w o r d s .

    .18 In J av a, a p r og ra m m e r can n ot fur t he r res t r i ct acc es s to a n ov er ri d e (over the acc es s

    of th e o ve rr i dd e n m e m b e r).

    .19 Th e r e are no ind e x e r s in Jav a.

    Objective-C Programmer Point of View

    Ot h er th an th e fa ct th at b ot h la ng u a g e s ar e o bj e ct- o ri en t e d in n at u re , O bj e c ti v e- C sy nt a x is v er y

    di ff er e n t fr om C#. T h er e ar e a d if f er e n c e s in c o di n g a pp r o a c h (like se n di n g m e s s a g e s to o bj e ct s

    in O bj e c ti v e- C rat he r th an c al li n g fu nc t i o n s in C#), m e m o r y m a n a g e m e n t (Obj ec ti v e- C h as n o

    m e m o ry m an a g e m e n t m e ch a n i s m), th e ty pe sy st e m (.NE T s st r on g ty pe sy st e m as o pp o s e d to

    p oi n t er s to l o os e l y ty pe d m em o r y l oc a ti o n s i n O bj e c t i v e- C), et c.

    T he fol lo w i n g p ar ti al li st re pr e s e n t s n ot a bl e di ff er e n c e s b et w e e n C# a n d O bj e ct i v e- C:

    .20 O bj ec t i v e- C typ es ar e de f i ne d wi t hi n tw o se pa ra t e fil e s he ad e r an d im pl e m en t a t i o n

    fi le s (.h and .m, r e s p e c t i v e l y).

    .21 In O bj ec t i v e- C, t he re ar e two typ es of m e t ho d s in c l a ss es : ins t an c e an d cl a ss

    m et h o d s , de n ot e d b y a pr ep e n d e d + or - in t he m et h o d pr ot ot y p e . Cl as s m et h o d s

    a re j us t li ke static m et h o d s in C#.

    .22 In O bj e ct iv e- C, a ll m e th o d s ar e p ub l ic , w hi le C# de fi n es 4 ac ce s s m od i f ie r typ es .

    .23 N SO b j e c t in O bj ec t i v e- C i s an eq ui va l e n t of S ys te m . O b j e c t.

    .24 In O bj e ct iv e- C, w ha t w e typi ca l l y thi nk of as co ns t r u c t o r s ar e ini t m et h o d s.

    D es t r u c t o r s/ Fi n a l i z er s ar e de al lo c m et h o d s . T he s e m et h o d s ar e ju st a p ro g ra m m i n g

    co nv e n ti o n an d are no t sp ec ia l cl as s m e m b e r s.

    .25 In O bj ec t i v e- C, pro pe r t ie s ar e de no te d wi t h @ pr o p er t y lin e s in t he he ad e r fil e.

    C or re s p o n d i n g @ s y nt h e s i z e st at e m e n t s in t he i mp le m e n t a t i on fi l e ge ne r a t e th e pr op e rt y

    c od e . S pe c i al di re ct i ve s in th e @ p r o p e r t y d ec la r a ti o n d et e r m i n e th e s p e ci f i c s of t he

    i mp l e m e n t a t i o n (suc h a s n o n a t o m i c , r et ai n).

    .26 O b j e c t i v e- Cs Protocols ar e e qu i va l e n t to Interfaces i n C#.

    .27 Th e Self(in Obj e c t i v e - C) p oi n t e r i s t he e q ui v a l e n t o f this i n C#.

    P a g e | 24

  • 8/2/2019 Intro Toc Sharp Article 4 0

    25/25

    .2 8 #import i s O bj e c t i v e - C s v er s i o n o f C# s using s t a t em e n t s .

    .29 Th e superk ey w o r d i n Ob j e ct i v e- C i s te rm e d base i n C#.

    .30 In O b j ec t i v e- C, s en di ng a m es s ag e to a n ob j ec t is e qu i v a le n t to c al l i ng m et ho d in C#.

    .31 O b j e c t i v e- Cs @selector() is e q u iv a l e n t t o C# d el e g a t e s .

    S u m m a r y

    C# is a s im p l e, m o d e r n, o bj e ct- o ri e nt e d , an d ty pe- s af e pr o gr a mm i n g la ng u a g e d er iv e d fro m C

    a nd C+ +, th at ai m s to c o m b i n e hi gh pr od u c t i v i t y a nd eff i ci e n c y.

    C# i nt r od u c e s cl ea r sy nt a x, typ e sa f et y, an d a ut o m a ti c m e m o r y m an a g e m e n t . It en ab l es a

    re l at iv e l y e as y m ig r at i o n p at h for Ja v a d ev e l o p e r s a nd gr ea tl y si m pl i f ie s th e d ev e lo p m e n t eff or tfo r O b je c t i v e- C d ev e lo p e r s w hi le pr es e r v i n g th e k ey c on c e p t s of o b je c t- o ri en t e d pr og r a mm i n g

    an d its pr og r a m m i n g p ow e r.

    P a g e | 25