So far this chapter’s examples have returned data via the method return value. This section demonstrates how methods can return data via their method parameters and how a method may take a variable number of arguments.
Arguments to method calls are usually passed by value, which means the value of the argument expression is copied into the target parameter. For example, in Listing 5.17, the value of each variable that Main() uses when calling Combine() will be copied into the parameters of the Combine() method. Output 5.4 shows the results of this listing.
Even if the Combine() method assigned null to driveLetter, folderPath, and fileName before returning, the corresponding variables within Main() will maintain their original values because the variables are copied when calling a method. When the call stack unwinds at the end of a call, the copied data is thrown away.
In Listing 5.17, the variable names in the caller exactly matched the parameter names in the called method. This matching is provided simply for readability purposes, whether names match is irrelevant to the behavior of the method call. The parameters of the called method and the local variables of the calling method are found in different declaration spaces and have nothing to do with each other.
For the purposes of this section, it is inconsequential whether the parameter passed is a value type or a reference type. Rather, the important issue is whether the called method can write a value into the caller’s original variable. Since a copy of the caller variable’s value is made, the caller’s variable cannot be reassigned. Nevertheless, it is helpful to understand the difference between a variable that contains a value type and a variable that contains a reference type.
The value of a reference type variable is, as the name implies, a reference to the location where the data associated with the object is stored. How the runtime chooses to represent the value of a reference type variable is an implementation detail of the runtime; typically, it is represented as the address of the memory location in which the object’s data is stored, but it need not be.
If a reference type variable is passed by value, the reference itself is copied from the caller to the method parameter. As a result, the target method cannot update the caller variable’s value, but it may update the data referred to by the reference.
Alternatively, if the method parameter is a value type, the value itself is copied into the parameter, and changing the parameter in the called method will not affect the original caller’s variable.
Consider Listing 5.18, which calls a function to swap two values, and Output 5.5, which shows the results.
The values assigned to first and second are successfully switched. To do this, the variables are passed by reference. The obvious difference between the call to Swap() and Listing 5.17’s call to Combine() is the inclusion of the keyword ref in front of the parameter’s data type. This keyword changes the call such that the variables used as arguments are passed by reference, so the called method can update the original caller’s variables with new values.
When the called method specifies a parameter as ref, the caller is required to supply a variable, not a value, as an argument and to place ref in front of the variables passed. In so doing, the caller explicitly recognizes that the target method could reassign the values of the variables associated with any ref parameters it receives. Furthermore, it is necessary to initialize any local variables passed as ref because target methods could read data from ref parameters without first assigning them. In Listing 5.18, for example, temp is assigned the value of first, assuming that the variable passed in first was initialized by the caller. Effectively, a ref parameter is an alias for the variable passed. In other words, it is essentially giving a parameter name to an existing variable, rather than creating a new variable and copying the value of the argument into it.
As mentioned earlier, a variable used as a ref parameter must be assigned before it is passed to the called method, because the called method might read from the variable. The “swap” example given previously must read and write from both variables passed to it. However, it is often the case that a method that takes a reference to a variable intends to write to the variable but not to read from it. In such cases, clearly it could be safe to pass an uninitialized local variable by reference.
To achieve this, code needs to decorate parameter types with the keyword out. This is demonstrated in the TryGetPhoneButton() method in Listing 5.19, which returns the phone button corresponding to a character.
Output 5.6 shows the results of Listing 5.19.
In this example, the TryGetPhoneButton() method returns true if it can successfully determine the character’s corresponding phone button. The function also returns the corresponding button by using the button parameter, which is decorated with out.
An out parameter is functionally identical to a ref parameter; the only difference is which requirements the language enforces regarding how the aliased variable is read from and written to. Whenever a parameter is marked with out, the compiler checks that the parameter is set for all code paths within the method that return normally (i.e., the code paths that do not throw an exception). If, for example, the code does not assign button a value in some code path, the compiler will issue an error indicating that the code didn’t initialize button. Listing 5.19 assigns button to the underscore character because even though it cannot determine the correct phone button, it is still necessary to assign a value.
A common coding mistake when working with out parameters is to forget to declare the out variable before you use it. Starting with C# 7.0, it is possible to declare the out variable inline when invoking the function. Listing 5.19 uses this feature with the statement TryGetPhoneButton(character, out char button) without ever declaring the button variable beforehand. Prior to C# 7.0, it would be necessary to first declare the button variable and then invoke the function with TryGetPhoneButton(character, out button).
Another C# 7.0 feature is the ability to discard an out parameter entirely. If, for example, you simply wanted to know whether a character was a valid phone button but not actually return the numeric value, you could discard the button parameter using an underscore: TryGetPhoneButton(character, out _).
Prior to C# 7.0’s tuple syntax, a developer of a method might declare one or more out parameters to get around the restriction that a method may have only one return type; a method that needs to return two values can do so by returning one value normally, as the return value of the method, and a second value by writing it into an aliased variable passed as an out parameter. Although this pattern is both common and legal, there are usually better ways to achieve that aim. For example, if you are considering returning two or more values from a method and C# 7.0 is available, it is likely preferable to use C# 7.0 tuple syntax. Prior to that, consider writing two methods, one for each value, or still using the System.ValueTuple type but without C# 7.0 syntax.
In C# 7.2, support was added for passing a value type by reference that was read only. Rather than passing the value type to a function so that it could be changed, read-only pass by reference was added: It allows the value type to be passed by reference so that not only copy of the value type occurs but, in addition, the invoked method cannot change the value. In other words, the purpose of the feature is to reduce the memory copied when passing a value while still identifying it as read only, thus improving the performance. This syntax is to add an in modifier to the parameter. For example:
int Method(in int number) { ... }
With the in modifier, any attempts to reassign number (number++, for example) results in a compile error indicating that number is read only.
Another C# 7.0 addition is support for returning a reference to a variable. Consider, for example, a function that returns the first pixel in an image that is associated with red-eye, as shown in Listing 5.20.
By returning a reference to the variable, the caller is then able to update the pixel to a different color, as shown in the highlighted lines of Listing 5.20. Checking for the update via the array shows that the value is now yellow.
There are two important restrictions on return by reference, both due to object lifetime: (1) Object references shouldn’t be garbage collected while they’re still referenced, and (2) they shouldn’t consume memory when they no longer have any references. To enforce these restrictions, you can only return the following from a reference-returning function:
For example, FindFirstRedEyePixel() returns a reference to an item in the image array, which was a parameter to the function. Similarly, if the image was stored as a field within the class, you could return the field by reference:
byte[] _Image;
public ref byte[] Image { get { return ref _Image; } }
In addition, ref locals are initialized to refer to a particular variable and can’t be modified to refer to a different variable.
There are several return-by-reference characteristics of which to be cognizant:
ref string text; // Error
class Thing { ref string _Text; /* Error */ }
class Thing { ref string Text { get;set; } /* Error */ }
class Thing { string _Text = "Inigo Montoya";
ref string Text { get { return ref _Text; } } }
ref int number = 42; // ERROR
In the examples so far, the number of arguments that must be passed has been fixed by the number of parameters declared in the target method declaration. However, sometimes it is convenient if the number of arguments may vary. Consider the Combine() method from Listing 5.17. In that method, you passed the drive letter, folder path, and filename. What if the path had more than one folder, and the caller wanted the method to join additional folders to form the full path? Perhaps the best option would be to pass an array of strings for the folders. However, this would make the calling code a little more complex, because it would be necessary to construct an array to pass as an argument.
To make it easier on the callers of such a method, C# provides a keyword that enables the number of arguments to vary in the calling code instead of being set by the target method. Before we discuss the method declaration, observe the calling code declared within Main(), as shown in Listing 5.21 with Output 5.7.
In the first call to Combine(), four arguments are specified. The second call contains only three arguments. In the final call, a single argument is passed using an array. In other words, the Combine() method takes a variable number of arguments—presented either as any number of string arguments separated by commas or as a single array of strings. The former syntax is called the expanded form of the method call, and the latter form is called the normal form.
To allow invocation using either form, the Combine() method does the following:
With a parameter array declaration, it is possible to access each corresponding argument as a member of the params array. In the Combine() method implementation, you iterate over the elements of the paths array and call System.IO.Path.Combine(). This method automatically combines the parts of the path, appropriately using the platform-specific directory separator character. Note that PathEx.Combine() is for demonstration only as it provides a rough implementation of what System.IO.Path.Combine() does already.
There are a few notable characteristics of the parameter array:
Using a parameter array, you can pass a variable number of arguments of the same type into a method. The section “Method Overloading,” which appears later in this chapter, discusses a means of supporting a variable number of arguments that are not necessarily of the same type.
By the way, a path Combine() function is a contrived example since, in fact, System.IO.Path.Combine() is an existing function that is overloaded to support parameter arrays.