Skip to content

Combine of covarince/contravariance delegates #3711

Open
@iskiselev

Description

Currently C# allow combine (using +/- operators) delegates with different compatible (covarince/contravariance) types, but .Net Delegate.Combine/Delegate.Remove doesn't allow it. So, this sample legal C# code (that will be compiled even without warnings) will result in run-time exception:

using System;
internal class Program
{

    private static void Main(string[] args)
    {
        Func<string, object> a1 = SomeFunction;
        Func<object, object> b1 = SomeFunction;
        var c1 = a1 + b1;

        Func<object, string> a2 = SomeFunction;
        Func<object, object> b2 = SomeFunction;
        var c2 = a2 - b2;
    }

    public static string SomeFunction(object input)
    {
        return string.Empty;
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Type

No type

Projects

  • Status

    Requires runtime or BCL work

Relationships

None yet

Development

No branches or pull requests

Issue actions