IsNullOrEmpty 等效于 Array 吗?C# [英] IsNullOrEmpty equivalent for Array? C#

查看:33
本文介绍了IsNullOrEmpty 等效于 Array 吗?C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET 库中是否有一个函数会返回 true 或 false,以确定数组是 null 还是空?(类似于string.IsNullOrEmpty).

Is there a function in the .NET library that will return true or false as to whether an array is null or empty? (Similar to string.IsNullOrEmpty).

我在 Array 类中查看了诸如此类的函数,但什么也没看到.

I had a look in the Array class for a function such as this but couldn't see anything.

var a = new string[]{};
string[] b = null;
var c = new string[]{"hello"};

IsNullOrEmpty(a); //returns true
IsNullOrEmpty(b); //returns true
IsNullOrEmpty(c); //returns false

推荐答案

没有现成的方案,但你可以使用这个扩展方法:

There isn't an existing one, but you could use this extension method:

/// <summary>Indicates whether the specified array is null or has a length of zero.</summary>
/// <param name="array">The array to test.</param>
/// <returns>true if the array parameter is null or has a length of zero; otherwise, false.</returns>
public static bool IsNullOrEmpty(this Array array)
{
    return (array == null || array.Length == 0);
}

只需将它放在某个扩展类中,它就会扩展 Array 以获得一个 IsNullOrEmpty 方法.

Just place this in an extensions class somewhere and it'll extend Array to have an IsNullOrEmpty method.

这篇关于IsNullOrEmpty 等效于 Array 吗?C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆