|
-
Sep 8th, 2007, 04:45 PM
#1
Thread Starter
New Member
Arrays by ref
Consider:
private Double A = 1;
private Double C = 1;
private Double[,] B = new double[2, 2];
private Double[,] e = new double;
private Double[,] D = new double[2, 2];
private void test(ref double A, ref double[,] B, double C, double[,] D)
{
A = 0;
C = 0;
B[1, 1] = 0;
D[1, 1] = 0;
}
After the test sub executes, everything is changed but "C". Clearly, array names are references themselves ("pointers" is a better term).
2 questions:
Under what circumstances might one pass an array as ref double[,] instead of simply double[,]?
Is making a copy of an array the only way to protect an array from being overwritten?
-
Sep 8th, 2007, 05:01 PM
#2
Thread Starter
New Member
Re: Arrays by ref
 Originally Posted by CarlBuddig
private Double[,] B = new double[2, 2];
private Double[,] D = new double[2, 2];
Sorry, make that
private Double[,] B = new double[2, 2];
private Double[,] D = new double[2, 2];
then button sets B[1,1] and D[1,1] = 1 and calls test
-
Sep 8th, 2007, 08:01 PM
#3
Re: Arrays by ref
Let's start at the beginning.
1. .NET apps have two areas of memory available to them: the stack and the heap. The stack is where variables, amongst other things, are stored and the heap is where objects are stored.
2. There are two types of types in .NET apps: value types and reference types. Structures are value types and classes are reference types.
3. When you declare a variable in .NET code it is created on the stack. If your variable is of a value type then that stack variable contains the value of the variable. For instance, if you do this:an Int32 variable is created on the stack and the value 123 is stored in it. If your variable is a reference type then that stack variable contains the memory address of an object on the heap. For instance, if you do this:a Form1 variable is created on the stack, then a Form1 object is created on the heap, then the memory address of the Form1 object is stored in the Form1 variable. That's why it's called a reference type: because the variable refers to the object rather than actually containing the object.
4. In .NET code arrays are reference types: they are instances of the array class. When you do this:
Code:
string[] arr = new string[10];
a string array variable is created on the stack, then a string array object with enough space for 10 string objects is created on the heap, then the memory address of the array object is stored in the array variable.
5. Whether you pass method parameters by value or by reference, only variables are ever copied. No class instances are ever copied under any circumstances. That's one reason that structures should only ever be relatively simple: because they will be copied when passing by value.
6. When you pass a parameter by value you are passing a copy of the variable. In the case of structures this means that you've copied the value, so any changes made in the method will affect only that copy, not the original. In the case of classes it means that you've copied the memory address contained in the variable. That means that the parameter contains the same memory address as the original variable, so they refer to the same object. Any changes you make to that object will obviously affect the original as well, because they are the same object. On the other hand, if you assign a new object to the parameter within the method, that will not affect the original variable as it still refers to the original object.
7. When you pass a parameter by reference you are actually passing a reference to the variable. That means that any changes you make inside the method are made to the variable referred to by the parameter, thus any and all changes will affect the original. For value types, that means that if you assign a new value to the parameter inside the method that same value will be assigned to the original variable. For reference types it means that if make any changes to the object referred to by the parameter or if you assign a new object to the parameter, all those changes will affect the original variable too.
So, to your question. If you pass a variable into a method, any changes made to any of the elements will affect the original, whether it's passed by value or by reference. If you don't want that then you would need to make a copy of the array and pass that. That said, if the array contains reference type objects as elements then both arrays contain references to the same objects, so any changes made to an element of one will still affect the corresponding element of the original.
Having said all that, I have never found myself in a position where I was passing an array to a method that would make changes to that array when I didn't want those changes made. If you find that you are then it sounds like a design issue.
I should also mention that once created, arrays cannot have their size changed. That means that if you pass an array to a method by value and inside the method the size of the array is changed, what's actually happening is that a new array object is created and the elements of the original are copied to the new. As I said, assigning new reference type objects to parameters passed by value does not affect the original variable, so you're safe from that. What you would not be safe from is new values or objects being assigned to the elements of the existing array.
-
Sep 9th, 2007, 07:53 AM
#4
Thread Starter
New Member
Re: Arrays by ref
Thank you for taking the time to provide that clear explanation.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|