Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document new method group conversion #28952

Merged
merged 1 commit into from
Apr 13, 2022

Conversation

BillWagner
Copy link
Member

Fixes #28942

Beginning in C# 11, the compiler is allowed to cache the delegate created from a method group conversion. The compiler now does this.

Fixes dotnet#28942

Beginning in C# 11, the compiler is allowed to cache the delegate created from a method group conversion. The compiler now does this.
Copy link
Contributor

@CamSoper CamSoper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@BillWagner BillWagner merged commit 8935584 into dotnet:main Apr 13, 2022
@BillWagner BillWagner deleted the method-group-delegates branch April 13, 2022 16:04
Comment on lines +126 to +130
The C# standard on [Method group conversions](~/_csharpstandard/standard/conversions.md#108-method-group-conversions) now includes the following item:

> - The conversion is permitted (but not required) to use an existing delegate instance that already contains these references.

Previous versions of the standard prohibited the compiler from reusing the delegate object created for a method group conversion. The C# 11 compiler caches the delegate object created from a method group conversion and reuses that single delegate object. This feature is first available in Visual Studio 17.2 as a preview feature. It is first available in .NET 7 preview 2.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a bit misleading. The quoted item was already in the C# 5 standard that ECMA published in 2017. If the C# compiler in .NET 7 SDK were changed to cache and reuse delegate instances whenever the language version is C# 5 or higher, then that would be allowed by the standards, although I understand if Microsoft won't do that because of the compatibility risk.

The following would be more correct:

The C# compiler now optimizes as permitted by the following item in the C# standard on Method group conversions:

  • The conversion is permitted (but not required) to use an existing delegate instance that already contains these references.

Previous versions of the C# compiler did not reuse the delegate object created for a method group conversion, even though the C# 5 standard already allowed it. The C# 11 compiler caches the delegate object created from a method group conversion and reuses that single delegate object. This feature is first available in Visual Studio 17.2 as a preview feature. It is first available in .NET 7 preview 2.

@gewarren gewarren added the 🏁 Release: .NET 7 Work items for the .NET 7 release label Oct 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[17.2] Allow cached delegate for method groups
5 participants