C # analog of multi-name iteration in Python?

In Python, you can iterate over multiple variables at the same time as follows:

my_list = [[1, 2, 3], [4, 5, 6]]

for a, b, c in my_list:
    pass

Is there a C # analog closer to this?

List<List<int>> myList = new List<List<int>> {
    new List<int> { 1, 2, 3 },
    new List<int> { 4, 5, 6 }
};

foreach (List<int> subitem in myList) {
    int a = subitem[0];
    int b = subitem[1];
    int c = subitem[2];
    continue;
}

Change To clarify, the exact code in question was to name each index in the C # example.

+5
source share
4 answers

Not too different from what you have, but what about this?

foreach (var subitem in myList.Select(si => new {a = si[0], b = si[1], c = si[2]})
{
                int a = subitem.a;
                int b = subitem.b;
                int c = subitem.c;
                continue;
}
+2
source

You can try something like this:

var myList = new[] {Tuple.Create(1, 2, 3), Tuple.Create(4, 5, 6)};
foreach (var tuple in myList)
{
    //your code
}
+1
source

# ,

a, b = 1, 2

#.

, #. a, b c .

0

, lambda.

- :

for (int i = 0; i <tab.length; i ++) {int a = tab [0]; int b = tab [1]; int c = tab [2]; }

This is just a sugar code that IMHO should not be used in general code.

0
source

All Articles