C#编程中使用ref和out关键字来传递数组对象的用法

作者:goldensun 时间:2021-09-25 23:19:45 

在 C# 中,数组实际上是对象,而不只是像 C 和 C++ 中那样的可寻址连续内存区域。 Array 是所有数组类型的抽象基类型。 可以使用 Array 具有的属性以及其他类成员。 这种用法的一个示例是使用 Length 属性来获取数组的长度。 下面的代码将 numbers 数组的长度(为 5)赋给名为 lengthOfNumbers 的变量:


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

Array 类提供了许多其他有用的方法和属性,用于排序、搜索和复制数组。
示例
此示例使用 Rank 属性来显示数组的维数。


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);
 }
}

输出:


The array has 2 dimensions.

使用 ref 和 out 传递数组
与所有 out 参数一样,在使用数组类型的 out 参数前必须先为其赋值;即必须由被调用方为其赋值。例如:


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

与所有 ref 参数一样,数组类型的 ref 参数必须由调用方明确赋值。因此,不需要由被调用方明确赋值。可以将数组类型的 ref 参数更改为调用的结果。例如,可以为数组赋以 null 值,或将其初始化为另一个数组。例如:


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

下面两个示例演示了 out 与 ref 在将数组传递给方法时的用法差异。
在此示例中,在调用方(Main 方法)中声明数组 theArray,并在 FillArray 方法中初始化此数组。然后,数组元素将返回调用方并显示。


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

输出:


   Array elements are:
   1 2 3 4 5

    
在此示例中,在调用方(Main 方法)中初始化数组 theArray,并通过使用 ref 参数将其传递给 FillArray 方法。在 FillArray 方法中更新某些数组元素。然后,数组元素将返回调用方并显示。


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

输出:


   Array elements are:
   1111 2 3 4 5555
标签:C#,ref,out
0
投稿

猜你喜欢

  • Java静态代理和动态代理的深入讲解

    2023-04-12 03:07:33
  • c#字符串编码问题的处理解决

    2021-07-06 13:47:49
  • Java常用线程池原理及使用方法解析

    2022-02-22 17:00:23
  • java IO流将一个文件拆分为多个子文件代码示例

    2023-08-30 12:46:15
  • Java计算器核心算法代码实现

    2022-03-18 05:38:55
  • 浅析Spring Boot单体应用熔断技术的使用

    2022-05-10 02:37:08
  • C#泛型与非泛型性能比较的实例

    2022-01-31 17:26:51
  • springboot从application.properties中注入list, map方式

    2023-11-28 23:42:33
  • Java设计模式之建造者模式的示例详解

    2022-02-13 18:47:34
  • 史上最全图文讲解Java泛型

    2022-08-23 20:27:47
  • C# using三种使用方法

    2023-05-02 20:18:47
  • SpringBoot使用swagger生成api接口文档的方法详解

    2021-10-22 18:11:48
  • 基于Java实现Json文件转换为Excel文件

    2022-08-04 23:53:15
  • springboot整合Quartz实现动态配置定时任务的方法

    2023-03-08 22:13:10
  • Android Activity 不能被截屏的解决方法

    2021-06-28 08:37:35
  • Spring实现默认标签解析流程

    2021-07-29 10:07:55
  • Jetpack Compose常用组件详细介绍

    2022-10-16 19:42:58
  • idea2020.1无法自动加载maven依赖的jar包问题及解决方法

    2021-06-13 17:40:36
  • SpringBoot Jpa分页查询配置方式解析

    2023-03-02 10:04:02
  • Java如何根据不同系统动态获取换行符和盘分割符

    2022-02-27 10:33:10
  • asp之家 软件编程 m.aspxhome.com