加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 06:44:04 所属栏目:百科 来源:网络整理
导读:在 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
在 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读