我希望C#中List数据类型的所有索引都成为任何整数数组。那可能吗? 我试过这个:
ckey = Command.IndexOf("Callers:");
其中ckey
是int[]
而Command
是List<String>
。
答案 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();