Skip to content

Method group natural type improvements C# 13.0readabilitycorrectness

Method groups are now more likely to identify the correct method.

Method groups are sets of methods with the same name that the compiler must reduce to a callable delegate type.

Prior to C# 13, natural type resolution could be blocked by overloads that were not actually applicable, such as methods with the wrong generic arity or unsatisfied constraints.

C# 13 improves this by pruning inapplicable overloads earlier so the compiler is more likely to identify a natural type without additional casts.

Code

csharp
// Generic arity is considered before determining the natural type.
var parse = NumberParser.Parse<int>; // Func<string, int>

static class NumberParser
{
    public static int Parse(string text) => int.Parse(text);
    public static T Parse<T>(string text) where T : IParsable<T>
        => T.Parse(text, null);
}
csharp
// Required an explicit delegate type because the method group
// did not get a natural type.
Func<string, int> parse = NumberParser.Parse<int>;

static class NumberParser
{
    public static int Parse(string text) => int.Parse(text);
    public static T Parse<T>(string text) where T : IParsable<T>
        => T.Parse(text, null);
}

More information