A property is a member that provides a flexible tool to read and write the value of a private field. We use them as public data members but actually, they are specific methods called accessors.

In this article, we are going to talk more about properties and how to use them in C#.

If you want to see complete navigation of this tutorial, you can do that here C# Intermediate Tutorial.

To download the source code, you can visit Properties in C# Source Code. 

We are going to divide this article into the following sections:

Property Syntax

The syntax of a property declaration can be used in the following way:

As we can see, a property can contain two blocks of code. The get block contains statements that execute when we read from a property. The set block contains statements that execute when we write to a property:

In the example above we see that our private fields are now exposed through the properties. If we want to read the value of the _name field all we have to do is to call the Name property with the student object. The same applies to the _lastName field. Moreover, if we want to set a value to our fields, all we have to do is to call a set block of our properties:

Our properties can have a complex code inside get or set blocks. They are not limited only to read a value or just to write a value. We can use conditions or method calls etc. in the get or set blocks:

Read-Only and Write-Only Properties

We can declare a property that only has a get block and not the set. That kind of property is called Read-Only property. If we create a read-only property, we can only read the value of a private field. It is quite common to create a read-only property inside our class. What we want with it is to set it with the constructor method and then to use its value throughout the entire class, but never to set its value outside the constructor. If we try to set it, the compiler will throw an error:

Read only property error - Properties in C#

In the same way, as we can create a read-only property, we can create a write-only property. That type of property has only the set block and not the get. It is not a common case to create write-only properties. Of course, if we need it, we can only set the values with this type of property and not read it:

Write only property error - Properties in C#

Property Accessibility

We can specify an access modifier for our property (public, private…) if we want to restrict its availability. But in C# we can even override the accessibility of get or set accessors. So, what we can do is declare a public property which has the public get accessor and private set accessor. If our property is a public one, we don’t have to add the public keyword for the get accessor, it is going to be public anyway:

Privete set accessor - Properties in C#

This means that we can read in all the classes from our Name property, but we can set it only within the Student class.

When we use an accessor overriding inside the property, we must pay attention to the following rules:

  • We can change the accessibility level of only one accessor. There is no point in having both accessors modified. If we want to modify both accessors, we should just modify the property access level.
  • We can’t use access modifier on the get or set blocks that are less restrictive of the access modifier applied on a property itself. So, if our property is private, there is no point in having the public get or set block.

Auto-Implemented Properties

If no additional logic is required in a property accessor, we can use the auto-implemented properties for more readable and concise way of declaring properties. The auto-implemented property consists only of the get and set keywords, nothing more:

When we declare the properties like this, the compiler creates a private field for us, which could be accessed only through the property’s get or set accessors.

So in our example instead of:

We can just write:

In the Visual Studio, we are even going to get a suggestion to use an auto property:

Property suggestion

Conclusion

Excellent.

In this article, we have learned:

  • About properties and it’s syntax
  • How to use read and write only properties
  • How to modify the accessibility level of the property
  • The way to use auto-implemented properties

In the next article, we are going to talk about Static methods, static classes, and extension methods as well.

If you have enjoyed reading this article and if you would like to receive the notifications about the freshly published .NET Core content we encourage you to subscribe to our blog.