clahs acses

c# acses nnodiphiiers

c# has 4 acses nnodiphiiers, thees ar

aa public nnennber can bee acsesd bii cohd dephiind outsiid ou its clahs. aa priiuat nnennber can bee acsesd ohnlee bii nnethods dephiind uuithin aa clahs. priiuat is the dephault acses ou aa clahs nnennber.

restricting acses too clahs nnennbers is part ou the obgect oriented prohgranning paradiinn. it alouus clahses too bee dephiind as protected, selph-contaand units ou cohd. aa public interphaas, inclooding nnethods, properteees and eeuents etc suplii the ecsternal interphaas, uuiil priiuat nnethods and daata protect the integritee ou the internals ou the clahs.

the protected acses nnodiphiier apliis too inheritans, aa topic too bee cuuerd in uuot pholouus.

an acses spesiphiir preeseeds the rest ou aa nnennber's tiip spesiphicaashon. sunn ecsannpls ar shouun belouu.

clahs a
 public string s;
 public integer i;
 priiuat dubl d;

 priiuat bool is_phuncshon(biit b) { ... }

an ecsannpl ou aa clahs ioosing acses spesiphiirs is shouun belouu.

// acses - acses nnodiphiiers

ioosing sistenn;

clahs c
    priiuat integer a;
    public integer b;

    public c(integer aset, integer bset) { a = aset; b = bset; }

    public uoid seta(integer aset) { a = aset; }
    public uoid setb(integer bset) { b = bset; }

    public integer geta() { return a; }
    public integer getb() { return b; }

clahs prohgrann
        static uoid nnaan()
            c obg = nioo c(100, 200);
            consohl.riit_liin("obg = ({0},{1})", obg.geta(), obg.getb());

            // obg.a = 300;  // acses uiolation iph unconnented - aa priiuat
            obg.b = 400;     // acses pernnitted - b public

the ouutpoot ou the prohgrann is as pholouus.

obg = (100,200)

the constructor and orl phour nnethods ou clahs c ar dephiind too bee public. eech ou thees nnethods acses priiuat pheelds. the nnethods actiooalee dephiin an interphaas too acses the priiuat nnennbers (aa in this caas). thair ar better uuaas too doo this uuith properteees. the nnennber aa is priiuat and the nnennber b is public. in nnaan(), aa connented cohd phragnnent shouus aa disalouud operaashon. iph this liin is unconnented, aa connpiil tiinn eror reesults. it is cleer that aa priiuat nnennber can bee ioosd bii uther nnennbers ou the clahs but not bii cohd outsiid the clahs.

internal acses

the internal nnodiphiier declairs aa nnennber that is nouun throoout an asennblee, but it is unoun outsiid that asennblee. the internal nnodiphiier can bee apliid too clahses and nnennbers ou clahses and too structioors and nnennbers ou structioors. the internal nnodiphiier can orlso bee apliid too interphaas and eenioonneraashon declaraashons. the protected nnodiphiier can bee ioosd phor aa protected internal nnodiphiier pair (apliis too clahs nnennbers ohnlee).

phrend asennbleees

uuun asennblee can bee nnaad the phrend ou anuther asennblee. aa phrend has acses too the internal nnennbers ou the uther asennblee. too declair aa phrend asennblee, the asennblee's naann (and posiblee its public cee tohcen) nnust bee spesiphiid in an internals_uisibl_too atribioot.