Skip to content

Possible bug with inferred delegate types #7514

Answered by HaloFour
MVey asked this question in Q&A
Discussion options

You must be logged in to vote

This is expected.

Inferred delegate types will re-use any existing Action<...>/Func<....> delegate if the signature matches. Otherwise, C# will create its own delegate type. It's impossible to infer the natural delegate type for a lambda otherwise as delegates are nominally typed and do not have signature equivalence.

        // fine
        Predicate<int> pred1 = (int x) => x % 2 == 0;
        
        // not fine, pred is Func<int, bool>
        var pred = (int x) => x % 2 == 0;
        // error CS0029: Cannot implicitly convert type 'System.Func<int, bool>' to 'System.Predicate<int>'
        Predicate<int> pred2 = pred;

See: https://github.com/dotnet/csharplang/blob/main/proposals/csha…

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
4 replies
@MVey
Comment options

@CyrusNajmabadi
Comment options

@bernd5
Comment options

@333fred
Comment options

Answer selected by MVey
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
6 participants