C# Expression-bodied members

C# expression bodied members allows us to define members (property or method) definition in a single expression.

This expression is very concise and readable in nature.

We can use expression body definition with the following.

  • Method
  • Property Get

C# Expression-bodied members syntax


C# Expression-bodied method

It consists of a single expression. If method has a return type, expression must return similar type. Let's see an example.

C# Expression-Bodied Method Example

Output

Peter John

C# Expression bodied Property Get

We can use expression body to implement get property. A single expression can be used to set a value for the property.

We should not use return statement in this property.

Let's see an example, that implements expression body in get property.

C# Expression-Bodied Get Property Example

Output

Javatpoint





Latest Courses