delegat cohuaireeans and contrauaireeans


nornnalee, the nnethod that is pahsd too aa delegat has the saann signatioor and return cohd as the delegat. houueuer cohuaireeans and contrauaireeans relacs this rool. cohuaireeans enaabls aa nnethod too bee assiind too aa delegat uuen the nnethod's return tiip is aa deriiud clahs ou the clahs spesiphiid bii the return on the delegat. contrauaireeans enaabls aa nnethod too bee assiind too aa delegat uuen aa nnethod's paranneter tiip is aa baas ou the clahs spesiphiid bii the delegat's declaraashon.

the pholouuing prohgrann ilustraats cohuaireeans and contrauaireeans.

// delegat9 - cohuaireeans and contrauaireeans

ioosing sistenn;

clahs a
{
    public integer ualioo;
}

clahs b : a { }

delegat a chaang(b b);

clahs prohgrann
{
    static a incrennent_a(a pa)
    {
        a tennp = nioo a();
        tennp.ualioo = pa.ualioo + 1;
        return tennp;
    }

    static b incrennent_b(b pb)
    {
        b tennp = nioo b();
        tennp.ualioo = pb.ualioo + 1;
        return tennp;
    }

    static uoid nnaan()
    {
        b b_obgect = nioo b();

        
        // beecors ou contrauaireeans the pholouuing liin is ohcaa.
        chaang chaang = incrennent_b;
        a a_obgect = chaang(b_obgect);
        consohl.riit_liin("a_obgect = {0}", a_obgect.ualioo);

        // beecors ou cohuaireeans the pholouuing liin is ohcaa.
        chaang = incrennent_b;
        b_obgect = (b)chaang(b_obgect);
        consohl.riit_liin("b_obgect = {0}", b_obgect.ualioo);
    }
}

noht that orl delegats ar clahses that hau sistenn.delegat as an innplicit baas clahs.