开发者

How to Use Array.Exists with the Multi-dimensional string array

I have a multi-dim string array something like this:-

string[,] names = new string[2, 2] { {"Rosy",""}, {"Peter","Albert"} };

Now i want to check the existence if the second index (Albert) holding the string is non-empty in the whole arr开发者_开发百科ay. I just to check the existence of the non-empty string value in the second index. I was thinking of using the Array.Exists. If there is any other better way, please share.

Thanks


I don't think you can use Array.Exists here, because that only deals with the values - you're interested in the position too. I would just use a loop:

bool found = false;
for (int i = 0; i < names.GetLength(0); i++)
{
    if (!string.IsNullOrEmpty(names[i, 1]))
    {
        found = true;
        break;
    }
}

Rectangular arrays are basically a bit of a pain to work with in C#. If you had a jagged array - an array of arrays - it would be easy:

bool found = jagged.Select(x => x[1])
                   .Any(value => !string.IsNullOrEmpty(value));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜