Home > Articles

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

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.13, the value of each variable that Main() uses when calling Combine() will be copied into the parameters of the Combine() method. Output 5.5 shows the results of this listing.

Listing 5.13: Passing Variables by Value

class Program
{
  static void Main()
  {
      // ...
      string fullName;
      string driveLetter = "C:";
      string folderPath  = "Data";
      string fileName    = "index.html";

      fullName = Combine(driveLetter, folderPath, fileName);

      Console.WriteLine(fullName);
      // ...
  }

  static string Combine(
      string driveLetter, string folderPath, string fileName)
  {
      string path;
      path = string.Format("{1}{0}{2}{0}{3}",
          System.IO.Path.DirectorySeparatorChar,
          driveLetter, folderPath, fileName);
      return path;
  }
}

Output 5.5

C:\Data\index.html

Even if the Combine() method assigns 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.

Reference Parameters (ref)

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

Listing 5.14: Passing Variables by Reference

class Program
{
  static void Main()
  {
      // ...
      string first = "hello";
      string second = "goodbye";
      Swap(ref first, ref second);            

      Console.WriteLine(
          $@"first = ""{ first }"", second = ""{ second }""" );
      // ...
  }

  static void Swap(ref string x, ref string y)
  {
      string temp = x;
      x = y;
      y = temp;
  }
}

Output 5.6

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.13’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.14, 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.

Begin 7.0

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.15, which returns the phone button corresponding to a character.

Listing 5.15: Passing Variables Out Only

class ConvertToPhoneNumber
{
  static int Main(string[] args)
  {
      if(args.Length == 0)
      {
          Console.WriteLine(
              "ConvertToPhoneNumber.exe <phrase>");
          Console.WriteLine(
              "'_' indicates no standard phone button");
          return 1;
      }
      foreach(string word in args)
      {
          foreach(char character in word)
          {
              if(TryGetPhoneButton(character, out char button))
              {
                  Console.Write(button);
              }
              else
              {
                  Console.Write('_');
              }
          }
      }
      Console.WriteLine();
      return 0;
  }
  static bool TryGetPhoneButton(char character, out char button)
  {
      bool success = true;
      switch( char.ToLower(character) )
      {
          case '1':
              button = '1';
              break;
          case '2': case 'a': case 'b': case 'c':
              button = '2';
              break;

          // ...

          case '-':
              button = '-';
              break;
          default:
                // Set the button to indicate an invalid value
                button = '_';
              success = false;
              break;
      }
      return success;
  }
}

Output 5.7 shows the results of Listing 5.15.

Output 5.7

>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.15 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 error 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.15 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 (which would require referencing the System.ValueTuple NuGet package) but without C# 7.0 syntax.

Begin 7.2

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 so that the value type could be passed by reference so that not only copy of the value type occurred but, in addition, the invoked method could not change the value type. 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) will result in a compile error indicating that number is read only.

End 7.2

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.16.

Listing 5.16: ref Return and ref Local Declaration

// Returning a reference
public static ref byte FindFirstRedEyePixel(byte[] image)
{
  // Do fancy image detection perhaps with machine learning
  for (int counter = 0; counter < image.Length; counter++)
  {
    if(image[counter] == (byte)ConsoleColor.Red)
    {
      return ref image[counter];
    }
  }
  throw new InvalidOperationException("No pixels are red.");
}

public static void Main()
{
  byte[] image = new byte[254];
  // Load image
  int index = new Random().Next(0, image.Length - 1);
  image[index] =
      (byte)ConsoleColor.Red;
  System.Console.WriteLine(
      $"image[{index}]={(ConsoleColor)image[index]}");
  // ...

  // Obtain a reference to the first red pixel
  ref byte redPixel = ref FindFirstRedEyePixel(image);
  // Update it to be Black
  redPixel = (byte)ConsoleColor.Black;
  System.Console.WriteLine(
      $"image[{index}]={(ConsoleColor)image[redPixel]}");
}

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 line of Listing 5.16. Checking for the update via the array shows that the value is now black.

There are two important restrictions on return by reference—both due to object lifetime: Object references shouldn’t be garbage collected while they’re still referenced, and 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; } }

Second, 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.16, 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:

    class Thing { ref string _Text;  /* Error */ }
  • You can’t declare a by-reference type for an auto-implemented property:

    class Thing { ref string Text { get;set; }  /* Error */ }
  • Properties that return a reference are allowed:

    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 = null; ref int number = 42;  // ERROR

    End 7.0

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.13. 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.17.

Listing 5.17: Passing a Variable Parameter List

using System;
using System.IO;
class PathEx
{
  static void Main()
  {
      string fullName;

      // ...

      // Call Combine() with four arguments      
      fullName = Combine(                        
          Directory.GetCurrentDirectory(),       
          "bin", "config", "index.html");        
      Console.WriteLine(fullName);

      // ...

      // Call Combine() with only three arguments
      fullName = Combine(                        
          Environment.SystemDirectory,           
          "Temp", "index.html");                 
      Console.WriteLine(fullName);         

      // ...

      // Call Combine() with an array            
      fullName = Combine(                        
          new string[] {                         
              "C:\\", "Data",                    
              "HomeDir", "index.html"} );        
      Console.WriteLine(fullName);
      // ...
  }

  static string Combine(params string[] paths)
  {
      string result = string.Empty;
      foreach (string path in paths)
      {
          result = Path.Combine(result, path);
      }
      return result;
  }
}

Output 5.8 shows the results of Listing 5.17.

Output 5.8

C:\Data\mark\bin\config\index.html
C:\WINDOWS\system32\Temp\index.html
C:\Data\HomeDir\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 identical to Path.Combine() except that PathEx.Combine() handles a variable number of parameters rather than simply two.

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 parameters. The resulting CIL code is identical.

  • 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.

  • + Share This
  • 🔖 Save To Your Account