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.
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());
}
}