将整个数组从一种方法传递到另一种方法

时间:2016-12-26 20:58:11

标签: c#

如何将数组从一种方法传递到另一种方法? 同样在main方法中,洗牌一些数组接收一个错误,说它接受零参数,但我想在哪里放一个参数? 非常感谢一些示例代码

  class Program
{
    static void Main(string[] args)
    {
       ShuffledSomeArray();
        DoSomethingWithArray();
        Console.ReadLine();
    }
     static string[] ShuffledSomeArray(string [] array)
    {
        array = new string[5] { "1", "2", "3", "4", "5" };
        Random rnd = new Random();
        for(int i = 4; i>=0; i--)
        {
            int shuffle = rnd.Next(0, i);
           string rndpick = array[shuffle];
           array[shuffle] = array[i];
                array[i] = rndpick;
            Console.Write(array[i]);
        }

    }
    static void DoSomethingWithArray()
    {

    }
}

1 个答案:

答案 0 :(得分:1)

这样的事情:

 class Program
{
    static void Main(string[] args)
    {
        string[] arr = new string[5] { "1", "2", "3", "4", "5" };
        string[] result = ShuffledSomeArray(arr);
        DoSomethingWithArray(result);
        Console.ReadLine();
    }
     static string[] ShuffledSomeArray(string [] array)
    {
        Random rnd = new Random();
        for(int i = 4; i>=0; i--)
        {
            int shuffle = rnd.Next(0, i);
           string rndpick = array[shuffle];
           array[shuffle] = array[i];
                array[i] = rndpick;
            Console.Write(array[i]);
        }

    }
    static void DoSomethingWithArray(string[] array)
    {

    }
}