zoukankan      html  css  js  c++  java
  • c#.net中参数修饰符ref,out ,params

    params 关键字可以指定在参数数目可变处采用参数的方法参数。

    在方法声明中的 params 关键字之后不允许任何其他参数,并且在方法声明中只允许一个 params 关键字。

    示例
    // cs_params.cs
    using System;
    public class MyClass
    {

    public static void UseParams(params int[] list)
    {
    for ( int i = 0 ; i < list.Length ; i++ )
    Console.WriteLine(list[i]);
    Console.WriteLine();
    }

    public static void UseParams2(params object[] list)
    {
    for ( int i = 0 ; i < list.Length ; i++ )
    Console.WriteLine(list[i]);
    Console.WriteLine();
    }

    public static void Main()
    {
    UseParams(1, 2, 3);
    UseParams2(1, 'a', "test");

    int[] myarray = new int[3] {10,11,12};
    UseParams(myarray);
    }
    }
    输出
    1
    2
    3

    1
    a
    test

    10
    11
    12

    ===============================================
    方法参数上的 out 方法参数关键字使方法引用传递到方法的同一个变量。当控制传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中。
    当希望方法返回多个值时,声明 out 方法非常有用。使用 out 参数的方法仍然可以返回一个值。一个方法可以有一个以上的 out 参数。
    若要使用 out 参数,必须将参数作为 out 参数显式传递到方法。out 参数的值不会传递到 out 参数。
    不必初始化作为 out 参数传递的变量。然而,必须在方法返回之前为 out 参数赋值。
    属性不是变量,不能作为 out 参数传递。
    如果两个方法的声明仅在 out 的使用方面不同,则会发生重载。不过,无法定义仅在 ref 和 out 方面不同的重载。例如,以下重载声明是有效的:
    class MyClass
    {
    public void MyMethod(int i) {i = 10;}
    public void MyMethod(out int i) {i = 10;}
    }
    而以下重载声明是无效的:
    class MyClass
    {
    public void MyMethod(out int i) {i = 10;}
    public void MyMethod(ref int i) {i = 10;}
    }
    有关传递数组的信息,请参见使用 ref 和 out 传递数组。
    示例
    // cs_out.cs
    using System;
    public class MyClass
    {
    public static int TestOut(out char i)
    {
    i = 'b';
    return -1;
    }

    public static void Main()
    {
    char i; // variable need not be initialized
    Console.WriteLine(TestOut(out i));
    Console.WriteLine(i);
    }
    }
    输出
    -1
    b

    ========================================
    方法参数上的 ref 方法参数关键字使方法引用传递到方法的同一个变量。当控制传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中。
    若要使用 ref 参数,必须将参数作为 ref 参数显式传递到方法。ref 参数的值被传递到 ref 参数。
    传递到 ref 参数的参数必须最先初始化。将此方法与 out 参数相比,后者的参数在传递到 out 参数之前不必显式初始化。
    属性不是变量,不能作为 ref 参数传递。
    如果两种方法的声明仅在它们对 ref 的使用方面不同,则将出现重载。但是,无法定义仅在 ref 和 out 方面不同的重载。例如,以下重载声明是有效的:
    class MyClass
    {
    public void MyMethod(int i) {i = 10;}
    public void MyMethod(ref int i) {i = 10;}
    }
    但以下重载声明是无效的:
    class MyClass
    {
    public void MyMethod(out int i) {i = 10;}
    public void MyMethod(ref int i) {i = 10;}
    }
    有关传递数组的信息,请参见使用 ref 和 out 传递数组。
    示例
    // cs_ref.cs
    using System;
    public class MyClass
    {
    public static void TestRef(ref char i)
    {
    // The value of i will be changed in the calling method
    i = 'b';
    }

    public static void TestNoRef(char i)
    {
    // The value of i will be unchanged in the calling method
    i = 'c';
    }

    // This method passes a variable as a ref parameter; the value of the
    // variable is changed after control passes back to this method.
    // The same variable is passed as a value parameter; the value of the
    // variable is unchanged after control is passed back to this method.
    public static void Main()
    {

    char i = 'a'; // variable must be initialized
    TestRef(ref i); // the arg must be passed as ref
    Console.WriteLine(i);
    TestNoRef(i);
    Console.WriteLine(i);
    }
    }
    输出
    b
    b
    参考资料:MSDN




    c#学习体会:使用 ref 和 out 传递数组(downmoon),希望与大家分享
    1、与所有的 out 参数一样,在使用数组类型的 out 参数前必须先为其赋值,即必须由接受方为其赋值。例如:

    public static void MyMethod(out int[] arr)
    {
       arr = new int[10];   // 数组arr的明确委派
    }
    2、与所有的 ref 参数一样,数组类型的 ref 参数必须由调用方明确赋值。因此不需要由接受方明确赋值。可以将数组类型的 ref 参数更改为调用的结果。例如,可以为数组赋以 null 值,或将其初始化为另一个数组。例如:

    public static void MyMethod(ref int[] arr)
    {
       arr = new int[10];   // arr初始化为一个新的数组
    }
    下面的两个示例说明 out 和 ref 在将数组传递给方法上的用法差异。

    示例 1
    在此例中,在调用方(Main 方法)中声明数组 myArray,并在 FillArray 方法中初始化此数组。然后将数组元素返回调用方并显示。


    using System;
    class TestOut
    {
       static public void FillArray(out int[] myArray)
       {
          // 初始化数组(必须):
          myArray = new int[5] {1, 2, 3, 4, 5};
       }

       static public void Main()
       {
          int[] myArray; // 初始化数组(不是必须的!)

          // 传递数组给(使用out方式的)调用方:
          FillArray(out myArray);

          // 显示数组元素
          Console.WriteLine("数组元素是:");
          for (int i=0; i < myArray.Length; i++)
             Console.WriteLine(myArray[i]);
       }
    }
    输出
    数组元素是:
    1
    2
    3
    4
    5
    示例 2
    在此例中,在调用方(Main 方法)中初始化数组 myArray,并通过使用 ref 参数将其传递给 FillArray 方法。在 FillArray 方法中更新某些数组元素。然后将数组元素返回调用方并显示。


    using System;
    class TestRef
    {
       public static void FillArray(ref int[] arr)
       {
          // 根据需要创建一新的数组(不是必须的)
          if (arr == null)
             arr = new int[10];
          // 否则填充数组,就可以了
          arr[0] = 123;
          arr[4] = 1024;
       }

       static public void Main ()
       {
          //初始化数组:
          int[] myArray = {1,2,3,4,5}; 

          // 使用ref传递数组:
          FillArray(ref myArray);

          //显示更新后的数组元素:
          Console.WriteLine("数组元素是:");
          for (int i = 0; i < myArray.Length; i++)
             Console.WriteLine(myArray[i]);
       }
    }
    输出
    数组元素是:
    123
    2
    3
    4
    1024

    源文地址:http://zhidao.baidu.com/question/2790061.html
    http://www.webshu.net/jiaocheng/programme/ASPNET/200606/1186.html

  • 相关阅读:
    微服务-分布式事务解决方案
    Python cannot import name 'Line' from 'pyecharts'
    powershell 基础
    Linux SSH config
    神奇的Python代码
    GitHub 中 readme 如何添加图片
    Linux 笔记(自用)
    Anaconda 安装 TensorFlow ImportError:DLL加载失败,错误代码为-1073741795
    Ubuntu 分辨率更改 xrandr Failed to get size of gamma for output default
    Git入门教程 Git教程入门
  • 原文地址:https://www.cnblogs.com/yiki/p/618307.html
Copyright © 2011-2022 走看看