Types of Parameter Passing in C#

The sections that follow explain the different types of parameter passing:

  1. Passing arguments by value
  2. Ref modifier
  3. Out modifier
  4. Implications of passing by reference
  5. Params modifier
  6. Optional parameters
  7. Named arguments


Passing Arguments by Value

A value parameter is used for input parameter passing. A value parameter corresponds to a local variable that gets its initial value from the argument that is passed for the parameter. Modifications to a value parameter do not affect the argument that is passed for the parameter. Listing 1-18 provides an example of parameter passing.

Code Snippet
  1. using System;
  2. namespace Ch01
  3. {
  4.     class Program
  5.     {
  6.         static void Main(string[] args)
  7.         {
  8.             /* Initializes with 100 */
  9.             int x = 100;
  10.             Console.WriteLine("Before method call :\t{0}", x);
  11.             /* pass as value to the Increment method*/
  12.             Increment(x);
  13.             Console.WriteLine("After method call :\t{0}", x);
  14.         }
  15.         /* a is the parameter for the MethodA*/
  16.         static void Increment(int a)
  17.         {
  18.             ++a;
  19.             Console.WriteLine("Incremented value :\t{0}", a);
  20.         }
  21.     }
  22. }

This program produced the output:
Before method call : 100
Incremented value : 101
After method call : 100

The call of the Increment method will be passed with the value type; as a result, the increment of the
value of parameter a in the Increment method does not update the original value of the X in the Main
method.



Ref Modifier

A reference parameter is used for both input and output parameter passing, and during execution of the method, the reference parameter represents the same storage location as the argument variable. A reference parameter is declared with the ref modifier. The example in Listing 1-19 shows the use of the ref parameter.

Example of the Ref Parameter


Code Snippet
  1. using System;
  2. namespace Ch01
  3. {
  4.     class Program
  5.     {
  6.         static void Main(string[] args)
  7.         {
  8.             int x = 100; /* Needs to initialize x */
  9.             Console.WriteLine(x); /* 100 */
  10.             Increment(ref x); /* pass the location (0x052de8b4) of the x */
  11.             Console.WriteLine(x); /* 101 */
  12.         }
  13.         static void Increment(ref int a) /* a pointing to the same memory
  14. * location as x (0x052de8b4)
  15. * of Main method */
  16.         { ++a; }
  17.     }
  18. }

This program will produce the output:100
101



Out Modifier


An output parameter is used for output parameter passing. An output parameter is similar to a reference parameter except that the initial value of the caller-provided argument is not necessary. An output parameter is declared with the out modifier. Listing 1-20 provides an example of the use of an out modifier. 
Example of the Out Modifier
Code Snippet
  1. using System;
  2. namespace Ch01
  3. {
  4.     class Program
  5.     {
  6.         static void Main(string[] args)
  7.         {
  8.             int x; /* Does not need to initialize x */
  9.             SetInitialValue(out x);
  10.             Console.WriteLine(x); /* 1 */
  11.         }
  12.         static void SetInitialValue(out int a)
  13.         { a = 1; }
  14.     }
  15. }

This program will produce the output:
1


Implications of Passing by Reference

When an argument passes by reference to a method, the same storage location is used to access that variable. In above Listing x and a refer to the same location.



Params Modifier

A parameter array permits a variable number of arguments to be passed to a method. A parameter array is declared with the params modifier. Only the last parameter of a method can be a parameter array, and the type of a parameter array must be a single dimensional array type. Listing 1-21 provides an example of the use of the params modifier.

Example of the Params Modifier


Code Snippet
  1. using System;
  2. namespace Ch01
  3. {
  4.     class Program
  5.     {
  6.         static void Main(string[] args)
  7.         {
  8.             string[] planets = { "Jupiter", "\n", "Pallas" };
  9.             Console.WriteLine("{0}", ConcatStrings(planets));
  10.         }
  11.         static string ConcatStrings(params string[] items)
  12.        {
  13.             string result = default(string);
  14.             foreach (string item in items)
  15.             { result = string.Concat(result, item); }
  16.             return result;
  17.         }
  18.     }
  19. }


This program will produce the output:
Jupiter
Pallas



Optional Parameters

A parameter can be optional if the default value for the parameter is specified in its declaration, as shown in Listing below.

Example of the Optional Parameters

Code Snippet
  1. using System;
  2. namespace Ch01
  3. {
  4.     class Program
  5.     {
  6.         static void Main(string[] args)
  7.         {
  8.             Show(); /* Please specify message */
  9.             Show("Message set"); /* Message set */
  10.         }
  11.         static void Show(string message = "Please specify message")
  12.         {
  13.             Console.WriteLine(message);
  14.         }
  15.     }
  16. }


This program will produce the output:
Please specify message
Message set


Named Arguments

A named argument is used to identify the argument by name instead of its position. Listing provides an example of the named argument.

Example of the Named Arguments

Code Snippet
  1. using System;
  2. namespace Ch01
  3. {
  4.     class Program
  5.     {
  6.         static void Main(string[] args)
  7.         {
  8.             Add(a: 10, b: 10); /* 20 */
  9.             Add(10, b: 10); /* 20 */
  10.             //Add(a: 10, 10); /* Compile time error, position */
  11.         }
  12.         static void Add(int a, int b)
  13.         {
  14.             Console.WriteLine(a + b);
  15.         }
  16.     }
  17. }


This program will produce the output:20
20

Post a Comment