Using throw-expressions in C#
gpeipman
6,902 views
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.
References
- C# 7.0: Throw expressions (Gunnar Peipman)
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.