Skip to content

[Version 11.0] Feature support for required members#1597

Draft
RexJaeschke wants to merge 13 commits intodraft-v11from
v11-required-members
Draft

[Version 11.0] Feature support for required members#1597
RexJaeschke wants to merge 13 commits intodraft-v11from
v11-required-members

Conversation

@RexJaeschke
Copy link
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
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.

1 participant