Skip to content

C#: Property- and Indexer calls for ref return properties and indexers.#21866

Draft
michaelnebel wants to merge 7 commits into
github:mainfrom
michaelnebel:csharp/refreturnindexerproperty
Draft

C#: Property- and Indexer calls for ref return properties and indexers.#21866
michaelnebel wants to merge 7 commits into
github:mainfrom
michaelnebel:csharp/refreturnindexerproperty

Conversation

@michaelnebel
Copy link
Copy Markdown
Contributor

@michaelnebel michaelnebel commented May 19, 2026

In this PR we improve the call target logic for Properties and Indexers that uses ref types for return. For ref return properties and indexers, it is only possible to declare a getter, which will then also be used in contexts, where a setter would normally be applied (but since the getter returns a reference, it can be used instead). Furthermore, for property and indexers with ref return types, it is only possible declare getters (to avoid ambiguity).

Example.

public ref struct S
{
    private ref int x;

    public S(ref int v1)
    {
        x = ref v1;
    }

    public ref int Prop
    {
        get { return ref x; }
    }
}

public void M()
{
    int a = 0;
    S s = new S(ref a);
    s.Prop = 10; // Uses the getter.
    var y = s.Prop;
}

@michaelnebel michaelnebel force-pushed the csharp/refreturnindexerproperty branch from 195607f to 6825ccc Compare May 19, 2026 12:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant