Main Site Documentation

Passing Parameters - The Rest of the Story


#1

I’ve been wondering if/how the NETMF differs from C# when it comes to passing parameters. As you may know there are two basic methods of passing parameters to a function: by reference or by value.

As I spend most of my times these days coding in C++ I had to look up the proper syntax and a found a good write up here: http://msdn.microsoft.com/en-us/library/0f66670z(VS.71).aspx . A few quick tests similar to what is shown below confirms that the C# NETMF parameter passing works just like .NET on any other platform.

For me it was good to know that when I was passing around an ArrayList, NETMF was not making a new copy of the list to pass about. So if your nor sure what passing by reference or by value is all about take a look at the web-page above for a good explanation.

namespace Reference_Testing
{
    public class Program
    {
        public static void Main()
        {
            ArrayList value = new ArrayList();
            value.Add(0);
            Debug.Print("Value before: " + value[0].ToString());
            stuff(value);
            Debug.Print("Value after: " + value[0].ToString());
            Thread.Sleep(100000);
        }

        public static void stuff(ArrayList value)
        {
            value[0] = 200;
            Debug.Print("Value in sub: " + value[0].ToString());
        }

    }
}

Output:
Value before: 0
Value in sub: 200
Value after: 200


#2

The way parameters are passed is not a characteristic of .NET, it is a characteristic of a language. In our case C#.

Normally, the C# values are passed by value. In your example, the variable “value” is passed as a value. It contents is a reference to an array list. If you change the value of value in the called method, the contents of the original value’s value would not change.

If you wanted to change the value of the original value in the method you would need to pass a reference to value … (ref ArrayList value).

Naming a variable value can really get confusing when you are discussing reference versus value. lol

:P:P:|;)


#3

Actually an Array, ArrayList, etc are ‘reference’ types, a reference type can have its value altered after being passed to a separate function but its structure cannot be changed. In other words, I can read or change the value of an Array cell but I try to do a ‘NEW’ on it the change is only local to the called function. Other simple types like INTs, etc are passed by value by default and you have to add a ‘ref’ prefix to pass them by reference.

AFAIK, the parameter passing in all .NET languages is very similar as it all gets compiled to IRL.


#4

If you are C/C++ programmer then it is easier to think of the reference as a pointer. Using the pointer (reference) you can modify the object but if you change the pointer itself (use new on C#) then you are no pointing (referencing) a new object.

So, when you are passing an object in C#, you are actually only passing a pointer.