Skip to content

[Version 11.0] Feature support for required members#1597

Draft
RexJaeschke wants to merge 14 commits into
draft-v11from
v11-required-members
Draft

[Version 11.0] Feature support for required members#1597
RexJaeschke wants to merge 14 commits into
draft-v11from
v11-required-members

Conversation

@RexJaeschke

Copy link
Copy Markdown
Contributor

This is Rex's adaptation of the corresponding MS proposal.

Open Issue: Combining required with ref or ref readonly

The MS spec states that,

required cannot be combined with the following modifiers: …, ref, ref readonly, …

I have confirmed that while this is true for properties, it is not true for fields. The following compiles:

public ref struct S2
{
    [SetsRequiredMembers]
    public S2() { }
    public static int x = 555;
 
    public          ref int r1a = ref x;
    public required ref int r1b = ref x;
    public          ref readonly int r2a = ref x;
    public required ref readonly int r2b = ref x;
}

    var s2 = new S2();
    Console.WriteLine($"S2.x   = {S2.x}");
    Console.WriteLine($"s2.r1a = {s2.r1a}");
    Console.WriteLine($"s2.r1b = {s2.r1b}");
    Console.WriteLine($"s2.r2a = {s2.r2a}");
    Console.WriteLine($"s2.r2b = {s2.r2b}");

Is the MS spec wrong/out-of-date, or am I missing something?

Bill Wagner stated: I don’t see any notes in LDM meetings. But, I think the spec was a bit vague, and was originally meant to focus on properties. I’m hoping Fred can shed more light on it.

Fred Silberberg stated: Required members supports both fields and properties, but I'm not sure whether we specifically considered the intersection given the late-breaking nature of the way ref fields were developed. Likely we should block this.

@RexJaeschke RexJaeschke added this to the C# 11 milestone Mar 7, 2026
@RexJaeschke RexJaeschke added type: feature This issue describes a new feature Review: pending Proposal is available for review labels Mar 7, 2026
@RexJaeschke RexJaeschke marked this pull request as draft March 7, 2026 16:13
@BillWagner BillWagner force-pushed the v11-required-members branch from cc1f70b to 2ede9c1 Compare April 29, 2026 19:41
@BillWagner BillWagner force-pushed the v11-required-members branch from 3c6b27a to 73c9ad0 Compare May 13, 2026 16:36
@BillWagner BillWagner force-pushed the v11-required-members branch from 73c9ad0 to 42ef70d Compare June 24, 2026 20:16
@BillWagner

Copy link
Copy Markdown
Member

An earlier version of this feature is already present on v11-alpha from a prior meeting. Edits made to this PR since then are not yet on v11-alpha; they will land at the next propagation. If you need them on v11-alpha sooner, please open a separate PR targeting v11-alpha.
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Review: pending Proposal is available for review type: feature This issue describes a new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants