16 November

C# 11 – Improved method group

Programming

min. read

Reading Time: < 1 minute
C# 11
C# 11

Let’s list all the features that C# 11 brings to the table. Let’s discuss them all one by one, with their use cases, and see how they can come in handy. At the bottom of each article, you can find a link to all 14 new C# features!

Improved method group conversion to delegate

In C# 10 and before, for some bizarre reason, the code behind method groups was not converted to the same IL code as lambdas or anonymous delegates. The code behind method groups was slower, and not memory efficient.

static readonly List<int> Numbers = Enumberable.Range(0, 100).ToList();

public int Sum()
{
    return Numbers.Where(x => Filter(x)).Sum(); // <- faster
}

public int SumMethodGroup()
{
    return Numbers.Where(Filter).Sum(); // <- slower
}

static bool Filter(int number)
{
    return number > 50;
}

In C# 11, this was fixed and now both versions would be memory and CPU efficient as they should be!

Further reading at dotnet GitHub page.

Current article:
Improved method group


Author

Tomasz Juszczak

CTO /

Technical Lead

Tomasz Juszczak

About prog

Founded in 2016 in Warsaw, Poland. Prographers mission is to help the world put the sofware to work in new ways, through the delivery of custom tailored 3D and web applications to match the needs of the customers.


SIMILAR POSTS


C# 11 – Numeric IntPtr

Programming

min. read



Let's talk

SEND THE EMAIL

I agree that my data in this form will be sent to [email protected] and will be read by human beings. We will answer you as soon as possible. If you sent this form by mistake or want to remove your data, you can let us know by sending an email to [email protected] We will never send you any spam or share your data with third parties.