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

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

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