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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/csharp/language-reference/operators/delegate-operator.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ A static anonymous method can't capture local variables or instance state from e

You also use the `delegate` keyword to declare a [delegate type](../builtin-types/reference-types.md#the-delegate-type).

Beginning with C# 11, the compiler may cache the delegate object created from a method group. Consider the following method:

```csharp
static void StaticFunction() { }
```

When you assign the method group to a delegate, the compiler will cache the delegate:

```csharp
Action a = StaticFunction;
```

Before C# 11, you'd need to use a lambda expression to reuse a single delegate object:

```csharp
Action a = () = StaticFunction;
```

## C# language specification

For more information, see the [Anonymous function expressions](~/_csharpstandard/standard/expressions.md#1116-anonymous-function-expressions) section of the [C# language specification](~/_csharpstandard/standard/README.md).
Expand Down
9 changes: 9 additions & 0 deletions docs/csharp/whats-new/csharp-11.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ The following features are available in the 6.0.200 version of the .NET SDK. The
- [static abstract members in interfaces](#static-abstract-members-in-interfaces).
- [Newlines in string interpolation expressions](#newlines-in-string-interpolations).
- [Simplified parameter null checks](#simplified-parameter-null-checks).
- [Improved method group conversion to delegate](#improved-method-group-conversion-to-delegate)

You can download the latest .NET 6 SDK from the [.NET downloads page](https://dotnet.microsoft.com/download). You can also download [Visual Studio 2022](https://visualstudio.microsoft.com/vs/), which includes the .NET 6 SDK.
You can also try all these features with the preview release of the .NET 7 SDK, which can be downloaded from the [all .NET downloads](https://dotnet.microsoft.com/download/dotnet) page.
Expand Down Expand Up @@ -119,3 +120,11 @@ void Method(string name)
```

This feature provides a concise syntax for runtime null parameter checking. It's intended for library authors to provide runtime checks even when APIs have been annotated for nullable reference types. These checks can simplify the necessary validation. You can learn more in the language reference article on [null parameter checks](../language-reference/operators/null-parameter-check.md).

## Improved method group conversion to delegate

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.
Comment on lines +126 to +130

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.