Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
Using throw-expressions in C# 7.0
C# 7.0 introduces throw expressions. We can add exception throwing to expression-bodied members, null-coalescing expressions and conditional expressions. Throw expressions are the way to tell compiler to throw exception under specific conditions like in expression bodied members or inline comparisons.
This example uses simple Person class to demonstrate different situations where throw-expressions can be used:
- auto-property initializer statement
- inline comparison using operator "?"
- expression-bodied member
Check the output of demo under code window after running it.
- C# 7.0: Throw expressions (Gunnar Peipman)