HomeCSharpC# 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

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

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