HomeCSharpC# Error CS0206 – A property or indexer may not be passed as an out or ref parameter

C# Error CS0206 – A property or indexer may not be passed as an out or ref parameter

C# Compiler Error

CS0206 – A property or indexer may not be passed as an out or ref parameter

Reason for the Error

You will get this error in your C# program when you try to pass a property as ref or out parameter.

For example, try to compile the below code snippet.

namespace DeveloperPubNamespace
{   
    class Program
    {
        public static int Id { get; set; }

        public void ProcessData(ref int id)
        {
            Id = 0;
        }
        static void Main()
        {
            ProcessData(ref Id);
        }
    }
}

This program will result with the error code CS0206 because Id is a Property in the class Program and we are trying to pass it as ref parameter to the function ProcessData.

Error CS0206 A property or indexer may not be passed as an out or ref parameter ConsoleApp3 C:\Users\Senthil\source\repos\ConsoleApp3\ConsoleApp3\Program.cs 13 Active

C# Error CS0206 – A property or indexer may not be passed as an out or ref parameter

Solution

In C#, a property is not allowed to be passed as ref or out parameter. You should not be passing property as ref/out parameter to avoid this error.

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