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; }

}
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Reply