C# でオブジェクトが Null かどうかを確認する
Muhammad Maisam Abbas
2024年2月16日
Csharp
Csharp Object

このチュートリアルでは、C# でオブジェクトが null かどうかを確認する方法について説明します。
C# の ==
演算子を使用して Null オブジェクトを確認する
二項演算子 ==
は、左側の値が演算子の側は、C# の演算子の右側の値と同じです。次のコード例は、C# の ==
演算子を使用してオブジェクトが null かどうかを確認する方法を示しています。
using System;
namespace check_null_object {
class Program {
static void Main(string[] args) {
string check = null;
if (check == null) {
Console.WriteLine("check is null");
} else {
Console.WriteLine("check is not null");
}
}
}
}
出力:
check is null
上記のコードは、C# の ==
二項演算子を使用して、文字列変数 check
が null
であるかどうかをチェックします。
C# で is
キーワードを使用して Null オブジェクトを確認する
is
キーワードを使用して、C# でオブジェクトが null かどうかを確認することもできます。is
キーワードは、C# の二項演算子 ==
の代わりに使用されます。次のコード例は、C# の is
キーワードを使用してオブジェクトが null かどうかを判断する方法を示しています。
using System;
namespace check_null_object {
class Program {
static void Main(string[] args) {
string check = null;
if (check is null) {
Console.WriteLine("check is null");
} else {
Console.WriteLine("check is not null");
}
}
}
}
出力:
check is null
上記のコードは、C# の ==
二項演算子を使用して、文字列変数 check
が null
であるかどうかをチェックします。
チュートリアルを楽しんでいますか? <a href="https://www.youtube.com/@delftstack/?sub_confirmation=1" style="color: #a94442; font-weight: bold; text-decoration: underline;">DelftStackをチャンネル登録</a> して、高品質な動画ガイドをさらに制作するためのサポートをお願いします。 Subscribe
Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.
LinkedIn