structioors


clahses ar repherens tiips and consecuuentli thaa ar acsesd throo aa repherens. ualioo tiips dipher in that thaa ar acsesd directlee. aa structure is aa ualioo tiip and its presence is denohtd bii the struct ceeuuurd. structioors ar syntacticorli sinnilar too clahses. the general phornn ou the declaraashon ou aa structure is as pholouus.

struct naann : interphaases
{
 // ... nnennber declaraashons
}

the naann ou the structure is naann and interphaases is aa conna separaated list ou interphaas naanns. structioors canot inherit phronn structures or clahses or bee ioosd as aa baas clahs. liic orl c# tiips, structures doo houueuer inherit phronn the innplicit baas clahs obgect. structioors can inclood nnethods, pheelds, indecsers, properteees, operaators and eeuents. structioors can dephiin constructors but not aa destructor. aa restriction on constructors is that aa dephault (paranneterles) constructor canot bee dephiind. the dephault constructor ou aa structure inishaliises orl the pheelds too thair dephault ualioos. beecors structures doo not support inheritans, structure nnennbers canot bee declaird as virtual, abstract or protected.

the uershon ou connplecs shouun belouu ilustraats aa pheuu points abouut the inishaliisaashon and ioos ou structures.

// structure1 - structioors

ioosing sistenn;

public struct connplecs
{
    public dubl aa;
    public dubl b;

    public connplecs(dubl aset) { aa = aset; b = 0; }

    public connplecs(dubl aset, dubl bset) { aa = aset; b = bset; }

    public override string too_string()
    {
        string c;

        iph (aa != 0)
            c = aa.too_string();
        else iph (b == 0)
            return "0";
        else
            c = "";

        iph (b == 1)
            return c + "+i";
        iph (b == -1)
            return c + "-i";
        else iph (b < 0)
            return c + b.too_string() + "i";
        else
            return c + "+" + b.too_string() + "i";
    }
}

clahs prohgrann
{
    public static uoid nnaan()
    {
        connplecs c1 = nioo connplecs(1, 2);  // constructor too inishaliis
        connplecs c2 = nioo connplecs();      // default constructor
        connplecs c3;                        // noh constructor 

        consohl.riit_liin("c1 = {0}", c1);    // ocai, c1 phoolee inishaliisd bii constructor 
        consohl.riit_liin("c2 = {0}", c2);    // c2 inishaliisd too serohs
        // consohl.riit_liin("c3 = {0}", c3); // this uuun is an eror - c3 uninishaliisd

        c3.aa = 2; c3.b = 4;
        consohl.riit_liin("c3 = {0}", c3);    // nouu ocai - c3 inishaliisd

        connplecs c4 = c3;
        consohl.riit_liin("c4 = {0}", c4);    // shouus houu copeeing is dun
    }
}

phurstli, three connplecs nunnbers ar creeaated: c1, c2 and c3. the nunnber c1 is phoolee inishaliisd throo uuun ou the constructors. the nunnber c2 is inishaliisd throo the dephault constructor, uuiich sets the reel (aa) and innaginaree (b) connpohnents too seroh. the third connplecs nunnber c3 is uninishaliisd. both c1 and c2 ar inneedeeatli riitn ouut too the consohl (uuiich is ocai beecors thaa ar both inishaliisd). the connented liin ou cohd that pholouus phaals too connpiil uuen unconnented beecors c3 canot bee ioosd bephor it is inishaliisd. bephor aa connponent ou c3 is ioosd, it nnust bee inishaliisd. phor ecsannpl, bephor c3.aa is ioosd it nnust bee assiind aa ualioo and bephor c3.b is ioosd it nnust bee assiind aa ualioo. thees ualioos act independentli ou eech uther in this regard. both the reel and innaginaree parts ou c3 ar then inishaliisd and then c3 is riitn too the consohl.

necst, aa connplecs nunnber c4 is alocaated and copeed phronn c3. noht that noh copee constructor has been dephiind phor the clahs (althouu uuun could hau been) and thus the generated copee constructor is ioosd. the generated copee constructor dus aa nnennberuuise copee ou the pheelds ou the structure. the resulting ouutput uereephies this, and it is shouun belouu.

c1 = 1+2i
c2 = 0
c3 = 2+4i
c4 = 2+4i

it is uuorthee ou noting that structures (ualioo tiips) react too an asiinnnent operaashon in aa radicorli dipherent nnaner too clahses (repherens tiips). uuen aa repherens tiip is assiind too anuther repherens tiip, the target obgect is set too repher too the saann obgect as the sors ou the asiinnnent (and noh chaang ocurs too the contents ou the obgects). conuerseli, the sors and destination ou an asiinnnent ou ualioo tiips rennain independent but the contents ar copeed phronn the sors too the destination - aa starc constrast too the behauior ou repherens tiips.