What is a reference/ref parameter of an array type in C#?


Declare the reference parameters using the ref keyword. A reference parameter is a reference to a memory location of a variable. When you pass parameters by reference, unlike value parameters, a new storage location is not created for these parameters.

Declare a ref parameter −

public void swap(ref int x, ref int y) {}

Declare a ref parameter of array type −

static void Display(ref int[] myArr)

The following is an example showing how to work with ref parameter of an array type in C# −

class TestRef {
   static void Display(ref int[] myArr) {
      if (myArr == null) {
         myArr = new int[10];
      }

      myArr[0] = 345;
      myArr[1] = 755;
      myArr[2] = 231;
   }

   static void Main() {
      int[] arr = { 98, 12, 65, 45, 90, 34, 77 };

      Display(ref arr);

      for (int i = 0; i < arr.Length; i++) {
         System.Console.Write(arr[i] + " ");
      }

      System.Console.ReadKey();
   }
}

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 21-Jun-2020

803 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements