【C#】配列やコレクションの IsNullOrEmpty [⇒PCページへ]

2021年07月11日19:45  C# 写真あり


 ああ、そう言えば「配列 や List の TryGetValue」やったのに、もっとよく使うもの忘れてたな~、と思い出したので、とても簡単なものだが、あると非常に便利ものなので一応書いておこう。

 これは文字列で string.IsNullOrEmpy() をよく使うので、同じような感覚で、配列や List 等が null または 空 (Count = 0) でないかを調べるものがあれば、便利なのにな~と思ったので、昔作ったものだ。実際にあまりに頻繁に使うので、標準関数かと思い違いしてしまうほどだ(笑)。

 まぁたぶん、皆似たようなものを作ってる気がするが、まだ勉強しはじめたばかりの人もいるかもなので(面白いことに、もう随分たくさんの記事を書いたが、上級者向けより、初心者向けの記事の方がアクセスは多いものなのだ)、役に立つかはわからないが、とりあえず書いておこう。



●配列やコレクションの IsNullOrEmpty
using System;
using System.Collections.Generic;

public static class Extensions //名前は任意
{
/// <summary>
/// コレクションが null または Count = 0 か?
/// 2021/07/11 Fantom
/// http://fantom1x.blog130.fc2.com/blog-entry-399.html
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="collection">調べるコレクション</param>
/// <returns>true : null または Count = 0</returns>
public static bool IsNullOrEmpty<T>(this ICollection<T> collection)
{
return collection == null || collection.Count == 0;
}

/// <summary>
/// 配列が null または Length = 0 か?
/// 2021/07/11 Fantom
/// http://fantom1x.blog130.fc2.com/blog-entry-399.html
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array">調べる配列</param>
/// <returns>true : null または Length = 0</returns>
public static bool IsNullOrEmpty<T>(this T[] array)
{
return (array == null || array.Length == 0);
}
}

●メインコード例
using System;
using System.Collections.Generic;

var emptyList = new List();
var list = new List() { 1, 2, 3 };
Console.WriteLine("emptyList : " + emptyList.IsNullOrEmpty());
Console.WriteLine("list : " + list.IsNullOrEmpty());

var emptyDic = new Dictionary();
var dic = new Dictionary()
{
{ "hoge", 100 },
{ "fuga", 200 },
};
Console.WriteLine("emptyDic : " + emptyDic.IsNullOrEmpty());
Console.WriteLine("dic : " + dic.IsNullOrEmpty());

var emptySet = new HashSet();
var set = new HashSet() { "hoge", "fuga" };
Console.WriteLine("emptySet : " + emptySet.IsNullOrEmpty());
Console.WriteLine("set : " + set.IsNullOrEmpty());

var emptyArray = new string[0];
var array = new string[] { "apple", "banana", "candy" };
Console.WriteLine("emptyArray : " + emptyArray.IsNullOrEmpty());
Console.WriteLine("array : " + array.IsNullOrEmpty());

emptyList : True
list : False
emptyDic : True
dic : False
emptySet : True
set : False
emptyArray : True
array : False

 これも本当になんてことないものなんだけどね。できればこれも標準関数に欲しいくらい(笑)。

 私はこれに NOT値(反転値)を求める IsExist() みたいな定義も加えて、よく使っている。

●!IsNullOrEmpty() (反転値) を返す IsExist()
//null でない、かつ Count > 0
public static bool IsExist(this ICollection collection) => !collection.IsNullOrEmpty();

//null でない、かつ Length > 0
public static bool IsExist(this T[] array) => !array.IsNullOrEmpty();

 これは「if (!list.IsNullOrEmpty())」みたいなものを「if (list.IsExist())」と「!」を入れないで済むので、少しわかりやすくなるので重宝してる。まぁ、日本語でも「〇〇でなくはない」みたいな否定の否定って意味がわかりずらいからね。その辺は好みで(笑)。






(関連記事)
【C#】配列 や List の TryGetValue
【Unity】【C#】配列・リストのシャッフル
【C#】2次元配列(ジャグ配列・多次元配列)のソート
【C#】多次元配列とジャグ配列(2次元配列)のサイズ(長さ)、相互変換など
【C#】配列やListなどの中身(要素)を見る拡張メソッド Dump


関連記事
トラックバックURL

http://fantom1x.blog130.fc2.com/tb.php/399-fab88b4a

前の記事 次の記事