[英]C# List<string> add index to list console output
In the bellow sample scenario i like to add the index of the item to the output console.在下面的示例场景中,我喜欢将项目的索引添加到 output 控制台。 And i just dont get it to work.我只是不让它工作。
public static void Main()
{
List<string> myList = new List<string> { "This", "is", "a", "test" };
foreach (var item in myList)
Debug.WriteLine(item);
}
Something like this像这样的东西
Debug.WriteLine(item.index + " : : + item);
You can use Select
to get an items with their index and print it out您可以使用Select
获取带有索引的项目并将其打印出来
List<string> myList = new List<string> { "This", "is", "a", "test" };
foreach (var item in myList.Select((value, index) => new { value, index }))
Debug.WriteLine($"{item.value}:{item.index}");
You can also use IndexOf
method, bit it'll return a first index and can cause a problem, if list has a duplicated items您也可以使用IndexOf
方法,如果列表中有重复项,它将返回第一个索引并可能导致问题
foreach (var item in myList)
Debug.WriteLine($"{item}:{myList.IndexOf(item)}");
Using a regular for
loop also might be an option使用常规for
循环也可能是一种选择
for (int i = 0; i < myList.Count; i++)
Debug.WriteLine($"{myList[i]}:{i}");
public static void Main()
{
int index = 0;
List<string> myList = new List<string> { "This", "is", "a", "test" };
foreach (var item in myList)
{
Debug.WriteLine(item + " " + index);
index++;
}
}
Remember that sometimes the simplest approach is the best, especially if you are a beginner.请记住,有时最简单的方法是最好的,尤其是如果您是初学者。 While Pavel's answer is also correct and preferable, if you need to ask this question you are just starting with programming, I would advice you to stick to general solutions (incrementing an index variable) which work in all programing languages.虽然 Pavel 的回答也是正确且可取的,但如果您需要问这个问题,您才刚刚开始编程,我建议您坚持适用于所有编程语言的通用解决方案(增加索引变量)。 Once you are comfortable with those, move to language specific sugars一旦你对这些感到满意,就转向语言特定的糖
You can declare index
variable:您可以声明index
变量:
List<string> myList = new List<string> { "This", "is", "a", "test" };
var index = 0;
foreach (var item in myList)
Console.WriteLine($"{item} {index++}");
Or using simple for
loop where i
is alread declared and your loop more flexible - you can iterate from end to start and from start to end of List<T>
:或者使用简单for
循环,其中i
已经声明并且您的循环更加灵活 - 您可以从头到尾以及从头到尾迭代List<T>
:
List<string> myList = new List<string> { "This", "is", "a", "test" };
for (int i = 0; i < myList.Count; i++)
{
Console.WriteLine($"{myList[i]} {i}");
}
The C# foreach does not have an inbuilt index. C# foreach 没有内置索引。 You can go with two approaches.您可以使用两种方法 go。
int index=-1;
foreach (var item in myList){
index++;
Debug.WriteLine(index + ":" + item);
}
or you can use the forloop或者你可以使用 forloop
for (int i = 0; i < myList.Length; i++)
{
var item = myList[i];
Debug.WriteLine(i+ ":" + item);
}
You can use您可以使用
myList.IndexOf(item)
to access item's index so your code should be:访问项目的索引,所以你的代码应该是:
public static void Main()
{
List<string> myList = new List<string> { "This", "is", "a", "test" };
foreach (var item in myList)
Debug.WriteLine(item + ": " + myList.IndexOf(item));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.