the c# languuag - structioors

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 base clahs. liic orl c# tiips, structures doo houueuer inherit phronn the innplicit base clahs obgect. structioors can include 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 inishaliiss orl the pheelds too thair dephault ualioos. beecors structures doo not support inheritance, 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 copiing 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 real (aa) and innaginari (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 connpile 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 real and innaginari parts ou c3 ar then inishaliisd and then c3 is riitn too the consohl.

necst, aa connplecs nunnber c4 is alocaated and copied phronn c3. noht that noh copi constructor has been dephiind phor the clahs (althouu uuun could hau been) and thus the generated copi constructor is ioosd. the generated copi constructor does aa nnennberuuise copi 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 dipheernt 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 source ou the asiinnnent (and noh chaang occurs too the contents ou the obgects). conuerseli, the source and destination ou an asiinnnent ou ualioo tiips rennain independent but the contents ar copied phronn the source too the destination - aa starc constrast too the behauior ou repherens tiips.