将整个数组从一种方法传递到另一种
问题描述:
如何将数组从一种方法传递到另一种方法? 同样在主要方法中,洗牌某些数组会收到一个错误,说它需要零参数,但我想在那里放置什么参数? 一些示例代码是极大的赞赏将整个数组从一种方法传递到另一种
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()
{
}
}
答
事情是这样的:
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)
{
}
}
你通过任何其他参数相同的方式。 – SLaks
虽然我认为谷歌应该是您的第一个选择这样的问题:看看这里:https://msdn.microsoft.com/en-us/library/hyfeyz71.aspx –