Say It With a Single Word
Expression Bodied Members and Default Expressions, Hand in Hand
Immediately after implementing the C# 7.1 default expressions in Sharpen I ran them on the NHibernate code. And here is one of my absolutely favorite findings.
The EmptyMapClass<TKey, TValue>
class contains the following indexer:
public TValue this[TKey key]
{
get { return default(TValue); }
set { /* Omitted for brevity. */ }
}
After applying both the C# 7.0 expression bodied members for getters and the C# 7.1 default expressions the getter strips down to a single word only!
public TValue this[TKey key]
{
get => default;
set { /* Omitted for brevity. */ }
}
Why do I like this example so much? For me, it demonstrates, almost symbolically, the elegance and expressiveness of the modern C#. We have much less to type and to read. Sometimes, just a single word. It is up to the compiler to figure out the “missing parts”.
What do you think of this small real-life example found by the help of Sharpen? Are you thrilled about it the same way I am, or you see it as a hard-to-digest syntax sugar? ;-) The comments are below, waiting for your opinion :-)
Improve this page