The use of ref and out keywords to pass array objects in C programming

  • 2021-09-05 00:42:03
  • OfStack

In C #, arrays are actually objects, not just addressable contiguous memory areas like in C and C + +. Array is the abstract base type for all array types. You can use the properties that Array has, as well as other class members. An example of this usage is to use the Length attribute to get the length of an 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 = numbers.Length;

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 dimension of an array.


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

Output:


 The array has 2 dimensions.

Passing arrays using ref and out
Like all out parameters 1, an out parameter of array type must be assigned before it can be used; That is, it must be assigned a value by the callee. For example:


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

Like all ref parameters 1, an ref parameter of an array type must be explicitly assigned by the caller. Therefore, there is no need for explicit assignment by the callee. You can change the ref parameter of the array type to the result of the call. For example, you can assign an array an null value 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 between out and ref when passing an array to a method.
In this example, the array theArray is declared in the caller (Main method) and initialized in the FillArray method. The array elements are then 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:
    System.Console.WriteLine("Array elements are:");
    for (int i = 0; i < theArray.Length; i++)
    {
      System.Console.Write(theArray[i] + " ");
    }

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

Output:


    Array elements are:
    1 2 3 4 5  


In this example, the array theArray is initialized in the caller (Main method) and passed to the FillArray method by using the ref parameter. Update some array elements in the FillArray method. The array elements are then 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:
    System.Console.WriteLine("Array elements are:");
    for (int i = 0; i < theArray.Length; i++)
    {
      System.Console.Write(theArray[i] + " ");
    }

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

Output:


    Array elements are:
    1111 2 3 4 5555


Related articles: