Expression-bodied members in C#

Expression-body members is one of those updates that no many developer known about. I have encountered many cases some developer first thought is “A property?”. Part of the reason many developer have yet to embrace expression-bodied members is the lack of principals such as SOLID or completely never heard of it.

Basically Expression-body members are one line of code. Using the good friend lambda =>

public string CustomerName => $"{FirstName} {LastName}";

As you can see in the sample above we can use Expression-body members as a read-only property. You can also can apply it to methods.

public Customer GetCustomers => UoW.GetAllCustomers();

That will work like a charm. Yes, Expression-bodies members works in both methods and read-only properties. You code also have something like this

public int Calc() => c == -1 ? 0 : 1;

I personally like this new update because save me lines of code that are wasted in a open close curly braces. { } . Now you know start using Expression-body members in C#.

Spread the word
  • Yum