且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用for循环从c#中的列表中检索值...

更新时间:2023-02-23 08:36:15

所以,你有一个字符串数组列表(有趣,但你应该知道)。

以下是访问值的代码示例:

So, you have a list of string arrays (interesting, but you should know).
Here is a code sample for accessing the values:
List<string[]> list = new List<string[]>();
list.Add(new string[] { "Id", "Name", "Age" });

for(int i=0; i<list.Count; i++)
    for(int j=0; j<list[i].Count(); j++)
        Console.WriteLine(list[i][j]);

// simpler with foreach
foreach(var l in list)
    foreach(string s in l)
        Console.WriteLine(s);



请注意,如果这是你的意图,很难用一个for循环来访问这些值 - 我想可以做到,但这不明智,因为你的代码会很乱。


Please note, that will be hard to access the values with just a single for loop if that''s your intention - I suppose can be done, but it is not wise, since your code will be a mess.