C# Expression Bodied Syntax

Introduction

C# concentrate to improve its syntax in order to optimize its’ code readability and make it concise. Expression-bodied style feature has been added in C#6 and improved in C#7. Expression-bodied style makes all properties constructor, destructor, indexers and methods declarations simplicity. Developer can clear express their design intention and reduce lines of code to enhance code readability. In this article, I define a Tank class to explain the idea of expression-bodied style. After you implement the tank class, you understand the practical usage.



A Tank

A Tank is an armored fighting vehicle and is very common appearing in strategic war game. A tank consist two main attributes. They are model and weapons. User can switch any weapon before attack the enemy. I design this tank can only consist 3 weapons. I ignore the equipment consuming in this demonstration.


Traditional Definition

In traditional syntax, the tank is defined as follows.

I create a private variable called _weaponIdx to save which weapons the tank is using currently. And this variable is seted by weaponIdx property accessor. There are two functions in this class. They are attack function and currentWeapon function.

CurrentWeapon is for checking which weapon is the current weapon and attack is for attacking the enemy. The total lines of code are 43 in traditional class definition syntax.

Finally, I invoke the tank class in my console programme. The output shows as follow.

So, you know the overall class definition in traditional syntax now. Let’s study the expression bodied expression format with the same example.

Expression Bodied Definition.

Expression bodied gives you the power to reduce lines of code and make them more concise. The definition of expression bodied usually composed in the following syntax format.

Member => expression;

The following members support expression bodied syntax.

  1. Method
  2. Properties
  3. Constructor
  4. Destructor
  5. Getters
  6. Setters
  7. Indexers

In the tank example, I show you above expression bodied syntax  except indexer step by step.  I create a class called TankExpressionBody to show you the expression bodied syntax independently so that you can compare it with the traditional syntax of Tank class finally.



Constructor

An expression bodied constructor consist a single line code to handle the initialization process. In this example, the constructor has a single parameter named model, so the expression body constructor show as follow.

Destructor

Same as the body constructor, the expression bodied destructor consists a single line code too. The purpose of the destructor is releasing the unmanaged resources.

Property

I implement a property accessor for mainWeapon property in tank task. The following is the expression bodied syntax version of mainWeapon.

It allows user get the current weapon through this property. The property(set) accessor also support expression bodied syntax. For the weaponIdx property Accessor, it accesses the value of _weaponIdx variable.

Method

There are two functions in the tank class. The return type of them is string. Expression body return a value whose return type matches the return type of method. Therefore, they return string. Their expression body version show as follows.

Conclusion

Compared with the tradition syntax, expression bodied syntax remove the curly bucket and also the return keyword in property and method. In constructor and destructor, It only removes the curly bucket. But all of this little improvement can reduce lot of lines of code in enterprise project and enhance the code readability. However, expression body can only support single line expression. It means that a block of code does not support. Therefore, the return type and curly bucket is still essential. And expression bodied syntax does not support logical operation such as for, if, while and switch syntax. But for If else statement, we can use ternary operator to replace it. For instance, the following syntax can be work in compile time and runtime.

But once there are too many conditions. The code will become hard to maintain. Therefore, it should avoid too many ternary operator in expression bodied syntax.

Finally, The total lines of code reduce to 26 if we use expression bodied syntax to define a class.  It almost cut half of original lines of code.

For better understand this syntax, please check out the example here: https://github.com/Isaac234517/ExpressionBodiedSyntaxInC-

Get the latest article from lab.

Please leave your email below.

Subscribe

Thank you for you subscription