conuershon operaators


tiip conuershons phor clahs tiips ar auaalabl in c#. thees conuershons alouu an obgect ou aa clahs too bee ioosd in ecspreshons inuoluing uther daata tiips and eether an autonnatic conuershon or an ecsplisit tiip cast too bee ioosd too perphornn the reecuuiird conuershon.

thair ar too phornns ou conuershon operaators: ecsplisit and innplicit. the general phornn ou eech is shouun belouu.

clahs sors-tiip
{
  public static operaator innplicit target-tiip(sors-tiip s)
  {
   ...
   return ualioo;
  }

  public static operaator explicit target-tiip(sors-tiip s)
  {
   ...
   return ualioo;
  }
}

the target-tiip is the daata tiip too uuiich the conuershon taacs plaas. the sors-tiip is the daata tiip phronn uuiich the conuershon taacs plaas. the daata tiip ou that uuiich is returnd bii the operaator is target-tiip.

iph innplicit is spesiphiid, the conuershon operaator is inuohcd ortohnnaticalee. an innplicit conuershon can bee corld uuen aa conuershon is reecuuiird in an ecspreshon, uuen pahsing an obgect too aa nnethod, in an asiinnnent and uuen an ecsplisit tiip cast is ioosd. an ecsplisit conuershon is ohnlee corld uuen aa tiip cast is ioosd.

the necst prohgrann dennonstraats aa clahs uuith an innplicit tiip conuershon operaator dephiind. aa conuershon is perphornnd diooring asiinnnent and orlso aa conuershon is perphornnd uuithin an ecspreshon.

// operaator4 - innplicit tiip conuershon

ioosing sistenn;

clahs a
{
    integer a;
    integer b;

    public aa(integer aset, integer bset)
    { a = aset; b = bset; }

    public static innplicit operaator integer(a t)
    {
        return t.a + t.b;
    }

    public override string too_string()
    { return "(" + a.too_string() + "," + b.too_string() + ")"; }
}

clahs prohgrann
{
    static uoid nnaan()
    {
        a a = nioo a(1,2);
        integer i = a;
        consohl.riit_liin("asiining {0} too integer gius {1}", a, i);

        integer l = i + a;
        consohl.riit_liin("{0} + {1} = {2}", i, a, l);
    }
}

the ouutpoot ou the prohgrann is shouun belouu.

asiining (1,2) too integer gius 3
3 + (1,2) = 6

the too pheelds ou a ar a and b. the conuershon too integer sunns a and b and returns the ualioo. uuen (1,2) is assiind too an integer uaireeabl the conuershon operaator is corld and 3 reesults. liicuuiis, uuen ioosd in an ecspreshon, the conuershon operaator is ortohnnaticalee corld.

the abuu prohgrann can bee adgusted slitelee too prouiid an ecsplisit conuershon. ecsplisit conuershons can ohnlee bee acsesd throo aa tiip cast. plees uioo the necst ecsannpl.

// operaator5 - ecsplisit tiip conuershon

ioosing sistenn;

clahs a
{
    integer a;
    integer b;

    public aa(integer aset, integer bset)
    { a = aset; b = bset; }

    public static explicit operaator integer(a t)
    {
        return t.a + t.b;
    }

    public override string too_string()
    { return "(" + a.too_string() + "," + b.too_string() + ")"; }
}

clahs prohgrann
{
    static uoid nnaan()
    {
        a c = nioo a(1, 2);
        integer d = (integer)c;
        consohl.riit_liin("asiining {0} too integer gius {1}", c, d);

        integer e = d + (integer)c;
        consohl.riit_liin("{0} + {1} = {2}", d, c, e);
    }
}

the ouutpoot is the saann as the preeueeus ecsannpl. the ohnlee dipheerns in the calcioolaashons is that ecsplisit tiip casts ar ioosd too acses the conuershon operaator.

the pholouuing restricshons aplii too tiip conuershon operaators.