ByValue parameter is not used
-
Hello, I want to call a method which accepts an array. I've defined an array (originalArray) and want work with it within the method CheckArray. But if I change something within the array inside the method, the original array will be modified too. I thought the parameters are ByValue automatically? Isn't it? Thanks in advance for every tip.
private void button4_Click(object sender, System.EventArgs e) { CheckArray(originalArray); } private void CheckArray(int[,] iArray) { int[,] copyArray = iArray; copyArray[0,0] = 0; }
-
Hello, I want to call a method which accepts an array. I've defined an array (originalArray) and want work with it within the method CheckArray. But if I change something within the array inside the method, the original array will be modified too. I thought the parameters are ByValue automatically? Isn't it? Thanks in advance for every tip.
private void button4_Click(object sender, System.EventArgs e) { CheckArray(originalArray); } private void CheckArray(int[,] iArray) { int[,] copyArray = iArray; copyArray[0,0] = 0; }
Seraphin wrote:
Hello, I want to call a method which accepts an array. I've defined an array (originalArray) and want work with it within the method CheckArray. But if I change something within the array inside the method, the original array will be modified too. I thought the parameters are ByValue automatically? Isn't it?
Yes. But...
private void button4_Click(object sender, System.EventArgs e)
{
CheckArray(originalArray);
}private void CheckArray(int[,] iArray)
{
int[,] copyArray = iArray;
copyArray[0,0] = 0;
}You've not copied the array, you've only created another reference to the same array. When you assign a value to the array using the copied reference, the original array is affected, too.
iArray
andcopyArray
both reference the same array. Remember,iArray
is just a reference to an array held somewhere in memory. References are just variables. The reference is passed by value so that if you did something like this:private void button4_Click(object sender, System.EventArgs e)
{
CheckArray(originalArray);
}private void CheckArray(int[,] iArray)
{
iArray = new int[10, 10];
iArray[0, 0] = 0;
}The
originalArray
will not be affected. You've only changed theiArray
variable, which has local scope within the method. To copy the array, you have to create a new array, not just a new reference, and copy the contents of the old array into the new array. Unfortunately, theCopyTo
method only works for one-dimensional arrays, so you may have to write code that does the copying by hand. Or maybe someone else knows of a better way. -
Seraphin wrote:
Hello, I want to call a method which accepts an array. I've defined an array (originalArray) and want work with it within the method CheckArray. But if I change something within the array inside the method, the original array will be modified too. I thought the parameters are ByValue automatically? Isn't it?
Yes. But...
private void button4_Click(object sender, System.EventArgs e)
{
CheckArray(originalArray);
}private void CheckArray(int[,] iArray)
{
int[,] copyArray = iArray;
copyArray[0,0] = 0;
}You've not copied the array, you've only created another reference to the same array. When you assign a value to the array using the copied reference, the original array is affected, too.
iArray
andcopyArray
both reference the same array. Remember,iArray
is just a reference to an array held somewhere in memory. References are just variables. The reference is passed by value so that if you did something like this:private void button4_Click(object sender, System.EventArgs e)
{
CheckArray(originalArray);
}private void CheckArray(int[,] iArray)
{
iArray = new int[10, 10];
iArray[0, 0] = 0;
}The
originalArray
will not be affected. You've only changed theiArray
variable, which has local scope within the method. To copy the array, you have to create a new array, not just a new reference, and copy the contents of the old array into the new array. Unfortunately, theCopyTo
method only works for one-dimensional arrays, so you may have to write code that does the copying by hand. Or maybe someone else knows of a better way. -
Thx, but what "ref" stands for, if you use it in a parameter list? If its just a reference, why I should use "ref"? I've read that ByValue is the standard, so I am confused ...
Hmm, I thought I had replied to this post, but it's not showing up. Here is the second attempt (good thing I had my answer saved). Say I did this:
private void button4_Click(object sender, System.EventArgs e)
{
CheckArray(ref originalArray);
}private void CheckArray(ref int[,] iArray)
{
iArray = new int[10, 10];
iArray[0, 0] = 0;
}This will actually change the
originalArray
variable. It now points to the new array I created in theCheckArray
method. Without theref
modifier, it would only change the localiArray
variable. A reference is a variable that points to some object in memory. TheoriginalArray
variable is a reference to an array held in memory somewhere. It allows me to perform operations on the array. However, operations on theoriginalArray
variable itself only effect that variable. For example:int[] arrayA = new int[10];
int[] arrayB = arrayA;// Both arrayA and arrayB reference the same array.
// An operation on the array via the arrayA reference variable.
// Does not affect arrayB.
arrayA[0] = 42;// An operation on the arrayA reference variable itself.
arrayA = null;// This will print 42 because the arrayB reference variable
// still points to the array.
Console.WriteLine(arrayB[0]);When a reference variable is passed by value to a method, a copy of the reference variable itself is made, much like what I did above with the
arrayB
variable. You'll notice that when I nulled out thearrayA
, it did not affect thearrayB
variable. It's the same when you pass a reference by value to a method. operations on the reference variable itself only affect the local variable. Now, theref
keyword gives me a reference to a reference, so to speak. If I modify a parameter withref
, and perform operations that change the variable itself, it also affects the reference variable that was originally passed to the method. This takes awhile to wrap your mind around it, but keep at it and it will eventually make sense. :)