Home  >  Article  >  Backend Development  >  What are the reference/ref parameters of array types in C#?

What are the reference/ref parameters of array types in C#?

WBOY
WBOYforward
2023-09-13 22:45:04896browse

C# 中数组类型的引用/ref 参数是什么?

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

Declare reference parameters-

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

Declare ref parameters of array type-

static void Display(ref int[] myArr)

The following example shows how to use ref parameters of 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();
   }
}

The above is the detailed content of What are the reference/ref parameters of array types in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete