c# 的关键字 params,out,ref

params:

params在参数中有且只能出现一次,用一个数组代替参数,并且不能有其他参数;

ex:

static void Main(string[] args)
{
object[] arr = new object[3]{"a","b","c"}; useParmas(arr);  } public static void useParams(params object[] list) { for(int i = 0 ; i<list.length ; i++) { Console.WriteLine(list[i]); } }

ref:

1.若要使用ref,则参数及调用方法时都需要显示使用ref;

2.传递的值必须要先初始化;

3.属性不是变量因此不能作为参数传递;

static void Main(string[] args)
{
    int i = 10;  //2.传递的值必须要先初始化
 
  Console.WriteLine(i);  //10;
useRef(
ref i);  //110; /1.显示的使用ref } public static void useRef(ref int i)  //1.显示的使用ref { i += 100;
Console.WriteLine(i); }

out:

1.out在使用时 传递的值不需要初始化

2.需要调用方法以便在方法返回之前赋值

static void Main(string[] args)
 {
       int a; string b; bool c;

        useParams(out a, out b, out c);

        Console.WriteLine("int is {0}", a);  //20
        Console.WriteLine("string is {0}", b);  //bbb
        Console.WriteLine("bool is {0}", c);  //true
}

public static void useParams(out int a , out string b , out bool c)
{
      a = 20;
      b = "bbb";
      c = true;
}
原文地址:https://www.cnblogs.com/SmileCN/p/3058636.html