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

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