微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C#中的元组和解包分配支持?

Python中我可以写
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#中,我发现自己写出来

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

Pythonic方式是非常清洁.有没有办法在C#中做到这一点?

解决方法

.NET中有一组 Tuple类:
Tuple<int,int> MyMethod()
{
    // some work to find row and col
    return Tuple.Create(row,col);
}

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

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

原文地址:https://www.jb51.cc/csharp/94838.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐