SoFunction
Updated on 2025-03-06

How to use ref and out keywords to pass array objects in C# programming

In C#, arrays are actually objects, not just addressable contiguous memory areas like those in C and C++. Array is an abstract base type for all array types. You can use properties that Array has as well as other class members. An example of this usage is to use the Length property to get the length of the array. The following code assigns the length of the numbers array (5) to a variable named lengthOfNumbers:

int[] numbers = { 1, 2, 3, 4, 5 };
int lengthOfNumbers = ;

The Array class provides many other useful methods and properties for sorting, searching, and copying arrays.
Example
This example uses the Rank property to display the dimensions of an array.

class TestArraysClass
{
  static void Main()
  {
    // Declare and initialize an array:
    int[,] theArray = new int[5, 10];
    ("The array has {0} dimensions.", );
  }
}

Output:

 The array has 2 dimensions.

Pass an array using ref and out
Like all out parameters, the out parameters of the array type must be assigned first before using them; that is, the callee must assign a value to them. For example:

static void TestMethod1(out int[] arr)
{
  arr = new int[10];  // definite assignment of arr
}

Like all ref parameters, the ref parameters of the array type must be explicitly assigned by the caller. Therefore, it is not necessary to explicitly assign values ​​by the called party. The ref parameter of the array type can be changed to the result of the call. For example, you can assign a null value to an array, or initialize it to another array. For example:

static void TestMethod2(ref int[] arr)
{
  arr = new int[10];  // arr initialized to a different array
}

The following two examples demonstrate the difference in usage of out and ref when passing an array to a method.
In this example, the array theArray is declared in the caller (Main method) and the array is initialized in the FillArray method. The array element will then be returned to the caller and displayed.

class TestOut
{
  static void FillArray(out int[] arr)
  {
    // Initialize the array:
    arr = new int[5] { 1, 2, 3, 4, 5 };
  }

  static void Main()
  {
    int[] theArray; // Initialization is not required

    // Pass the array to the callee using out:
    FillArray(out theArray);

    // Display the array elements:
    ("Array elements are:");
    for (int i = 0; i < ; i++)
    {
      (theArray[i] + " ");
    }

    // Keep the console window open in debug mode.
    ("Press any key to exit.");
    ();
  }
}

Output:

    Array elements are:
    1 2 3 4 5  

    
In this example, the array theArray is initialized in the caller (Main method) and passed it to the FillArray method by using the ref parameter. Update certain array elements in the FillArray method. The array element will then be returned to the caller and displayed.

class TestRef
{
  static void FillArray(ref int[] arr)
  {
    // Create the array on demand:
    if (arr == null)
    {
      arr = new int[10];
    }
    // Fill the array:
    arr[0] = 1111;
    arr[4] = 5555;
  }

  static void Main()
  {
    // Initialize the array:
    int[] theArray = { 1, 2, 3, 4, 5 };

    // Pass the array using ref:
    FillArray(ref theArray);

    // Display the updated array:
    ("Array elements are:");
    for (int i = 0; i < ; i++)
    {
      (theArray[i] + " ");
    }

    // Keep the console window open in debug mode.
    ("Press any key to exit.");
    ();
  }
}

Output:

    Array elements are:
    1111 2 3 4 5555