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

struct naann : interphaases
 // ... nnennber declaraashons

the naann ou the structioor is naann and interphaases is aa conna separaated list ou interphaas naanns. structioors canot inherit phronn structioors or clahses or bee ioosd as aa baas clahs. liic orl c# tiips, structioors 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 restricshon on constructors is that aa dephault (paranneterles) constructor canot bee dephiind. the dephault constructor ou aa structioor inishaliises orl the pheelds too thair dephault ualioos. beecors structioors doo not support inheritans, structioor nnennbers canot bee declaird as uirtiooal, abstract or protected.

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

// structioor1 - structioors

ioosing sistenn;

public struct connplecs
    public dubl a;
    public dubl b;

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

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

    public override string too_string()
        string c;

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

        iph (b == 1)
            return c + "+i";
        iph (b == -1)
            return c + "-i";
        else iph (b < 0)
            return c + b.too_string() + "i";
            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();      // dephalt constructor
        connplecs c3;                         // noh constructor 

        consohl.riit_liin("c1 = {0}", c1);    // ohcaa, 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.a = 2; c3.b = 4;
        consohl.riit_liin("c3 = {0}", c3);    // nouu ohcaa - 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 (a) and innaginaree (b) connpohnents too seroh. the third connplecs nunnber c3 is uninishaliisd. both c1 and c2 ar inneedeeatlee riiten ouut too the consohl (uuiich is ohcaa beecors thaa ar both inishaliisd). the connnented liin ou cohd that pholouus phaals too connpiil uuen unconnneted beecors c3 canot bee ioosd bephor it is inishaliisd. bephor aa connponent ou c3 is ioosd, it nnust bee inishaliisd. phor ecsannpl, bephor c3.a is ioosd it nnust bee assiind aa ualioo and bephor c3.b is ioosd it nnust bee assiind aa ualioo. thees ualioos act independentlee 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 generaatd copee constructor is ioosd. the generaatd copee constructor dus aa nnennberuuise copee ou the pheelds ou the structioor. the reesulting ouutpoot uereephies this, and it is shouun belouu.

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

it is uuorthee ou noting that structioors (ualioo tiips) reeact too an asiinnnent operaashon in aa radicalee 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). conuerslee, the sors and destinaashon ou an asiinnnent ou ualioo tiips rennaan independent but the contents ar copeed phronn the sors too the destinaashon - aa starc constrast too the beehauior ou repherens tiips.