C# 2.0 以降の新機能を一つづつ確認していきます。
以前に一度行ったのですが、公式ドキュメント再編でリンク切れしているところを見つけてしまったので。今ならもっと簡潔なサンプルが欠けるところもあるだろうし、せっかくなので今もう一度確認して行きます。
throw 式 (Throw expressions)
https://docs.microsoft.com/ja-jp/dotnet/csharp/language-reference/keywords/throw#the-throw-expression
throw を、式およびステートメントとして使用できる。
class MyClass { // コンストラクタ internal MyClass(string value) => _value = value ?? throw new ArgumentException(); // プロパティ private string _value; internal string Value { get => _value; set => _value = value ?? throw new ArgumentException(); } // メソッド internal string GetValue() => throw new ArgumentException(); internal void SetValue(string value) => _value = value ?? throw new ArgumentException(); }