C#中的二维数组切片 [英] Two dimensional array slice in C#

查看:141
本文介绍了C#中的二维数组切片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在 C# 中对二维数组进行切片.

I'm looking to slice a two dimensional array in C#.

我有 double[2,2] 价格并且想要检索此数组的第二行.我试过价格 [1,],但我觉得它可能是别的东西.

I have double[2,2] prices and want to retrieve the second row of this array. I've tried prices[1,], but I have a feeling it might be something else.

提前致谢.

推荐答案

没有直接的切片"操作,但是你可以像这样定义一个扩展方法:

There's no direct "slice" operation, but you can define an extension method like this:

public static IEnumerable<T> SliceRow<T>(this T[,] array, int row)
{
    for (var i = 0; i < array.GetLength(0); i++)
    {
        yield return array[i, row];
    }
}

double[,] prices = ...;

double[] secondRow = prices.SliceRow(1).ToArray();

这篇关于C#中的二维数组切片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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