Using throw-expressions in C#

gpeipman
3,705 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

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.

Click Run to see demo

References

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content