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

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

WBOY
Release: 2023-09-13 22:45:04
forward
987 people have browsed it

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) {}
Copy after login

Declare ref parameters of array type-

static void Display(ref int[] myArr)
Copy after login

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();
   }
}
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template