Resolved Contravariance Not Working As Tutorial

Dalski

Active member
Joined
Jun 3, 2020
Messages
40
Programming Experience
Beginner
I cannot seem to get the Zebra class to execute it's method.

Though the delegate is being passed as a Zebra delegate, contravariantly it's being received in the MyMammalMethod as the base class Mammal.

I can't see what I haven't copied exactly in the tutorial, apart from the fact that I renamed the delegate identifier; which is irrelevant. at 7:30, Peter executes the Display() method in the derived Zebra class.

C#:
using System;
using System.Collections;
using System.Collections.Generic;

//  Generic delegate:
delegate void DalsContravariantDelegate<in T>(T a);

class Program
{
    public static void Main()
    {
        Zebra z = new Zebra();
        Mammal m = new Mammal();

        Console.WriteLine("\nContravariance with a delegate:");
        //  Declare generic delegate "mammal_method" of DalsContravariantDelegate type which is generically declared as mammal type.
        DalsContravariantDelegate<Mammal> mammal_method = MyMammalMethod;
        //  Declare generic delegate zebra_method & ultimately bind it to the MyMammalMethod.
        DalsContravariantDelegate<Zebra> zebra_method = mammal_method;
        zebra_method(z);
    }

    //  Signature matches the obj call:
    static void MyMammalMethod(Mammal mammal)
    {
        mammal.Display();
    }
}


class Mammal
{
    virtual public void Display()
    {
        Console.WriteLine("Mammal: " + GetHashCode());
    }
}

class Zebra : Mammal
{
    public void Display()
    {
        Console.WriteLine("Zebra: " + GetHashCode());
    }
}
 
VS give this warning:
Warning CS0114 'Zebra.Display()' hides inherited member 'Mammal.Display()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.
 
Being a fan of explicit code writing, I'd prefer if you took to the time to add keywords as well as access modifiers. I also prefer public virtual void over virtual public void for readability.

Re the warning... You can override or call new but it would be helpful to include information on this for those reading it.
 
This is why I tell people not to follow Youtube tutorials. They are mostly all dirt, bugged, badly explained or written, and almost always never work.

Give me a library and a book any-day.
 
Thank you both. I'm really surprised that override is needed as it kind of dilutes the point of contravariance I think.
I appreciate your feedback on semantics Sheepings. This is probably the 10th tutorial I've watched on covariance & contravariance. I'm just learning is all. There are not many good written tutorials on this subject. Yes you'll find many written tutorials which I've already done, but when you're a beginner it's hard to get your head around it. This video, he shouldn't have digressed on a lambda expression. He should've stayed focused on the topic. But it is the best tutorial on this subject.
 
I'm really surprised that override is needed as it kind of dilutes the point of contravariance I think.
Not at all. If you want to override a method then you have to override it. It's that simple. Otherwise they are not the same method but different methods with the same name. That's got nothing to do with contravariance. Contravariance is about accepting a delegate that has parameters that are base types of the types expected. The fact that the code compiles and runs proves that that is happening so 100% contravariance there. Your issue is polymorphism or, at least, that you're not implementing it properly. You are passing a Zebra object and then using it via a Mammal reference and then calling the Display method. Why would you think that that the Zebra.Display method would be called on a Mammal reference without overriding when that is specifically what overriding is for?
 
Thanks jm, you've helped me a lot there. I'm following tutorialsteacher.com as the base of all my studies, but it falls well short compared to the likes of learncpp.com so I'm having to read up further on most topics as tutorialsteacher.com just scratches the surface.

Regarding the poor setup of the tutorial with confusing nesting/ polymorphism. I'm just following the Youtube tutorial, & it had got me very confused. Thinking about nesting classes (I know we can't nest indefinitely as in C++) but one can't help but wonder & what would be the ultimate deciding point. I won't go into that as I can guarantee I won't clearly be able to explain my thoughts.

In the first Youtube tutorial there was a strange occurance where strange methods were called, but thanks to your input above I'm looking into polymorphism now so won't trouble the forum here on that for the minute. Thank you all again for your input; much appreciated.
 
Regarding the poor setup of the tutorial with confusing nesting/ polymorphism. I'm just following the Youtube tutorial
That is why you should not be following tutorials on youtube. You will always run into a problem where something is poorly explained or not explained at all.
I'm following tutorialsteacher.com as the base of all my studies, but it falls well short compared to the likes of learncpp.com
You want to learn C#? Then start here : C# docs - get started, tutorials, reference. And read every single page on that documentation platform until you know most of it backwards. That's the only source i recommend as its never faltered me.
 
Back
Top Bottom