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


x

Interesting BlogPosts to read

How to get an Enumerator from range of elements from ArrayList in C# ?
The code snippet shown in this blog post demonstrates how you can get the enumeraor from a range of ...
How to check for the column name in an SqlDataReader Object in C# ?
When working in ADO.NET for data access , there are times when you want to check to see if a column ...
How to convert Array to List in C#?
Want to convert array of strings to List of strings in C# ? . Below is a sample code to illustrate t...