Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检查字符串数组不为null的正确方法_C# - Fatal编程技术网

C# 检查字符串数组不为null的正确方法

C# 检查字符串数组不为null的正确方法,c#,C#,我有一个字符串数组,它总是给出字符串数组长度>0。检查字符串数组是否为空的正确方法是什么 string[] mystr = new string[0]; ... ... if (..) { mystr = string[] oldstrArray.Clone(); } ... ... if (mystr[0].Length != 0) { //Always enters this loop even if mystr is not assigned in the above i

我有一个字符串数组,它总是给出字符串数组长度>0。检查字符串数组是否为空的正确方法是什么

string[] mystr = new string[0];
...
...
if (..) {
    mystr = string[] oldstrArray.Clone();
    }
...
...

if (mystr[0].Length != 0) {
//Always enters this loop even if mystr is not assigned in the above if condition
}

您需要的是数组中第一个字符串的长度,而不是数组本身。您想检查
mystr.Length
而不是
mystr[0].Length

另外,请注意您发布的代码-它应该编译(LINQPad对此非常有用)。您的存在语法错误,这可能会妨碍人们尝试帮助您。

请尝试以下代码片段:

private static void Main(string[] args)
{
    string[] mystr = null;
    if (IsNullOrEmpty(mystr))
    {
         //Do your thing
    }            
}

static bool IsNullOrEmpty(string[] strarray)
{
    return strarray== null || strarray.Length == 0;
}
或者这个(扩展方法)

if(mystr[0]。长度!=0)

它将给出数组第一个成员的长度。空检查很简单

if(mystr==null)


Length属性提供数组的大小。长度可用于检查空数组

请使用编程语言标记您的问题。字符串不能包含任何字符或为空。这是两种不同的情况。要测试两者,请使用字符串。IsNullOrEmpty@jdweng:OP不想检查
字符串
是否为null或空,而是检查
字符串[]
。因此他测试了!=isNullorEmpty要完成回答,可以添加案例mystr.Any:)。这就是你在最后一个案例中的描述。您对上一个案例的评论是正确的。@我们最初的答案确实使用了
.Any()
,但我认为最好使用
.All()
,但我忘了更改
.WriteLine()
=\为什么要在“正常函数”中选中
strarray<1
但是将扩展名改为
strarray==0
?我知道这是一个编辑因为我用扩展法扩展了你的答案。这就是我问你的原因。我想知道为什么我的解决方案被更正了,但不是您自己的^^^顺便说一句。我编辑了您的编辑:D('因为我必须更改至少6个字符,我更改了代码中的注释)
string[] mystr = new string[] { "", null, "", "", null };

//Check if the array itself is null which is what your question is truly asking
if (mystr == null) 
    Console.WriteLine("Array is null");

//Check to see if the array itself is empty/zero-length
if (mystr != null && mystr.Length == 0) 
    Console.WriteLine("Array contains no elements");

//Check if all elements are null or empty
if (mystr != null && mystr.All(s => string.IsNullOrEmpty(s))) 
    Console.WriteLine("All elements are either null or empty");
string[] mystr = new string[] { "", null, "", "", null };

//Check if the array itself is null which is what your question is truly asking
if (mystr == null) 
    Console.WriteLine("Array is null");

//Check to see if the array itself is empty/zero-length
if (mystr != null && mystr.Length == 0) 
    Console.WriteLine("Array contains no elements");

//Check if all elements are null or empty
if (mystr != null && mystr.All(s => string.IsNullOrEmpty(s))) 
    Console.WriteLine("All elements are either null or empty");