IsNullOrEmpty相当于阵列? C# [英] IsNullOrEmpty equivalent for Array? c#

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

问题描述

有没有在.NET库,将返回true或false作为一个数组是否为空或空的函数? (类似string.IsNullOrEmpty)。

我曾在阵列类寻像这样的功能,但不可能看不到任何东西。

  VAR一个=新的字符串[] {};
字符串[] B = NULL;
变种C =新的String [] {你好};IsNullOrEmpty(一); //返回true
IsNullOrEmpty(二); //返回true
IsNullOrEmpty(C); //返回false


解决方案

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

  ///<总结>指示指定数组是否为null或具有零长度LT; /总结>
///< PARAM NAME =数组方式>要测试的阵列< /参数>
///<退货和GT;真,如果数组参数为null或长度为零;否则为false< /回报>
公共静态布尔IsNullOrEmpty(此Array阵列)
{
    回报(阵列== NULL || array.Length == 0);
}

只要将这个在扩展类的地方,它会延长阵列有一个 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).

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

i.e.

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);
}

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

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

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