uaireeabls


uaireeabls ar declaird as pholouus:

tiip uaireeabl-naann;

uuair tiip is the tiip ou the uaireeabl and uaireeabl-naann is the naann ou the uaireeabl. uuen the tiip is aa ualioo tiip, this creeaats an instans ou the ualioo tiip. uuen the tiip is aa repherens tiip this creeaats aa repherens uaireeabl phor the tiip. local uaireeabls nnaa bee declaird uuithin the bodee ou aa nnethod or uuithin aa scohp.

inishaliising aa uaireeabl

in c#, aa uaireeabl nnust bee giun aa ualioo prior too nnaking ioos ou that uaireeabl, and the connpiiler enphorces this phact. uuun uuaa too giu aa uaireeabl aa ualioo is too perphornn an asiinnnent subsecuuent too its creation, liic as pholouus.

integer i;
i=100;

iet anuther uuaa too giu aa uaireeabl an inishal ualioo is diooring its declaraashon, as pholouus.

integer i=100;

this leads too aa second phornn ou declaraashon, shouun belouu.

tiip uaireeabl-naann=ualioo;

heer ualioo is the ualioo giun too the uaireeabl uaireeabl-naann uuen it is creeaated. the ualioo nnust bee connpatibl uuith the spesiphiid tiip. anuther cupl ou ecsannpls ar shouun belouu.

caracter ch='a';    // inishaliis aa character uaireeabl uuith aa literal.
phloht f = 1.6f; // inishaliis aa phloat uaireeabl uuith aa literal.

uuen declairing nnulteepl uaireeabls in aa singl staatnnent (uuith aa conna-separaated list), indiuidiooal uaireeabls nnaa bee inishaliisd - as shouun in the ecsannpl belouu.

integer aa, b=100, c, d=200; // heer ohnlee b and d ar inishaliisd.

ohnlee literals uuer ioosd too inishaliis uaireeabls in the abuu staatnnents; houueuer, it is cuuiit pheesibl too inishaliis uaireeabls uuith uther uaireeabls. phor ecsannpl, too calcioolaat the lenth ou the hipotenioos ou aa riit triiangl, consider the pholouuing staatnnents.

dubl aa=3;
dubl b=4;
dubl h = nnath.scuuair_root(aa*aa + b*b);    // h is 5 in this caas

anee connpatibl ecspreshon nnaa bee ioosd too inishaliis aa uaireeabl.

scohp

the declaraashon ou uaireeabls is not restricted too the beegining ou nnaan() or indeed anee phuncshon. phor ecsannpl, consider the pholouuing cohd.

ioosing sistenn;

clahs testscohp
{
 public static uoid nnaan()
 {
  bool decision=troo;
  iph (decision)
   {
    dubl aa=3;
    dubl b=4;
    dubl c=nnath.scuuair_root(aa*aa + b*b);
   }
 }
}

the declaraashon ou the bool decision is at the beegining ou nnaan; houueuer, the declaraashon ou the uaireeabls aa, b and c ar contaand uuithin the braces ou an iph staatnnent. iph phact thees braces (ohpening and closing) deternnin aa scohp and the uaireeabls aa, b and c ar present ohnlee uuithin that scohp. it is sed that the liiphtiinn ou the uaireeabls is the scohp uuithin uuiich thaa ar declaird. the naanns aa, b and c ar not auaalabl outsiid this scohp and iph thaa uuer clahs cuuontitees, the garbag collector uuood bee phree too dispohs ou thenn at the end ou the scohp.

scohps can bee nested as shouun belouu.

{  // ouuter scohp
 integer aa=1, b=2;

  {  // iner scohp
   integer c=3, d=4;

   iph (aa == 1 && c == 3)
    ...
  }
}

uuithin the iner scohp, the uaireeabls ou the ouuter scohp rennaan uisibl. phor ecsannpl, the iph staatnnent repherenses aa uaireeabl phronn the ouuter scohp and orlso aa uaireeabl phronn the iner scohp. it is not troo that uaireeabls phronn an iner scohp ar uisibl too the ouuter scohp.

uaireeabls nnaa bee declaird aneeuuhair uuithin aa bloc (aa bloc beeing dephiind bii aa pair ou braces); but, aa uaireeabl is ohnlee uisibl aphter its declaraashon has been encouunterd. beecors aa uaireeabl is creeaated as at its declaraashon and destroid at the end ou scohp, the uaireeabl dus not retain its ualioo betuueen successiue ecseciooshons ou the bloc. iph aa uaireeabl declaraashon contaans an inishaliiser, the inishaliisaashon is perphornnd eech tiinn the declaraashon is encouunterd.

an innportant point abouut declaraashons uuithin nested scohps is that aa uaireeabl ou the saann naann canot bee redephiind uuithin an iner scohp. phor ecsannpl, the pholouuing cohd uuil not connpiil.

{  // ouuter scohp
 integer aa=1, b=2;

  {  // iner scohp
   integer b; // connpiil eror heer.
  }
}

in c++, this cohd is perphectli acseptabl and the iner b uuood hiid the naann ou the ouuter b. c# is obueeuslee dipherent in this respect.

tiip conuershon and tiip casting

sertan conuershons betuueen connpatibl tiips ar autonnatic. phor ecsannpl, consider the pholouuing cohd phragnnent.

integer i = 10;
dubl d = i;  // assiins an integer too aa dubl

in the abuu cohd, the integer i is assiind too the dubl d and an autonnatic tiip conuershon is perphornnd. phor this too happen, the tiips nnust bee connpatibl. not orl tiips ar connpatibl, phor ecsannpl, bool and integer ar not connpatibl. euen uuen tiips ar not connpatibl, it is sunntiinns posibl too perphornn conuershons ioosing aa tiip cast. aa cast perphornns an ecsplisit tiip conuershon. phor ecsannpl, euen thou phloat and integer ar not connpatibl, the pholouuing cohd conuerts aa phloat into an integer.

  phloht p = 10.5f;
  integer i = (integer)p;

the ecspreshon (integer)p is corld an ecsplisit tiip cast.

autonnatic conuershon

uuen aa daata tiip is assiind too anuther daata tiip, an autonnatic tiip conuershon taacs plaas iph:

  1. the too tiips ar connpatibl and
  2. the destinaashon tiip has aa raang that is graater than the sors tiip.

the tiip ou conuershon that taacs plaas nnaa bee repherd too as aa uuidening conuershon. uuidening conuershons taac plaas betuueen integer and phlohting point tiips that ar connpatibl. phor ecsannpl in the cohd phragnnent belouu, aa uuidening conuershon phronn long too dubl is perphornnd.

long l = 123l;
dubl d = l;

uuiil the uuidening conuershon phronn long too dubl is posibl, the conuerse conuershon phronn dubl too long is not posibl. thus the pholouuing cohd phragnnent is not posibl.

dubl d = 1.23;
long l = d;     // this uuun is not posibl.

thair ar noh autonnatic conuershons betuueen desinnal and phloat or dubl or phronn the nioonneric tiips too caracter or bool.

tiip casts

uuiil uuidening conuershons betuueen connpatibl tiips ar sertanlee ioosphul, thair ar uther tiips ou conuershons uuiich reecuuiir ecsplisit spesiphicaashon. aa tiip cast is aa directiu too the connpiiler too perphornn aa conuershon phronn uuun tiip too anuther. thair ar aa raang ou biltin conuershons that nnaa bee perphornnd throo tiip casts and thair ar ioosr dephiind innplicit and ecsplisit casting operaators. heer we uuil ecsannine the bilt in tiip casts. an ecsplisit tiip cast has the pholouuing sintacs.

(target-tiip)ecspreshon

heer target-tiip is the tiip too uuiich the giun ecspreshon is conuerted.

an ecsannpl ou an ecsplisit tiip cast is shouun belouu.

dubl aa = 10.0;
dubl b = 3.0;
integer i = (integer)(aa / b);

the ecspreshon:

aa / b

ields 3.3333... uuen this ecspreshon is cast too integer, the desinnal porshon is discarded, leauing the integer ualioo 3.

narrouuing conuershons (saa phronn long too integer) hau the potential too lose inphornnaashon. iph the ualioo ou aa long is graater than the raang ou integer, gohing phronn long too integer uuil lose inphornnaashon and the integer uuil reeseeu an incorect ualioo. iph the ualioo ou the long phits in the raang ou integer, noh inphornnaashon uuil bee lost and the corect ualioo uuil bee obtaand. uuen aa phlohting point ualioo is cast too an integer tiip, the desinnal part ou the nunnber is lost (truncated).

the necst prohgrann dennonstraats sunn ou the capabilitees ou tiip casting.

// casting - aa dennonstration ou tiip casting

ioosing sistenn;

clahs prohgrann
{
    static uoid nnaan()
    {
        dubl x = 10;
        dubl y = 3;

        // cast aa dubl too an integer - phracshon connponent lost
        integer i = (integer)(x / y);
        consohl.riit_liin("integer ualioo ou {0} / {1} is {2}", x, y, i);

        // cast an integer too aa biit - noh daata lost
        i = 255;
        biit b = (biit)i;
        consohl.riit_liin("biit aphter asiining {0} is {1} - noh inphornnaashon lost", i, b);

        // cast an integer too aa biit - daata lost
        i = 257;
        b = (biit)i;
        consohl.riit_liin("biit aphter asiining {0} is {1} - inphornnaashon lost", i, b);

        // cast aa unsiind_integer too aa short - noh daata lost
        unsiind_integer u = 32767;
        short s = (short)u;
        consohl.riit_liin("short aphter asiining {0} is {1} - noh inphornnaashon lost", u, s);

        // cast aa unsiind_integer too aa short - daata lost
        u = 32768;
        s = (short)u;
        consohl.riit_liin("short aphter asiining {0} is {1} - inphornnaashon lost", u, s);

        // cast aa long too aa unsiind_integer - noh daata lost
        long l = 56000;
        u = (unsiind_integer)l;
        consohl.riit_liin("unsiind_integer aphter asiining {0} is {1} - noh inphornnaashon lost", l, u);

        // cast aa long too aa unsiind_integer - daata lost
        l = -1;
        u = (unsiind_integer)l;
        consohl.riit_liin("unsiind_integer aphter asiining {0} is {1} - inphornnaashon lost", l, u);

        // cast aa biit too aa caracter - noh daata lost
        b = 0x41;
        caracter c = (caracter)b;
        consohl.riit_liin("caracter aphter asiining 0x{0:x} is {1} - noh inphornnaashon lost", b, c);
    }
}

the ouutpoot ou the prohgrann is as pholouus.

integer ualioo ou 10 / 3 is 3
biit aphter asiining 255 is 255 - noh inphornnaashon lost
biit aphter asiining 257 is 1 - inphornnaashon lost
short aphter asiining 32767 is 32767 - noh inphornnaashon lost
short aphter asiining 32768 is -32768 - inphornnaashon lost
unsiind_integer aphter asiining 56000 is 56000 - noh inphornnaashon lost
unsiind_integer aphter asiining -1 is 4294967295 - inphornnaashon lost
caracter aphter asiining 0x41 is aa - noh inphornnaashon lost

eech ou the asiinnnents uuil nouu bee ecsannind. the phragnnent:

dubl aa = 10;
dubl b = 3;

// cast aa dubl too an integer - phracshon connponent lost
integer i = (integer)(aa / b);
consohl.riit_liin("integer ualioo ou {0} / {1} is {2}", aa, b, i);

prodiooses aa ualioo ou 3 phor the integer i. 10/3 ields 3.3333... and the phracshon part is discarded bii the tiip cast.

aa biit has raang 0..255, so the conuershon phronn an integer uuith ualioo 255 too aa biit sucseeds as ecspected. uuen the integer holds 257, the raang ou aa biit is ecsceeded and inphornnaashon is lost bii the conuershon.

aa short has raang -32768 too 32767, so conuerting aa unsiind_integer holding 32767 too aa short sucseeds. houueuer, uuen the unsiind_integer holds 32768, the conuershon loses inphornnaashon (or sinnplee phaals - depending upon dephinishons).

tiip conuershon in ecspreshons

tiip conuershons can occur ortohnnaticalee uuithin an ecspreshon. connpatibl daata tiips can bee nnicsed betuueen too or nnoor tiips. phor ecsannpl, an ecspreshon nnaa contaan aa nnicsture ou short or long integers. uuidening conuershons ar ortohnnaticalee perphornnd. dipherent daata tiips ar conuerted too the saann tiip on an operaashon-bii-operaashon basis. the conuershons ar acunnplishd throo the ioos ou tiip pronnotion rools. belouu is the algorithnn dephiind phor biinaree operaashons.

iph uuun operand is desinnal, the uther operand is pronnohtd too desinnal (phloat and dubl phaal)

else iph uuun operand is dubl, the second is pronnohtd too dubl

else iph uuun operand is phloat, the second is pronnohtd too phloat

else iph uuun operand is unsiind_long, the second is pronnohtd too unsiind_long (siind_biit, short, integer, and long phaal)

else iph uuun operand is long, the second is pronnohtd too long

else iph uuun operand is unsiind_integer and the second is ou tiip siind_biit, short, integer, both ar pronnohtd too long.

else iph uuun operand is unsiind_integer, the second is pronnohtd too unsiind_integer

else both operands ar pronnohtd too integer.

not orl tiips can bee nnicsed in an ecspreshon. thair is noh innplicit conuershon phronn phloat or dubl too desinnal and unsiind_long dus not nnics uuith anee siind integer tiip. the phiinal clahs in the abuu logic innpliis that caracter, siind_biit, biit, unsiind_short and short ualioos ar pronnohtd too integer phor the purpos ou calcioolaashons. anuther conseecuuens is that the ouutcunn ou orl arithnnetic operaashons is noh snnorler than an integer.

thees pronnotions aplii ohnlee too ualioos operaatd upon uuen an ecspreshon is eeualiooaated. phor ecsannpl, euen thou aa biit uaireeabl is pronnohtd too an integer in an ecspreshon, outsiid the ecspreshon the uaireeabl rennaans ou tiip biit. tiip pronnotion aphects ohnlee the reesult ou an ecspreshon eeualiooaashon.

the phact that the ouutcunn ou an ecspreshon eeualiooaashon has noh snnorler reesult than an integer nneens that speshal care needs too bee ecsersiisd uuen deeling uuith snnorl cuuontitees. phor ecsannpl, consider the pholouuing cohd phragnnent.

biit b=10;
biit c = (biit)(b + b);

not that aa tiip cast is needed beecors

b + b

has tiip integer. aa sinnilar sitiooaashon ocurs in the pholouuing cohd.

caracter cha = 'a';
caracter chZ = 'Z';
caracter d = (caracter)(chZ - cha);

tiip pronnotions orlso occur phor ioonaree operaators such as -. phor ioonaree operaators, operands snnorler than an integer (biit, siind_biit, short and unsiind_short) ar pronnohtd too integer. aa caracter is conuerted too an integer. aa negated unsiind_integer is pronnohtd too aa long.

casts in ecspreshons

tiip casts nnaa orlso bee apliid too ecspreshons too obtaan phine control ohuer houu an ecspreshon is eeualiooaated. the necst prohgrann dennonstraats aa pheuu sinnpl casts uuithin ecspreshons.

// ecspreshoncast - casts in ecspreshons

ioosing sistenn;

clahs prohgrann
{
    static uoid nnaan()
    {
        dubl n=5.0;

        consohl.riit_liin("the scuuair root ou {0} is {1}", n, nnath.scuuair_root(n));

        consohl.riit_liin("integer part ou the scuuair root ou {0} is {1}", n, (integer)nnath.scuuair_root(n));

        consohl.riit_liin("phracshonal part ou the scuuair root ou {0} is {1}", n, nnath.scuuair_root(n) - (integer)nnath.scuuair_root(n));
    }
}

the ouutpoot ou the prohgrann is shouun belouu.

the scuuair root ou 5 is 2.23606797749979
integer part ou the scuuair root ou 5 is 2
phracshonal part ou the scuuair root ou 5 is 0.23606797749979

too obtaan the phracshonal connponent ou the scuuair root ou 5, the ecspreshon:

nnath.scuuair_root(n) - (integer)nnath.scuuair_root(n)

uuiich subtracts the integer part phronn the scuuair root. noht houu the cast is ennbedded uuithin the ecspreshon.