C#でのisnulloremptyの使い方は?

C#には、IsNullOrEmptyという静的メソッドがあり、文字列がnullまたは空であるかどうかを判定するために使用されます。このメソッドは、文字列型を直接呼び出すことで使用できます。以下は使用例です:

string str1 = null;
string str2 = "";
string str3 = "Hello World";

if (string.IsNullOrEmpty(str1))
{
    Console.WriteLine("str1 is null or empty");
}
else
{
    Console.WriteLine("str1 is not null or empty");
}

if (string.IsNullOrEmpty(str2))
{
    Console.WriteLine("str2 is null or empty");
}
else
{
    Console.WriteLine("str2 is not null or empty");
}

if (string.IsNullOrEmpty(str3))
{
    Console.WriteLine("str3 is null or empty");
}
else
{
    Console.WriteLine("str3 is not null or empty");
}

結果は次の通りです:

str1 is null or empty
str2 is null or empty
str3 is not null or empty

以上の例では、IsNullOrEmptyメソッドはstr1、str2、およびstr3がnullまたは空の文字列かどうかを別々に判断し、その結果に応じて情報を出力しています。

bannerAds