0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C# nullだった場合 ?? null 合体演算子

Last updated at Posted at 2024-08-19

C#の??は「null 合体演算子」と呼ばれる演算子です。この演算子は、左側のオペランドがnullであるかどうかをチェックし、nullであれば右側のオペランドを返し、そうでなければ左側のオペランドを返します。

使用例:

string name = null;
string result = name ?? "デフォルトの名前";

このコードでは、nameがnullの場合、resultには「デフォルトの名前」という文字列が代入されます。nameがnullでない場合は、その値がresultに代入されます。

??演算子を使うことで、nullチェックを簡潔に記述でき、デフォルト値を設定する場合に便利です。


参考になりましたら、いいね。お願いします!
コメントも気軽にお願いします。
とりあえず、作成して更新していく感じ。

参考
https://learn.microsoft.com/ja-jp/dotnet/csharp/language-reference/operators/null-coalescing-operator

0
0
1

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?