© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3w ago•
8 replies
lukepuke123

Set indexer inside a ref struct without assigning variable?

beginner
Hi, I'm trying to set up a situation where I have some instance of a type

where i can get and set directly on the index return where the index return getters and setters have different logic depending on what you set

int test = exampleInstance[5].MultByTwo;

and also
exampleInstance[5].MultByTwo = 4;

public SampleClass {
    private int[] m_sampleArray; // Assume has values

    public Accessor this[int index] => new(this, index);

    public ref struct Accessor {
        private readonly SampleClass m_reference;
        private readonly int m_index;

        public Accessor(SampleClass reference, int index) {
            m_reference = reference;
            m_inedx = index;
        }

        public int MultByTwo {
            get => (int)m_reference.m_sampleArray[m_index] * 2;
            set {
                m_reference.m_sampleArray[m_index] = (int)value * 2;
            }
        }

        public int DivByTwo {
            get => (int)m_reference.m_sampleArray[m_index] * 0.5f;
            set {
                m_reference.m_sampleArray[m_index] = (int)value * 0.5f;
            }
        }
    }
}
public SampleClass {
    private int[] m_sampleArray; // Assume has values

    public Accessor this[int index] => new(this, index);

    public ref struct Accessor {
        private readonly SampleClass m_reference;
        private readonly int m_index;

        public Accessor(SampleClass reference, int index) {
            m_reference = reference;
            m_inedx = index;
        }

        public int MultByTwo {
            get => (int)m_reference.m_sampleArray[m_index] * 2;
            set {
                m_reference.m_sampleArray[m_index] = (int)value * 2;
            }
        }

        public int DivByTwo {
            get => (int)m_reference.m_sampleArray[m_index] * 0.5f;
            set {
                m_reference.m_sampleArray[m_index] = (int)value * 0.5f;
            }
        }
    }
}


The issue I have is that the compiler wont let me set on an index return because Accessor is a struct (I think... maybe). The reason I have my index return a ref struct is because in my use case I need to be strict about the privileges of modification of the underlying SampleClass reference. As in, I can't have someone be storing an index reference for later and mutating it at a different time. Doesn't really make sense in the example I've provided but you get the idea.

With this set up, you can however go:

var accessor = sampleInstance[4];
accessor.MultByTwo = 4;

You just cant do it directly:

sampleInstance[4].MultByTwo = 4;

I'm just not happy with the ergonomics of this especially since this is going to be used by other people.

As a quick fix I've substitued out the getter setter fields in Acessor for GetMultByTwo() SetMultByTwo() etc.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

difference between `struct` and `ref struct`
C#CC# / help
12mo ago
Builder Pattern as Ref Struct
C#CC# / help
3y ago
with or without ref
C#CC# / help
3y ago