Advanced Method Parameters

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.

Value Parameters

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.

Listing 5.17: Passing Variables by Value
1. public class Program
2. {
3.     public static void Main()
4.     {
5.         // ...
6.         string fullName;
7.         string driveLetter = "C:";
8.         string folderPath = "Data";
9.         string fileName = "index.html";
10.  
11.         fullName = Combine(driveLetter, folderPath, fileName);
12.  
13.         Console.WriteLine(fullName);
14.         // ...
15.     }
16.  
17.     static string Combine(
18.         string driveLetter, string folderPath, string fileName)
19.     {
20.         string path;
21.         path = string.Format("{1}{0}{2}{0}{3}",
22.             Path.DirectorySeparatorChar,
23.             driveLetter, folderPath, fileName);
24.         return path;
25.     }
26. }
Output 5.4
C:\Users\Inigo\Data\index.html

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.

Beginner Topic
Matching Caller Variables with Parameter Names

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.

AdVanced Topic
Reference Types versus Value Types

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.

Reference Parameters (ref)

Consider Listing 5.18, which calls a function to swap two values, and Output 5.5, which shows the results.

Listing 5.18: Passing Variables by Reference
1. public class Program
2. {
3.     public static void Main()
4.     {
5.         // ...
6.         string first = "hello";
7.         string second = "goodbye";
8.         Swap(ref first, ref second);
9.  
10.         Console.WriteLine(
11.             $@"first = ""{ first }"", second = ""{ second }""");
12.         // ...
13.     }
14.  
15.     static void Swap(ref string x, ref string y)
16.     {
17.         string temp = x;
18.         x = y;
19.         y = temp;
20.     }
21. }
Output 5.5
first = "goodbye", second = "hello"

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.

note
The ref modifier assigns a parameter to refer to an existing variable on the stack rather than creating a new variable and copying the argument value into the parameters.
Output Parameters (out)

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.

Listing 5.19: Passing Variables Out Only
1. public static int Main(string[] args)
2. {
3.     if(args.Length == 0)
4.     {
5.         Console.WriteLine(
6.             "ConvertToPhoneNumber.exe <phrase>");
7.         Console.WriteLine(
8.             "'_' indicates no standard phone button");
9.         return 1;
10.     }
11.     foreach(string word in args)
12.     {
13.         foreach(char character in word)
14.         {
15.             if (TryGetPhoneButton(character, out char button))
16.             {
17.                 Console.Write(button);
18.             }
19.             else
20.             {
21.                 Console.Write('_');
22.             }
23.         }
24.     }
25.     Console.WriteLine();
26.     return 0;
27. }
28.  
29. static bool TryGetPhoneButton(char character, out char button)
30. {
31.     bool success = true;
32.     switch(char.ToLower(character))
33.     {
34.         case '1':
35.             button = '1';
36.             break;
37.         case '2':
38.         case 'a':
39.         case 'b':
40.         case 'c':
41.             button = '2';
42.             break;
43.  
44.         // ...
45.  
46.         case '-':
47.             button = '-';
48.             break;
49.         default:
50.             // Set the button to indicate an invalid value
51.             button = '_';
52.             success = false;
53.             break;
54.     }
55.     return success;
56. }

Output 5.6 shows the results of Listing 5.19.

Output 5.6
>ConvertToPhoneNumber.exe CSharpIsGood
274277474663

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.

note
Each and every normal code path must result in the assignment of all out parameters.
Read-Only Pass by Reference (in)

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.

Return by Reference

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.

Listing 5.20: ref Return and ref Local Declaration
1. // Returning a reference
2. public static ref byte FindFirstRedEyePixel(byte[] image)
3. {
4.     // Do fancy image detection perhaps with machine learning
5.     for (int counter = 0; counter < image.Length; counter++)
6.     {
7.         if (image[counter] == (byte)ConsoleColor.Red)
8.         {
9.             return ref image[counter];
10.         }
11.     }
12.     throw new InvalidOperationException("No pixels are red.");
13. }
14. public static void Main()
15. {
16.     byte[] image = new byte[254];
17.     // Load image
18.     int index = new Random().Next(0, image.Length - 1);
19.     image[index] =
20.         (byte)ConsoleColor.Red;
21.     Console.WriteLine(
22.         $"image[{index}]={(ConsoleColor)image[index]}");
23.     // ...
24.  
25.     // Obtain a reference to the first red pixel
26.     ref byte redPixel = ref FindFirstRedEyePixel(image);
27.     // Update it to be Black
28.     redPixel = (byte)ConsoleColor.Black;
29.     Console.WriteLine(
30.         $"image[{index}]={(ConsoleColor)image[redPixel]}");
31. }

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:

References to fields or array elements
Other reference-returning properties or functions
References that were passed in as parameters to the by-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:

If you’re returning a reference, you obviously must return it. This means, therefore, that in the example in Listing 5.20, even if no red-eye pixel exists, you still need to return a reference byte. The only workaround would be to throw an exception. In contrast, the by-reference parameter approach allows you to leave the parameter unchanged and return a bool indicating success. In many cases, this might be preferable.
When declaring a reference local variable, initialization is required. This involves assigning it a ref return from a function or a reference to a variable:

ref string text; // Error

Although it’s possible in C# 7.0 to declare a reference local variable, declaring a field of type ref isn’t allowed (see  “Instance Fields” in Chapter 6 for more information on declaring fields):

class Thing { ref string _Text; /* Error */ }

You can’t declare a by-reference type for an auto-implemented property (see  “Properties” in Chapter 6 for more information on declaring auto-properties):

class Thing { ref string Text { get;set; } /* Error */ }

Properties that return a reference are allowed (see “Properties” in Chapter 6 for more information on declaring properties):

class Thing { string _Text = "Inigo Montoya";

ref string Text { get { return ref _Text; } } }

A reference local variable can’t be initialized with a value (such as null or a constant). It must be assigned from a by-reference-returning member or a local variable, field, or array element:

ref int number = 42; // ERROR

Parameter Arrays (params)

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.

Listing 5.21: Passing a Variable Parameter List
1. using System;
2. using System.IO;
3.  
4. public class Program
5. {
6.     public static void Main()
7.     {
8.         string fullName;
9.  
10.         // ...
11.  
12.         // Call Combine() with four parameters
13.         fullName = Combine(
14.             Directory.GetCurrentDirectory(),
15.             "bin""config""index.html");
16.         Console.WriteLine(fullName);
17.  
18.         // ...
19.  
20.         // Call Combine() with only three parameters
21.         fullName = Combine(
22.             Environment.SystemDirectory,
23.             "Temp""index.html");
24.         Console.WriteLine(fullName);
25.  
26.         // ...
27.  
28.         // Call Combine() with an array
29.         fullName = Combine(
30.             new string[] {
31.                 $"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}""Documents",
32.                 "Web""index.html" });
33.         Console.WriteLine(fullName);
34.         // ...
35.     }
36.  
37.     static string Combine(params string[] paths)
38.     {
39.         string result = string.Empty;
40.         foreach(string path in paths)
41.         {
42.             result = Path.Combine(result, path);
43.         }
44.         return result;
45.     }
46. }
Output 5.7
C:\Users\Inigo\src\Chapter05.Tests\bin\Debug\net7.0\index.html
C:\WINDOWS\system32\Temp\index.html
C:\Users\Inigo\index.html

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:

1.
Places params immediately before the last parameter in the method declaration
2.
Declares the last parameter as an array

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:

The parameter array is not necessarily the only parameter on a method.
The parameter array must be the last parameter in the method declaration. Since only the last parameter may be a parameter array, a method cannot have more than one parameter array.
The caller can specify zero arguments that correspond to the parameter array parameter, which will result in an array of zero items being passed as the parameter array.
Parameter arrays are type-safe: The arguments given must be compatible with the element type of the parameter array.
The caller can use an explicit array rather than a comma-separated list of arguments.
If the target method implementation requires a minimum number of parameters, those parameters should appear explicitly within the method declaration, forcing a compile error instead of relying on runtime error handling if required parameters are missing. For example, if you have a method that requires one or more integer arguments, declare the method as int Max(int first, params int[] operands) rather than as int Max(params int[] operands) so that at least one value is passed to Max().

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.

Guidelines
DO use parameter arrays when a method can handle any number—including zero—of additional arguments.

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.

{{ snackbarMessage }}
;