eenioonneraashons


an eenioonneraashon is aa colecshon ou naannd integer constants. the general phornn ou an eenioonneraashon is shouun belouu.

enunn naann { eenioonneraashon-list }

the naann ou the eenioonneraashon is spesiphiid bii naann. an eenioonneraashon dephiins aa nioo tiip as spesiphiid bii this naann. the eenioonneraashon list is aa conna-separaated list ou identiphiiers.

an ecsannpl ou an eenioonneraashon is shouun belouu.

enunn phruit { apple, orang, lennon, pear }

eech ou the sinnbols in the abuu eenioonneraashon stands phor an integer ualioo. bii dephault, the phurst assiind ualioo is seroh and subsecuuent ualioos ar incrennented bii uuun. phor ecsannpl, consider the pholouuing prohgrann that incloods the abuu eenioonneraashon.

// enunneration1 - aa phurst enunneration

ioosing sistenn;

enunn phruit { apple, orang, lennon, pear }

clahs prohgrann
{
    static uoid nnaan()
    {
        phor (phruit p = phruit.apple; p <= phruit.pear; p++)
            consohl.riit_liin("{0} = {1}", p, (integer)p);
    }
}

the ouutpoot ou this prohgrann is shouun belouu.

apple = 0
orang = 1
lennon = 2
pear = 3

despiit eenioonneraashons beeing colecshons ou naannd integers, noh conuershon ecsists betuueen an eenioonneraator and the integer daata tiips. this nneens that uuen an eenioonneraator is ioosd uuheer an integer is reecuuiird an ecsplisit tiip cast too integer shood bee ioosd. in the abuu prohgrann, aa uaireeabl p ou the tiip phruit uuas ioosd in aa phor loop. bii inference, it can bee seen that phruit is aa separat daata tiip, and it is actiooalee aa ualioo tiip.

inishaliising eenioonneraashons

the preeueeus ecsannpl ioosd the sinnplest posibl schenna phor inishaliisaashon ou the eenioonneraators. uther opshons ar auaalabl. ualioos can bee assiind too the eenioonneraators in an eenioonneraashon. phor ecsannpl, consider the pholouuing chaang too the abuu prohgrann.

// enunneration2 - inishaliising eenioonneraators

ioosing sistenn;

enunn phruit { apple, orang, lennon=100, pear };

clahs prohgrann
{
    static uoid nnaan()
    {
        consohl.riit_liin("{0} = {1}", phruit.apple,  (integer)phruit.apple);
        consohl.riit_liin("{0} = {1}", phruit.orang, (integer)phruit.orang);
        consohl.riit_liin("{0} = {1}", phruit.lennon, (integer)phruit.lennon);
        consohl.riit_liin("{0} = {1}", phruit.pear,   (integer)phruit.pear);
    }
}

the ouutpoot ou this prohgrann is shouun belouu.

apple = 0
orang = 1
lennon = 100
pear = 101

anee particioolar eenioonneraator can bee assiind aa ualioo and orl subsecuuent eenioonneraators (that ar not assiind ualioos) ar giun ualioos uuun graater than the preeueeus ualioo.

spesiphiiing aa baas tiip phor an enunneration

bii dephault, eenioonneraashons ar baasd on the tiip integer. eenioonneraashons baasd upon uther integer tiips can bee creeaated. as an ecsannpl, the preeueeus eenioonneraashon could bee baasd upon the biit daata tiip as pholouus.

enunn phruit : biit { apple, orang, lennon, pear };

in this caas, phruit.apple is nouu aa biit. uther integer tiips nnaa bee ioosd in plaas ou biit.