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;
}

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...