Expression bodied properties in C#

Today I would like to introduce you to an often disregarded feature of C# 6.0. As you might already know from the title, it will be about expression bodied properties.

Although, this revamped property initialization syntax has been around for 2 years, I see many developers still not making use of it.

Advantages of using expression bodied properties
Space consumption

I understand the competition for an additional kilobyte of memory is not really a thing nowadays, but when putting this feature in use, your source files will get smaller.

Readablity

Since the body of your property is much more minimalistic, others might find it easier to get around in you code.

Example time!

I think a perfect example to demonstrate the use of expression bodied properties is the implementation of read-only properties.

We defined a private field _version , which is set to a version string within the SetVersion()  method. At the bottom, we have a Version property, which has a getter to return the version string. Very simple code…but could be even simpler.

This old way of getting stuff done can be replaced with this revamped code snippet.

It doesn’t really matter which way you choose. But sometimes there is no need to define a setter method in the body of a property. In such situations, the new syntax might come in handy.