Parameter Modifiers                  
                                                      
By default, C# passes method parameters by value.  This means the system copies the parameter from the
calling code into the method.  It also means changes made to the parameter do not affect the calling code because
the changes are applied to a copy.

You can change this behavior by applying the following parameter modifiers.





















A method can only have a single params modifier, and it must be the final parameter of the method.   Use the
ref
modifier to declare a pass by reference parameter.  In this case, the method receives a reference (or pointer) to
the caller’s data. Therefore, changes affect the caller’s copy.  Reference parameters must be initialized before
being sent to the method because you are passing a reference to an existing instance.


// ref parameters are passed by reference.
private static void Add(int x, int y, ref int ans)
{
  ans = x + y;
}

private static void Main()
{

  // C# requires local vars to be initialized before use.

  int result = 0;                

  // Note use of ref keyword in calling syntax.

  Add(90, 90, ref result);
  Console.WriteLine("90 + 90 = {0}", result);

  int result2;

  // Compile error! result2 error must be initialized.

  Add(90, 90, ref result2);
}



An out parameter is like ref, except that the value does not need to be initialized.  Instead, the method assigns
the parameter with a value.


// out parameters must be assigned by the method.
private static void Add(int x, int y, out int ans)
{
  ans = x + y;
}

private static void Main()
{

  // No need to assign before use when a variable is passed
  // in an out parameter.

  int result;                

  // Note use of out keyword in calling syntax.

  Add(90, 90, out result);
  Console.WriteLine("90 + 90 = {0}", result);
}



If the called method does not make an assignment to an output parameter before leaving the defining scope, it
generates a compile error.


// Error!  All out parameters must be assigned!

private static void Add(int x, int y, out int ans)
{
  Console.WriteLine(x + y);
}


The params modifier allows you to send a varying number of parameters as a single parameter.  A method can
only have one such params parameter, and it must be the last parameter.


static void Main(string[] args)
{
  double average;

  // Pass in an list of doubles...

  average = CalculateAverage(4.0, 3.2, 5.7);

  // ...Or pass an array of doubles.

  double[] data = {4.0, 3.2, 5.7};
  average = CalculateAverage(data);
}

static double CalculateAverage(
params double[] values)
{
  double sum = 0;
  for(int i = 0; i < values.Length; i++)
     sum += values[i];

  return (sum / values.Length);
}
Parameter Modifiers
Table of Contents
C# Tutorial | C#.NET Tutorial | Modifiers Tutorial

Copyright (c) 2008.  Intertech, Inc. All Rights Reserved.  This information is to be used
exclusively as an online learning aid.  Any attempts to copy, reproduce, or use for training is
strictly prohibited.
Courseware
Training Resources
Tutorials

Parameter Modifier

Meaning in Life

(none)

If a parameter is not marked with a parameter modifier, it is assumed to be   
an input parameter passed by value.

out

The parameter is passed by reference. The called method must assign a         
 value to each output parameter, or you will receive a compile error.

ref

The parameter is passed by reference. The called method may optionally      
reassign the ref parameter but is not required to.

params

This parameter modifier allows you to send in a varying number of
parameters as a single parameter.  
A method can only have a single params modifier, and it must be the final
parameter of the method.   
Services