List的所有索引

时间:2013-05-25 13:03:05

标签: c# arrays list

我希望C#中List数据类型的所有索引都成为任何整数数组。那可能吗? 我试过这个:

ckey = Command.IndexOf("Callers:");

其中ckeyint[]CommandList<String>

1 个答案:

答案 0 :(得分:1)

int[] indices = Enumerable.Range(0, Command.Count).ToArray();

编辑:如果你想找到给定字符串的索引,你可以这样做:

string toFind = //
int[] indices = strs.Select((s, idx) => new { Str = s, Idx = idx })
                    .Where(p => p.Str == toFind)
                    .Select(p => p.Idx)
                    .ToArray();
相关问题