zoukankan      html  css  js  c++  java
  • [转]C#中out 及 ref 区别

    方法参数上的 out 方法参数关键字使方法引用传递到方法的同一个变量。
    当控制传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中。
    当希望方法返回多个值时,声明 out 方法非常有用。
    使用 out 参数的方法仍然可以返回一个值。一个方法可以有一个以上的 out 参数。
    若要使用 out 参数,必须将参数作为 out 参数显式传递到方法。out 参数的值不会传递到 out 参数。
    不必初始化作为 out 参数传递的变量。然而,必须在方法返回之前为 out 参数赋值。
    属性不是变量,不能作为 out 参数传递。

    方法参数上的 ref 方法参数关键字使方法引用传递到方法的同一个变量。
    当控制传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中。
    若要使用 ref 参数,必须将参数作为 ref 参数显式传递到方法。
    ref 参数的值被传递到 ref 参数。 传递到 ref 参数的参数必须最先初始化。
    将此方法与 out 参数相比,后者的参数在传递到 out 参数之前不必显式初始化。
    属性不是变量,不能作为 ref 参数传递。

    两者都是按地址传递的,使用后都将改变原来的数值。
    ref可以把参数的数值传递进函数,但是out是要把参数清空
    就是说你无法把一个数值从out传递进去的,out进去后,参数的数值为空,所以你必须初始化一次。
    两个的区别:ref是有进有出,out是只出不进。

    代码实例如下:

    1 namespace TestOutAndRef
    2 {
    3     class TestApp
    4      {
    5
    6  static void outTest(out int x, out int y)
    7  {//离开这个函数前,必须对x和y赋值,否则会报错。
    8   //y = x;
    9   //上面这行会报错,因为使用了out后,x和y都清空了,需要重新赋值,即使调用函数前赋过值也不行
    10   x = 1;
    11   y = 2;
    12  }
    13  static void refTest(ref int x, ref int y)
    14  {
    15   x = 1;
    16   y = x;
    17  }
    18
    19
    20 static public void OutArray(out int[] myArray)
    21 {
    22      // Initialize the array:
    23       myArray = new int[5] { 1, 2, 3, 4, 5 };
    24 }
    25 public static void FillArray(ref int[] arr)
    26 {
    27      // Create the array on demand:
    28      if (arr == null)
    29           arr = new int[10];
    30      // Otherwise fill the array:
    31       arr[0] = 123;
    32       arr[4] = 1024;
    33 }
    34
    35
    36  public static void Main()
    37  {
    38   //out test
    39   int a,b;
    40   //out使用前,变量可以不赋值
    41   outTest(out a, out b);
    42   Console.WriteLine("a={0};b={1}",a,b);
    43   int c=11,d=22;
    44   outTest(out c, out d);
    45   Console.WriteLine("c={0};d={1}",c,d);
    46
    47   //ref test
    48   int m,n;
    49   //refTest(ref m, ref n);
    50   //上面这行会出错,ref使用前,变量必须赋值
    51
    52   int o=11,p=22;
    53   refTest(ref o, ref p);
    54   Console.WriteLine("o={0};p={1}",o,p);
    55
    56
    57
    58   int[] myArray1; // Initialization is not required
    59
    60   // Pass the array to the callee using out:
    61    OutArray(out myArray1);
    62
    63   // Display the array elements:
    64    Console.WriteLine("Array1 elements are:");
    65   for (int i = 0; i < myArray1.Length; i++)
    66        Console.WriteLine(myArray1[i]);
    67
    68   // Initialize the array:
    69   int[] myArray = { 1, 2, 3, 4, 5 };
    70
    71   // Pass the array using ref:
    72    FillArray(ref myArray);
    73
    74   // Display the updated array:
    75    Console.WriteLine("Array elements are:");
    76   for (int i = 0; i < myArray.Length; i++)
    77        Console.WriteLine(myArray[i]);
    78 }
    79      }
    80
    81 }

  • 相关阅读:
    Tarjan算法求双连通分量
    Tarjan
    前端技术实现js图片水印
    记录一下ionic canvas图片,还有canvas里面的图片跨域的问题
    ionic cordova screenshot 使用和操作
    关于ionic2 更新到ionic3 后组件不能用的解决方案
    背景图处理,这是个好东西记录一下
    radio样式的写法,单选和多选如何快速的改变默认样式,纯CSS,
    ionic使用cordova插件中的Screenshot截图分享功能
    ionic中执行pop返回上一个页面,还需要执行操作
  • 原文地址:https://www.cnblogs.com/hateyoucode/p/1918767.html
Copyright © 2011-2022 走看看