C# Error CS8149 – By-reference returns may only be used in methods that return by reference

C# Error

CS8149 – By-reference returns may only be used in methods that return by reference

Reason for the Error & Solution

By-reference returns may only be used in methods that return by reference

Example

The following sample generates CS8149:

// CS8149.cs (9,33)

delegate int E();

class C
{
    static int i;
    static void M()
    {
        var e = new E(() => ref i);
    }
}

To correct this error

Ensuring that methods and delegates declared as a returning by value do not return by reference corrects this error:

delegate int E();

class C
{
    static int i;
    static void M()
    {
        var e = new E(() => i);
    }
}

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