C# Error CS0154 – The property or indexer ‘property’ cannot be used in this context because it lacks the get accessor

C# Compiler Error

CS0154 – The property or indexer ‘property’ cannot be used in this context because it lacks the get accessor

Reason for the Error

You will receive this error in C# when you are trying to access a property value that doesnot have the Getter implemented.

For example, try running the below code snippet.

namespace ConsoleApp2
{
    public class Employee
    {
        public int Id
        {
            set
            {

            }
        }
    }
    class Program
    {
        public static void Main()
        {
            Employee emp = new Employee();
            int id = emp.Id;

        }

    }
    
}

When you compile the above C# code, you’ll receive the error CS0154 because the property “Id” of the Employee class doesn’t have Getter which you are trying to access it in the Main function.

Error CS0154 The property or indexer ‘Employee.Id’ cannot be used in this context because it lacks the get accessor ConsoleApp2 C:\Users\Senthil\source\repos\ConsoleApp1\ConsoleApp2\Program.cs 18 Active

C# Error CS0154 – The property or indexer 'property' cannot be used in this context because it lacks the get accessor

Solution

To fix this error, don’t try to retrieve the value from the property that doesn’t have getter defined.