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.

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