C# Feature - Throw expressions
Overview
Section titled “Overview”Throw expressions let throw appear where an expression is expected, which is useful for argument guards and concise members.
Introduced In
Section titled “Introduced In”C# 7.0 (2017)
Before
Section titled “Before”if (name == null){ throw new ArgumentNullException(nameof(name));}_name = name ?? throw new ArgumentNullException(nameof(name));Gotchas & Best Practices
Section titled “Gotchas & Best Practices”- Use throw expressions for short guard clauses, not complicated logic.
- They pair especially well with
??and expression-bodied members.