Arguments
-
There are two ways to pass argument: byVal or byRef. (C# and VB.Net) However, I also know that all objects are passed by reference. So what happens when we pass an object byval--Does it make a difference? Also, when arguments are passed byVal it hinders performance since a copy needs to be made. ByVal also means the called method can modify the argument and it will not affect the original data. If a method only needs to read an argument but does not need to modify it, does it make sense to send it byref? But then how can you guarantee the data will not be modified in the called method; specially when the called method is being coded by another programmer? Does it come down to simply making a choice between integrity and performance? Or is there a convention or am I just confused?
-
There are two ways to pass argument: byVal or byRef. (C# and VB.Net) However, I also know that all objects are passed by reference. So what happens when we pass an object byval--Does it make a difference? Also, when arguments are passed byVal it hinders performance since a copy needs to be made. ByVal also means the called method can modify the argument and it will not affect the original data. If a method only needs to read an argument but does not need to modify it, does it make sense to send it byref? But then how can you guarantee the data will not be modified in the called method; specially when the called method is being coded by another programmer? Does it come down to simply making a choice between integrity and performance? Or is there a convention or am I just confused?
CodingYoshi wrote:
However, I also know that all objects are passed by reference.
That is not correct. By default, objects references are passed by value. Do not confuse variable data types (value types vs. reference types) with argument passing mechanisms (pass-by-value vs pass-by-reference). For example, arrays are reference types. When you create an array, a block of memory is allocated and your array variable contains a "reference" the to the memory area (typically a four-byte address). For example: In Visual Basic:
Dim values(10) As Integer
In C#:
int[] values = new int[10];
The variable, values, contains a reference to a block of ten integers. If you pass your array to a method as an argument, the reference to the array is passed by value (important). You can modify the elements of the underlying array but you cannot assign a new array to it. To illustrate what I am talking about, take a look at this C# code:
using System;
class Program
{
static void Main()
{
int[] values = { 1, 2, 3 };
TestMethod(values);
Console.WriteLine("{0}, {1}, {2}", values[0], values[1], values[2]);
}static void TestMethod(int\[\] values) { int\[\] newArray = new int\[\] { 4, 5, 6 }; values = newArray; }
}
This code will print "
1, 2, 3
". Why? First, an array containing{ 1, 2, 3 }
is created. TheTestMethod()
creates a new array with{ 4, 5, 6 }
and attempts to point your array reference to it. But whenTestMethod()
returns, the reference is not changed because the reference was passed by value. However, you can change the underlying data pointed to by the reference. IfTestMethod()
did this:static void TestMethod(int[] values)
{
values[0] = 4;
values[1] = 5;
values[2] = 6;
}... the values would be changed (i.e., it prints "
4, 5, 6
") because you passed a reference to the data contained invalues
, whichTestMethod()
is free to change.CodingYoshi wrote:
when arguments are passed byVal it hinders performance since a copy needs to be made
Nah. Performance differences are usually insignificant. Maybe if you need to pass really large value types (like large structures) and you really need to t