Monday 29 July 2019

Value, Reference and Out Parameters in C# Programming

Confused by all the parameter types in C#? here’s a quick guide to help you sort out the differences between value, reference and out parameters. This is an extract from my book, The Little Book Of C#.

By default, when you pass variables to functions or methods these are passed as ‘copies’. That is, their values are passed as arguments and these values are assigned to the corresponding parameters declared by the function. Any changes made within the method will affect only the copies (the parameters) within the scope of the method. The original variables that were passed as arguments (and which were declared outside the method) retain their original values.

Sometimes, however, you may in fact want any changes that are made to parameters inside a method to change the matching variables (the arguments) in the code that called the method. In order to do that, you can pass variables ‘by reference’. When variables are passed by reference, the original variables (or, to be more accurate, the references to the location of those variables in your computer’s memory) are passed to the function. So any changes made to the parameter values inside the function will also change the variables that were passed as arguments when the function was called.

To pass arguments by reference, both the parameters defined by the function and the arguments passed to the function must be preceded by the keyword ref. The following examples should clarify the difference between ‘by value’ and a ‘by reference’ arguments. In each case, I assume that two int variables have been declared like this:

int firstnumber;
int secondnumber;
firstnumber = 10;
secondnumber = 20;


Example 1: By Value Parameters

private void ByValue(int num1, int num2) {
    num1 = 0;
    num2 = 1;
}

This method might be called like this:

ByValue(firstnumber, secondnumber);

Remember that firstnumber had the initial value of 10, and secondnumber had the initial value of 20. Only the copies (the values of the parameters, num1 and num2) were changed in the ByValue() method. So, after I call that method, the values of the two variables that I passed as arguments are unchanged:

firstnumber now has the value 10.
secondnumber now has the value 20.

Example 2: By Reference Parameters

private void ByReference(ref int num1, ref int num2) {
    num1 = 0;
    num2 = 1;
}

This method might be called like this:

ByReference(ref firstnumber, ref secondnumber);

Once again, firstnumber has the initial value of 10, and secondnumber has the initial value of 20. But these are now ref parameters, so the parameters num1 and num2 ‘refer’ to the original variables. When changes are made to the parameters, the original variables are also changed:

firstnumber now has the value 0.
secondnumber now has the value 1.

You may also use out parameters which must be preceded by the out keyword instead of the ref keyword.

Example 3: out Parameters

private void OutParams(out int num1, out int num2) {
    num1 = 0;
    num2 = 1;
}

This method might be called like this:

int firstnumber;
int secondnumber;
OutParams(out firstnumber, out secondnumber);

In this case, as with ref parameters, the values of the variables that were passed as arguments are changed when the values of the parameters are changed:

firstnumber now has the value 0.
secondnumber now has the value 1.

At first sight, out parameters may seem similar to ref parameters. However, it is not obligatory to assign a value to a variable passed as an out argument before you pass it to a method. It is obligatory to assign a value to a variable passed as a ref argument.

You can see this in the example shown above. I do not initialize the values of firstnumber and secondnumber before calling the OutParams() method. That would not be permitted if I were using ordinary (by value) or ref (by reference) parameters. On the other hand, it is obligatory to assign a value to an out parameter within the method that declares that parameter. This is not obligatory with a ref argument.

If you need a complete guide to C# programming, my book, The Little Book Of C# is available on Amazon (US), Amazon (UK) and worldwide.