Loop a list with a Tuple C#
Asked Answered
P

3

8

I have a list in C# called MyList:

List<Tuple<string, int, int>> MyList= new List<Tuple<string, int, int>>() 
        {
           Tuple.Create("Cars", 22, 3),
           Tuple.Create("Cars", 28, 5),
           Tuple.Create("Planes", 33, 6)
        };

I want to loop through the whole list in the same order as I filled it above and be able to get the values one by one for each list item, like Cars, 22, 3. How do I do that? I guess I have to use ForEach somehow.

Photophilous answered 6/3, 2020 at 11:26 Comment(6)
You would generally write a loop in order to loop over a collection, yes. What have you tried and what didn't work?Paterfamilias
A loop is indeed what you would need!Perdue
You can switch to named tuples, it's more clear and readableCocainism
I know that I need a loop. But I don't know how to write it. This is what i did: ´ MyList.ForEach(Tuple<string, int, int> tuple) { };`Photophilous
@Photophilous that's not a loop. That's an attempt to use the ForEach method with an incorrect lambda syntax. foreach(var ... in MyList){ ... } is a loopEumenides
@Photophilous you can use simple for loop as it is in my answer below.Playhouse
C
10

You can use simple foreach loop for that, Item1, Item2 and Item3 represent an unnamed tuple items with corresponding types

foreach (var item in MyList)
{
    string name = item.Item1;
    int value = item.Item2;
    int something = item.Item3;
}

You can also switch to named tuples, which are available from C# 7. They are more readable and allow to define a custom name for tuple item and use a deconstruction

var MyList = new List<(string name, int value, int someting)>()
{
    ("Cars", 22, 3),
    ("Cars", 28, 5),
    ("Planes", 33, 6)
};

foreach (var item in MyList)
{
    var name = item.name;
    var value = item.value;
}
Cocainism answered 6/3, 2020 at 11:31 Comment(4)
It works but I get the type or namespace name 'ValueTuple' does not exist in the namespace 'System when using named tuples.Photophilous
@Photophilous it's available from C# 7 and .NET 4.6.1. You can try to install System.ValueTuple package, if you don't have it from the boxCocainism
@KGB91, you can use my answer which is more simple.Playhouse
Yeah but it is easier to fill the tuples with this code :)Photophilous
M
2

If you want to be more declarative about what your values are you can explicitly set them in a foreach loop like this:

List<Tuple<string, int, int>> myList= new List<Tuple<string, int, int>>() 
        {
           Tuple.Create("Cars", 22, 3),
           Tuple.Create("Cars", 28, 5),
           Tuple.Create("Planes", 33, 6)
        };    

foreach ((string type, int fuel, int amount) vehicles in myList) {
        string item1 = vehicles.type;
        int item2 = vehicles.fuel;
        int item3 = vehicles.amount;
    }
Mamoun answered 12/8, 2023 at 12:34 Comment(0)
P
1

This is an easy to understand and simple solution that will help you.
You can use a for loop.

  • Item1 (string),
  • Item2 (int),
  • Item3 (int)

are the Tuple Items in the order and in the format that you have placed them.

And the code below is the loop.

for (int i = 0; i < MyList.Count; i++)
{
     string item1 = MyList[i].Item1;
     int item2 = MyList[i].Item2;
     int item3 = MyList[i].Item3;
}
Playhouse answered 6/3, 2020 at 11:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.