I want to create a delegate pointing to a static function…the static function has a second parameter of type Flight, the Flight class implements ITravelObject interface and the second delegate parameter requires this interface.
The following code doesn’t work. Why is that? Is there any other possible implementation?
namespace CovarianceContravarianceTest
{
public class Data {}
public interface ITravelObject
{
int ID { get; set; }
}
public abstract class BaseObject {}
public class Flight : BaseObject, ITravelObject
{
int ITravelObject.ID {get;set;}
}
public static class LetService
{
public static void DoSomething(Data da, Flight let) {}
}
public delegate void TravelServiceMethodDelegate(Data dataAccess,ITravelObject travelObject);
class Program
{
static void Main(string[] args)
{
TravelServiceMethodDelegate test = new TravelServiceMethodDelegate(LetService.DoSomething);
}
}
}
This results in error
No overload for ‘DoSomething’ matches delegate ‘CovarianceContravarianceTest.TravelServiceMethodDelegate’
thanks everyone for answers, I’ve already figured out this isn’t even a covariance/contravariance issue
You are trying to plug this method
into this delegate
This cannot work. Why? Consider the following code:
This compiles, because
CarRideimplementsITravelObject.However, if I call
GoForACarRide(test), it will break, sincetestpoints toDoSomething, which expects aFlight.