C#中的元组和拆包分配支持? [英] Tuples and unpacking assignment support in C#?

查看:106
本文介绍了C#中的元组和拆包分配支持?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,我可以编写

In Python I can write

def myMethod():
    #some work to find the row and col
    return (row, col)

row, col = myMethod()
mylist[row][col] # do work on this element

但是在C#中,我发现自己会写出来

But in C# I find myself writing out

int[] MyMethod()
{
    // some work to find row and col
    return new int[] { row, col }
}

int[] coords = MyMethod();
mylist[coords[0]][coords[1]] //do work on this element

Python方式显然更清洁.有没有办法在C#中做到这一点?

The Pythonic way is obivously much cleaner. Is there a way to do this in C#?

推荐答案

有一组元组 .NET中的类:

There's a set of Tuple classes in .NET:

Tuple<int, int> MyMethod()
{
    // some work to find row and col
    return Tuple.Create(row, col);
}

但是没有像Python中那样解压缩它们的紧凑语法:

But there's no compact syntax for unpacking them like in Python:

Tuple<int, int> coords = MyMethod();
mylist[coords.Item1][coords.Item2] //do work on this element

这篇关于C#中的元组和拆包分配支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆