C# Error CS0548 – ‘property’ : property or indexer must have at least one accessor

C# Compiler Error

CS0548 – ‘property’ : property or indexer must have at least one accessor

Reason for the Error

You will get this error in your C# code when you define a property in a class without at least one accessor method (get or set).

For example, let’s compile the below C# program

using System;

namespace DeveloperPublishConsoleCore
{
    public class Employee
    {
        // Results in the Error Code CS0548
        public int Id { 
        } 
    }

    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("DeveloperPublish Hello World!");
        }
    }
}

You will receive the error code CS0548 because the class Employee contains a property Id which does not have either get or set accessor method.

Error CS0548 ‘Employee.Id’: property or indexer must have at least one accessor DeveloperPublishConsoleCore C:\Users\senth\source\repos\DeveloperPublishConsoleCore\DeveloperPublishConsoleCore\Program.cs 8 Active

C# Error CS0548 – 'property' : property or indexer must have at least one accessor

Solution

In C#, a property must have at least one accessor method (get or set). You can fix this error in your C# program by ensuring that the property that is causing this error has at least one accessor method.

using System;

namespace DeveloperPublishConsoleCore
{
    public class Employee
    {
        public int Id   
        {
            get
            {
                return 0;
            }
        }
    }

    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("DeveloperPublish Hello World!");
        }
    }
}