C#编程之使用ref和out关键字来传递数组对象的用法
白羽 2018-07-06 来源 :网络 阅读 1469 评论 0

摘要:本文将带你了解C#编程之使用ref和out关键字来传递数组对象的用法,希望本文对大家学C#/.Net有所帮助。


在 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#.NET的相关知识,希望对C#.NET有兴趣的朋友有所帮助。了解更多内容,请关注职坐标编程语言C#.NET频道!


本文由 @白羽 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程