HomeCSharpC# Error CS8167 – Cannot return by reference a member of parameter ‘{0}’ because it is not a ref or out parameter

C# Error CS8167 – Cannot return by reference a member of parameter ‘{0}’ because it is not a ref or out parameter

C# Error

CS8167 – Cannot return by reference a member of parameter ‘{0}’ because it is not a ref or out parameter

Reason for the Error & Solution

Cannot return by reference a member of parameter because it is not a ref or out parameter

Example

The following sample generates CS8167:

// CS8167.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 declared with out or ref, refactoring to return by value corrects 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

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