dotnet / dotnet/csharplang

[Proposal]: Ref and ref-like parameters of async methods

Open
#10,187 0 comments 0 reactions 1 assignee Claimed by @jnm2 View on GitHub
Proposal champion
Dominant language
C#
Stars
12.7k
Forks
1.1k
Avg merge
11h 1m
Merged PRs (30d)
3

Description

# Ref and ref-like parameters of async methods

Specification: [proposals/async-method-ref-parameters.md](https://github.com/dotnet/csharplang/blob/main/proposals/async-method-ref-parameters.md)
Discussion: https://github.com/dotnet/csharplang/discussions/9997

## Summary

This is a follow-up to the C# 13 feature [Allow ref and unsafe in iterators and async](https://github.com/dotnet/csharplang/tree/main/proposals/csharp-13.0/ref-unsafe-in-iterators-async.md), extending the relaxation of `ref` and `ref struct` variables in `async` methods to method _parameters_ as well, with the same overall semantics. Under this proposal, `ref`/`in`/`out` parameters or parameters of `ref`-like types shall be usable in `async` methods, including local functions and anonymous functions. They shall have the same behavior as locals: they are available up to an `await` boundary, and require reassignment afterwards to be usable again.

```cs
public async ValueTask ReceiveMessageAsync(in Message message)
{
var (sender, recipient) = (message.Sender, message.Recipient);
await LogMessageAsync(sender, recipient);
// do something else with the variables
}
```

This proposal does _not_ cover iterator methods, since their code always executes lazily.

## Design meetings

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.