Types of Parameter Passing in C#
The sections that follow explain the different types of parameter passing: * Passing arguments by value * Ref modifier * Out modifier * Implications of passing by reference * Params modifier * Optional parameters * 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 * using System; * namespace Ch01 * { * class Program * { * static void Main(string[] args) * { * /* Initializes with 100 */ * int x = 100; * Console.WriteLine("Before method call :\t{0}", x); * /* pass as value to the Increment method*/ * Increment(x); * Console.WriteLine("After method call :\t{0}", x); * } * /* a is the parameter for the MethodA*/ * static void Increment(int a) * { * ++a; * Console.WriteLine("Incremented value :\t{0}", a); * } * } * } 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 * using System; * namespace Ch01 * { * class Program * { * static void Main(string[] args) * { * int x = 100; /* Needs to initialize x */ * Console.WriteLine(x); /* 100 */ * Increment(ref x); /* pass the location (0x052de8b4) of the x */ * Console.WriteLine(x); /* 101 */ * } * static void Increment(ref int a) /* a pointing to the same memory * * location as x (0x052de8b4) * * of Main method */ * { ++a; } * } * } 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 ModifierCode Snippet * using System; * namespace Ch01 * { * class Program * { * static void Main(string[] args) * { * int x; /* Does not need to initialize x */ * SetInitialValue(out x); * Console.WriteLine(x); /* 1 */ * } * static void SetInitialValue(out int a) * { a = 1; } * } * } 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 * using System; * namespace Ch01 * { * class Program * { * static void Main(string[] args) * { * string[] planets = { "Jupiter", "\n", "Pallas" }; * Console.WriteLine("{0}", ConcatStrings(planets)); * } * static string ConcatStrings(params string[] items) * { * string result = default(string); * foreach (string item in items) * { result = string.Concat(result, item); } * return result; * } * } * } 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 * using System; * namespace Ch01 * { * class Program * { * static void Main(string[] args) * { * Show(); /* Please specify message */ * Show("Message set"); /* Message set */ * } * static void Show(string message = "Please specify message") * { * Console.WriteLine(message); * } * } * } 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 * using System; * namespace Ch01 * { * class Program * { * static void Main(string[] args) * { * Add(a: 10, b: 10); /* 20 */ * Add(10, b: 10); /* 20 */ * //Add(a: 10, 10); /* Compile time error, position */ * } * static void Add(int a, int b) * { * Console.WriteLine(a + b); * } * } * } This program will produce the output:20 20 http://dlvr.it/4dlzHL











