开发者

Using C# foreach tuple

开发者 https://www.devze.com 2023-02-12 18:54 出处:网络
How can I work with tuples in a foreach loop? The following code doesn\'t work: foreach Tuple(x, y) in sql.lineparams(lines)

How can I work with tuples in a foreach loop?

The following code doesn't work:

foreach Tuple(x, y) in sql.lineparams(lines)
{

}

sql.lineparams(li开发者_运维百科nes) is array of tuples <int, string>


What does the tuple consist of? Types called x and y? In that case, this should be your syntax:

foreach (Tuple<x, y> tuple in sql.lineparams(lines))
{
  ...
}

If the tuple actually consist of other types, like int and string, it will be like this:

foreach (Tuple<int, string> tuple in sql.lineparams(lines))
{
  ...
}

Or, you can let the compiler handle it for you:

foreach (var tuple in sql.lineparams(lines))
{
  ...
}


With C# 7 you can also directly reference the content of the tuple:

foreach ((x xVar, y yVar) in sql.lineparams(lines))
{

}
0

精彩评论

暂无评论...
验证码 换一张
取 消