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

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