A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 陈子斌 中级黑马   /  2012-7-12 09:51  /  1267 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

参数修饰符ref,out ,params的区别,不知道什么时候该用什么,好处在哪里,求高手解答下啊

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

4 个回复

倒序浏览
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

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

回复 使用道具 举报
1,out 关键字会导致参数通过引用来传递,在参数前面加上关键字out可以建立输出参数,告诉编译器这个变元按引用传入被调方法,被调用方法对调用者的原变量赋值;
2,ref 要求变量必须在传递之前进行初始化,而且ref关键字用于调用方法中已经初始化的变量;
3,params 关键字可以指定在参数数目可变处采用参数的方法参数。

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

回复 使用道具 举报
1, params 关键字可以指定在参数数目可变处采用参数的方法参数。
在方法声明中的 params 关键字之后不允许任何其他参数,并且在方法声明中只允许一个 params 关键字。
2, ref 关键字和 out 关键字意义差不多,也是传出参数。
只是在使用上面有所不同:传递到 ref 参数的参数必须最先初始化。这与 out 不同,后者的参数在传递之前不需要显式初始化。

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

回复 使用道具 举报
没听过这个 我是新手 来学习 嘿嘿 不知道楼主这是那里的知识啊 我现在刚学到静态啊
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马