SoFunction
Updated on 2025-03-06

Detailed explanation of parameter array, reference parameters and output parameters in C#

Preface

This article mainly introduces related content about C# parameter array, reference parameters and output parameters. It is shared for your reference and learning. I won’t say much below. Let’s take a look at the detailed introduction together.

Parameter array

In C#, you can specify an indefinite length parameter for the function. This parameter is the last parameter in the function definition. This parameter is called a parameter array.

Here is an example:

namespace Ch6Ex2
{
 class Program
 {
 static int SumVals(params int[] vals)
 {
 int sum = 0;
 foreach (int val in vals)
 {
 sum += val;
 }
 return sum;
 }
 static void Main(string[] args)
 {
 int sum = SumVals(1, 2, 3, 4, 5);
 ($"Summed Values = {sum}");
 ();
 }
 }
}

The function SumVals has an array of parameters, namely vals. When defining this parameter, you need to use the params parameter. When calling this function, you can pass multiple actual parameters into the parameter input.

When using a distributed parameter transfer, the compiler does the following:

1) Accept the argument list, use them to create and initialize an array in the heap.

2) Save the reference of the array to the formal parameters in the stack.

3) If there are no actual parameters in the corresponding shape parameter group, the compiler will create an array with zero elements to use.

4) If the array parameter is a value type, then the value is copied and the actual parameter is not affected by the internal method.

5) If the array parameter is a reference type, then the reference is copied, and the object referenced by the actual parameter can be affected within the method.

When using array arguments, the compiler uses your data instead of recreating one. That is, it is equivalent to reference parameters.

Reference parameters

Parameters can be passed by reference, and the ref keyword is required.

Here is an example:

namespace MyProgram
{
 class Program
 {
 static void SwapInts (ref int a, ref int b)
 {
  int temp = b;
  b = a;
  a = temp; 
 }
 static void Main(string[] args)
 {
  int a = 1;
  int b = 2;
  ($"a = {a}, b = {b}");
  SwapInts(ref a, ref b);
  ($"a = {a}, b = {b}");
  ();
 }
 }
}

This is a simple program that exchanges two values. Since the function SwapInts uses reference parameters, the values ​​of the quantities a and b can be modified in the function. It should be noted that when calling the function, you must also use ref to pass reference parameters.

Output parameters

The output parameter uses the out keyword, and its effect is almost the same as the reference parameter. The difference is:

  • The actual parameter that references the parameter must be a variable that has been assigned, and the output parameter does not have to be.
  • When a function uses an output parameter, it should be regarded as unassigned.

Here is an example:

namespace MyProgram
{
 class Program
 {
 static int MaxValue (int[] intArray, out int maxIndex)
 {
  int maxValue = intArray[0];
  maxIndex = 0;
  for (int i = 0; i < ; i++)
  {
  if (intArray[i] > maxValue)
  {
   maxValue = intArray[i];
   maxIndex = i;
  }
  }
  return maxValue;
 }
 static void Main(string[] args)
 {
  int maxIndex;
  int[] intArray = { 12, 45, 23, 77, 73 };
  int maxValue = MaxValue(intArray, out maxIndex);
  ($"maxValue = {maxValue}, maxIndex = {maxIndex}.");
  ();
 }
 }
}

This function takes the index of the maximum value in an array as the output parameter and returns the maximum value.

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.