Expand To Show Full Article
C# Error CS9050 - A ref field cannot refer to a ref struct. - Developer Publish

C# Error CS9050 – A ref field cannot refer to a ref struct.

C# Error

CS9050 – A ref field cannot refer to a ref struct.

Reason for the Error & Solution

A ref field cannot refer to a ref struct.

The compiler does not support the ref modifier on a field within a struct (to declare a stack-allocated field) of a type already declared stack-allocated (ref struct).

Example

The following sample generates CS9050:

// CS9050.cs (0,0)
ref struct Color
{
    public float r, g, b;
}
ref struct Group
{
    public ref Color color;
}

To correct this error

In this example, it is most likely a typo to have included a ref modifier on a field of a ref struct type within the declaration of a ref struct. Removing the ref modifier corrects this error.

ref struct Color
{
    public float r, g, b;
}
ref struct Group
{
    public Color color;
}