Skip to content

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

Description

@jnm2

Ref and ref-like parameters of async methods

Specification: proposals/async-method-ref-parameters.md
Discussion: #9997

Summary

This is a follow-up to the C# 13 feature Allow ref and unsafe in iterators and async, 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.

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions