简体   繁体   English

将2D数组的一行发送到C#中的函数

[英]Sending one row of a 2D array to a function in c#

I am new to C# and trying to learn how to send individual rows of a 2D array to a function. 我是C#的新手,正在尝试学习如何将2D数组的各个行发送到函数。 I have a two-dimensional array of 3 rows and 2 columns. 我有一个3行2列的二维数组。 If I want to send my third row to a function called calculate , can you please tell me how to do this. 如果我想将第三行发送给一个名为calculate的函数,请告诉我该如何做。

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            string[,] array2Db = new string[3, 2] { { "one", "two" }, { "three", "four" }, { "five", "six" } };
            calculate(array2Db[2,0]); //I want to send only the 3rd row to this array
            //This array may contain millions of words. Therefore, I can't pass each array value individually
        }

        void calculate(string[] words)
        {
            for (int i = 0; i < 2; i++)
            {
                Console.WriteLine(words);
            }
        }
    }
}

Any help would be greatly appreciated 任何帮助将不胜感激

You could make an extension method that will enumerate you the specific row. 您可以使用扩展方法来枚举特定行。

public static class ArrayExtensions
{
    public static IEnumerable<T> GetRow<T>(this T[,] items, int row)
    {
        for (var i = 0; i < items.GetLength(1); i++)
        {
            yield return items[row, i];
        }
    }
} 

Then you can use it with 然后,您可以将其与

string[,] array2Db = new string[3, 2] { { "one", "two" }, { "three", "four" }, { "five", "six" } };
calculate(array2Db.GetRow(2).ToArray());

array2Db[2, 0] will give you a single value at the third row first column, which is a string actually, not an array as the method calculate is expecting, If you want to pass the complete row means you have to call the method like the following: array2Db[2, 0]将使你在第三行第一列中的单个值,它是一个字符串实际上,不是数组作为方法calculate期待,如果你想通过完整的行意味着你必须调用方法如下所示:

calculate(new []{array2Db[2, 0],array2Db[2, 1]});

Which will pass the two columns of the third row as an array to the called method. 它将第三行的两列作为数组传递给被调用的方法。 A working Example here 这里工作示例

Using the length of the 'Y' dimension (x = 0, y = 1) we create an Enumerable of numbers between 0 and Y's length, which will serve as a loop to iterate and retrieve all the elements where the 'X' dimension = 2 (3rd in a 0-based collection) 使用“ Y”维度的长度(x = 0,y = 1),我们创建了一个介于0和Y长度之间的数字的Enumerable,它将作为循环来迭代和检索“ X”维度= 2(基于0的集合中的第3个)

var yRange = Enumerable.Range(0, array2Db.GetLength(1));
var result = yRange.Select(y => array2Db[2, y]);

Or in your case (I changed the parameter received by calculate() from string array to IEnumerable to avoid pointless type conversion: 或者在您的情况下(我将由calculate()接收的参数从字符串数组更改为IEnumerable,以避免无意义的类型转换:

calculate(Enumerable.Range(0, array2Db.GetLength(1)).Select(y => array2Db[2, y]));

static void calculate(IEnumerable<string> words)
{
    foreach(string word in words)
        Console.WriteLine(word);
}

EDIT: tried to add some clarification 编辑:试图添加一些澄清

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM