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

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