我有以下代码行,根据命令列表项,它应该进入if块,但它不会去那里。在调试模式int[] ckeys
只显示{int[0]}
没有任何值,你能告诉我这是什么问题吗?
List<string> Command=new List<string>();
string ASCLICAL = "Callers:";
string ASCLIMEM= "Members:";
string ASCLINOCAL="NoCallers";
int[] ckeys = Command.Select((s, idx) => new { Str = s, Idx = idx })
.Where(p => p.Str == ASCLICAL)
.Select(p => p.Idx)
.ToArray();
if(ckeys == null) {
ckeys = Command.Select((s, idx) => new { Str = s, Idx = idx })
.Where(p => p.Str == ASCLINOCAL)
.Select(p => p.Idx)
.ToArray();
}
命令列表包含:
[0] = "300 has 0 calls (max unlimited) in 'ringall' strategy (173s holdtime), W:0, C:1, A:0, SL:0.0% within 0s"
[1] = "Members:"
[2] = "Local/409@from-internal/n (In use) has taken 1 calls (last was 64167 secs ago)"
[3] = "No Callers"
[4] = ""
答案 0 :(得分:1)
永远不会为空。如果数组不包含元素,那么它将始终是一个包含0个元素的数组。
您可以使用Array.Length
或Array.Count
并检查尺寸&gt; 0
if (ckeys.Length == 0) {
}
答案 1 :(得分:1)
ckeys
不为空,它是空的。您应该将ckeys == null
更改为ckeys.Length == 0