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

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