Automatically implemented properties must define both get and set accessors in C#

When using the automatically implemented properties in C#, we cannot have the property with just a get.

When attempting to have just a getter property, one will get error similar to the one show below.

Automatically implemented properties must define both get and set accessors in C#

Error    1    ‘GinktageConsoleApps.Employee.Designation.get’ must declare a body because it is not marked abstract or extern. Automatically implemented properties must define both get and set accessors.    D:\Sample Programs\GinktageConsoleApps\GinktageConsoleApps\Program.cs    19    37    GinktageConsoleApps

In this case, the auto-implemented property can have a private set as shown below.

public class Employee

{

public string Name { get; set; }

public string Designation { get; private set; }

}

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