HomeCSharpC# Error CS8173 – The expression must be of type ‘{0}’ because it is being assigned by reference

C# Error CS8173 – The expression must be of type ‘{0}’ because it is being assigned by reference

C# Error

CS8173 – The expression must be of type ‘{0}’ because it is being assigned by reference

Reason for the Error & Solution

The expression must be of type because it is being assigned by reference

When assigning a reference to a variable, the type of the variables must match to be referenceable.

Example

The following sample generates CS8173:

// CS8173.cs (12,18)

class C
{
    void M()
    {
        string s = "s";
        object o = s;
        ref string rs = ref s;
        ref object ro = ref o;

        ro = ref s;
    }
}

To correct this error

Assigning the reference to a variable of the correct type will correct this error:

        rs = ref s;

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...