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!");
        }
    }
}

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