The difference between parms ref out

 

params keyword method parameters can be specified using the parameters in the variable number of arguments.

In the method declaration of params not allow any other parameters after the keyword, and allows only one method declaration params keyword.

Examples

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

Export

1
2
3
1
a
test
10
11
12

ref:

Ref keyword method parameters on the method of method parameters refer to the same variable passed to the method. When control is passed back to the calling method, any changes made in the method parameters are reflected in the variable.
To use ref parameter, the parameters must be explicitly passed as an argument to the ref method. parameter value ref is transmitted to the parameter ref.
Parameters passed to the ref parameter must first be initialized. Compared with this out parameter, which parameter prior to passing out parameter need not be explicitly initialized.
Attribute is not a variable, not be passed as parameters ref.
If you declare two methods differ only in terms of their use of the ref, overloading will occur. However, it can not be defined only in terms of ref and out different overloaded. For example, the following statement is valid overloaded:
class MyClass
{
   public void MyMethod (I int) = 10 {I;}
   public void MyMethod (I REF int) = 10 {I;}
}
However, the following statement is invalid overload:
MyClass class
{
   public void MyMethod (I OUT int) = 10 {I;}
   public void MyMethod (I REF int) = 10 {I;}
}

out:

Out method parameter keyword on the method parameter method reference the same variable passed to the method. When control is passed back to the calling method, any changes made in the method parameters are reflected in the variable.
When you want a method to return multiple values, declared out method is useful. Out parameters method can still return a value. A method can have more than one out parameter.
To work out parameters a, as a method parameter must be explicitly transmitted to the out parameter. out parameter value is not transmitted to the out parameter.
Not necessary to initialize variables passed as out parameters. However, it must be assigned before the method returns to the out parameter.
Attribute is not a variable, not as an out parameter.
If you declare two methods differ only in terms of use out of, overloading occurs. However, it can not be defined only in terms of ref and out different overloaded. For example, the following statement is valid overloaded:
class MyClass
{
   public void MyMethod (I int) = 10 {I;}
   10 public void MyMethod (I OUT int) = {I;}
}
and the following statement is invalid overload:
MyClass class
{
   public void MyMethod (I OUT int) = 10 {I;}
   public void MyMethod (I REF int) = 10 {I;}
}


Reproduced in: https: //www.cnblogs.com/yitian/archive/2009/02/04/1383578.html

Guess you like

Origin blog.csdn.net/weixin_34321977/article/details/93710328