2

I have a little question about arrays of struct in C#: lets say I have a struct Foo:

struct Foo
{
    public string S;
    public int X;
    ...
    ...
}

and I have an array of Foo: Foo[] arr = ...

In one method, I use arr[i] quite often, so I'd like to keep it in a local variable (the expression for i is also a little long):
var f = arr[i]

Now, my problem is that I know structs are value type, which means assignments like this cause a copy. The struct is a little big (7 strings and a bool), so I'd prefer to avoid copying in this case.

If I am not mistaken, the only way to access the struct's fields without copying the struct is to use the array directly: arr[i].S or arr[i].X, but this quickly becomes annoying to read. I'd really like to keep the array element in a local variable, but I don't want to waste performance by copying it into the variable.

Is there a way to make something like a reference variable (similar to C++) to avoid copying? If not, than I'm curious if it's something the compiler optimizes?

How should I deal with this element? Can I put it in a local variable without copying or do I have to access it through the array to avoid copying?

Thanks in advance.

1 Answer 1

7

You can do this in C# 7 and later using ref local variables:

using System;


public struct LargeStruct
{
    public string Text;
    public int Number;
}

class Test
{
    static void Main()
    {
        LargeStruct[] array = new LargeStruct[5];

        // elementRef isn't a copy of the array value -
        // it's really the variable in the array
        ref LargeStruct elementRef = ref array[2];
        elementRef.Text = "Hello";

        Console.WriteLine(array[2].Text); // Prints hello
    }
}

Of course, I'd normally recommend avoiding:

  • Large structs
  • Mutable structs
  • Public fields (although if it's mutable, doing that via public fields is probably best)

... but I acknowledge there are always exceptions.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I use this struct to store data in a Unity project. That's why I must use mutable public fields. At least, the struct is private, so I don't really care.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.