The result highlights the distinction between passing by value and "passing by reference". When you pass a pointer to a function in C++ or you pass a reference to a function in C#, it is really passed by value. That means a copy of the address is made for the function and it is assigned to the function parameter variable. If the function then changes the address contained in the local parameter variable, e.g. assigning it null, that does not affect the address contained in the original variable back in main(). However, the local parameter variable does contain a valid address of an object, so the function can use that address to change the object (since no copy of the object is made for the function, the terms "copy of the object" and "original object" don't apply).Originally Posted by CompiledMonkey
When you pass by value, the function is only restricted from changing the value contained in the specific variable that is sent to the function. If you try the example again and put 'ref' in front of the function parameter variable, then inside the function if you change the address stored in the parameter variable, you will be changing the address stored in the original variable back in Main().
Also try the example this way: create two references to the same object in Main(). Use 'ref' for the function parameter name, and send one object reference to the function. Use the parameter variable to change the original object, and also assign null to the parameter variable. Then in main() show that the object was changed using your second reference to display the object, and show that the first reference's address was changed to null.
I see.The class all of this code was in is called Program. So I had to make a reference since the methods outside of main are not static.