Expression-Bodied Properties
What if we could change our concatenated property syntax from this to this?
That's the purpose of the Expression-Bodied Properties that are new in C#6.
They provide a syntax shortcut for
read-only properties that immediately return a value.
Let's look a little closer at the syntax here.
Notice that there are no curly braces, no get keyword and no return statement.
Just a => some people call this "fat arrow syntax" but technically its called a Lambda operator.
Here some few example.
- FullName property that is the concatenation of the first and the last names.
- ItemTotal that calculates the total from the Quantity and price.
- VendorName, which exposes the ProductVendor's CompanyName.
Each of these takes up one line of code instead of the original three.
Comments
Post a Comment