我有一个方法,它的参数是字符串数组。在函数中我用另一个数组覆盖它时它不会在它之外改变(如果我理解正确的话数组是通过引用传递的)。
我有一个看起来像这样的方法:
static void Method(string word, string[] tab)
{
string [] tab1;
[..]
tab = tab1; // tab changes to tab1
}
static void Main(string[] args)
{
string[] tab = { "", "", "", "", "", "", "", "", "", "" };
Method("443", tab);
//and here tab does not change like I though it would.
}
最佳答案
更好的设计应该是:
static string[] Method(string word, string[] tab)
{
string [] tab1;
[..]
return tab1;
}
static void Main(string[] args)
{
string[] tab = { "", "", "", "", "", "", "", "", "", "" };
tab = Method("443", tab);
}
关于c# - 在方法中重写时的数组参数不会在其外部更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42841938/