C# Error CS8166 – Cannot return a parameter by reference ‘{0}’ because it is not a ref parameter

C# Error

CS8166 – Cannot return a parameter by reference ‘{0}’ because it is not a ref parameter

Reason for the Error & Solution

Cannot return a parameter by reference because it is not a ref parameter

Example

The following sample generates CS8166:

// CS8166.cs (11,20)

public class Test
{
    public struct S1
    {
        public char x;
    }

    ref char Test1(char arg1, S1 arg2)
    {
        return ref arg1;
    }
}

To correct this error

To return a parameter that is not passed by reference, refactoring to use return by value will correct this error:

public class Test
{
    public struct S1
    {
        public char x;
    }

    char Test1(char arg1, S1 arg2)
    {
        return arg1;
    }
}

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