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

前端之家收集整理的这篇文章主要介绍了C#中的元组和解包分配支持?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Python中我可以写 @H_301_2@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#中,我发现自己写出来

@H_301_2@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类: @H_301_2@Tuple<int,int> MyMethod() { // some work to find row and col return Tuple.Create(row,col); }

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

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

猜你在找的C#相关文章