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

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...