C# 传递和返回多维数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17896699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:45:43  来源:igfitidea点击:

C# Passing and returning a multidimensional array

c#multidimensional-arrayparameter-passingreturn-value

提问by user9993

I have a 2D array, that I fill randomly with numbers. The code I have for this works fine, however, to organise my code better I'm wanting to put the "fill it randomly with numbers" part into a method.

我有一个二维数组,我用数字随机填充。我的代码很好用,但是,为了更好地组织我的代码,我想将“用数字随机填充”部分放入方法中。

The array is created from the Main() method, as I plan on passing and returning the array to/from other methods that will manipulate it. I then tried to write the method for filling the array, but I'm unsure how to either pass a multidimensional array, or return one either. According to MSDN I need to use an "out" rather than a return.

该数组是从 Main() 方法创建的,因为我计划将数组传递给/从其他将操作它的方法返回。然后我尝试编写填充数组的方法,但我不确定如何传递多维数组或返回一个。根据MSDN,我需要使用“out”而不是返回。

This is what I've tried so far:

这是我迄今为止尝试过的:

    static void Main(string[] args)
    {
            int rows = 30;
            int columns = 80;



            int[,] ProcArea = new int[rows, columns];

            RandomFill(ProcArea[], rows, columns);

    }

    public static void RandomFill(out int[,] array, int rows, int columns)
    {

        array = new int[rows, columns];


        Random rand = new Random();
        //Fill randomly
        for (int r = 0; r < rows; r++)
        {
            for (int c = 0; c < columns; c++)
            {
                if (rand.NextDouble() < 0.55)
                {
                array[r, c] = 1;
            }
            else
            {
                array[r, c] = 0;
            }
        }
    }

These are the errors I have:

这些是我的错误:

"The best overloaded method match for 'ProcGen.Program.RandomFill(out int[*,*], int, int)' has some invalid arguments"
"Argument 1: cannot convert from 'int' to 'out int[*,*]'"

What am I doing wrong, and what can I do to fix these errors? Also, am I right in thinking, because I'm using "out" that all I need to do is:

我做错了什么,我能做些什么来修复这些错误?另外,我的想法是否正确,因为我使用的是“out”,我需要做的就是:

RandomFill(ProcArea[], rows, columns);

instead of?:

代替?:

ProcArea = RandomFill(ProcArea[], rows, columns);

Is there a proper way of calling the method?

是否有调用该方法的正确方法?

采纳答案by Rohit Vats

There is no need of an out parameter in your code.

您的代码中不需要 out 参数。

Arrays are passed by referenceuntil, in the method, you initialise it with a new reference.

数组passed by reference一直到,在方法中,你用一个新的引用初始化它。

So, in your method, if you don't initialise it with a new reference, then you can go without using the outparameter and values will be reflected in the original array -

因此,在您的方法中,如果您不使用新引用初始化它,那么您可以不使用out参数,并且值将反映在原始数组中 -

public static void RandomFill(int[,] array, int rows, int columns)
{

    array = new int[rows, columns]; // <--- Remove this line since this array
                                    // is already initialised prior of calling
                                    // this method.
    .........
}

回答by juhan_h

Out parameters need to be explicitly specified as outin the caller's side as well:

输出参数也需要像out在调用方一样显式指定:

RandomFill(out ProcArea[], rows, columns);

回答by ProgramFOX

Try:

尝试:

RandomFill(out ProcArea, rows, columns);

回答by Mohammad Khtheitroadul Effendi

Try it...it works :)

试试吧...它的工作原理:)

using System;
class system
{
    static void Main(string[] args)
    {
            int rows = 5;
            int columns = 5;


            int[,] ProcArea = new int[rows, columns];

            RandomFill(out ProcArea, rows, columns);

        // display new matrix in 5x5 form
            int i, j;
            for (i = 0; i < rows; i++)
            {
                for (j = 0; j < columns; j++)
                    Console.Write("{0}\t", ProcArea[i, j]);
                Console.WriteLine();
            }
            Console.ReadKey();

    }

    public static void RandomFill(out int[,] array, int rows, int columns)
    {

        array = new int[rows, columns];


        Random rand = new Random();
        //Fill randomly
        for (int r = 0; r < rows; r++)
        {
            for (int c = 0; c < columns; c++)
            {
                if (rand.NextDouble() < 0.55)
                {
                    array[r, c] = 1;
                }
                else
                {
                    array[r, c] = 0;
                }
            }
        }
    }
}