62
28

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[C#] もはや我々はnew ArgumentNullException(...)する必要はない

Last updated at Posted at 2023-05-10

null許容参照型が導入された現在でも、引数のnullチェックに事欠かくことのない皆様、いかがお過ごしでしょうか。
今日もnew ArgumentNullException(...)していますか。

しかし、もうこんなことをする必要はないんです。
.NET6以降の世界には ArgumentNullException.ThrowIfNull(object? argument, string? paramName = default) が存在します。

これによって我々は、

public void AMethod(string str)
{
    if (str is null)
        throw new ArgumentNullException(nameof(str));
    ...
}

の替わりに

public void AMethod(string str)
{
    ArgumentNullException.ThrowIfNull(str);
    ...
}

と書くことができます。

ほんの少しだけハッピーになれますね。

62
28
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
62
28

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?