C#方法笔记二:四种类型的参数

四种类型的参数主要有:
按值传递参数,按引用传递参数(ref),输出参数(out),数组参数(参数数组,params)
1,按值传递参数:
①值参数是通过将实参的值赋值给形参,来实现将值传递到方法。
②值参数中,实参也可以是任何计算结果满足类型要求的表达式,不一定的变量的格式。
代码①://只要参数是满足方法类型的表达式即可
class Program
    {
        static void Main(string[] args)
        {
           Program program = new Program();
          int x = 10;
          int y = 20;
          int sum = program.GetSum(x + y, y * x / 2); //此处x+y等
        }

        public int GetSum(int x, int y)
        {
            return x + y;
        }    
    }
 
2,按引用传递参数:ref
和前面的1讲到的“按值传递”相对应,传递的是参数的引用而不是参数的值。若参数在方法中已经赋值,那么在方法执行后不会改变参数的 值。
代码②:
 class Program
    {
        static void Main(string[] args)
        {
            Program pg = new Program();
            int x = 10;
            int y = 20;
            pg.GetValue(ref x, ref  y);
            Console.WriteLine("x={0},y={1}", x, y);

            Console.ReadLine();
          
        }

        public void GetValue(ref int x, ref int y)
        {
            x = 521;
            y = 520;
        }
    }
 运行结果为:
代码③:
 class Program
    {
        static void Main(string[] args)
        {
            Program program = new Program();
            int x = 10;
            int y = 20;
            program.ExChange(x, y);
            Console.WriteLine("x={0},y={1}", x, y);

            Console.ReadLine();
          
        }

        public void ExChange(int x, int y)
        {
            int temp;
            temp = x;
            x = y;
            y = temp;

        }
    }
运行结果为:,可见并没有实现我们想要的结果,实现x和y值的互换。
但是,加上ref关键字会实现我们想要的结果:
 代码④:
 class Program
    {
        static void Main(string[] args)
        {
            Program program = new Program();
            int x = 10;
            int y = 20;
            program.ExChange(ref x, ref y);
            Console.WriteLine("x={0},y={1}", x, y);

            Console.ReadLine();
          
        }

        public void ExChange(ref int x,ref  int y)
        {
            int temp;
            temp = x;
            x = y;
            y = temp;

        }
    }
运行结果为:,这也正说明了ref是按照引用传递的参数。
 
3,输出参数:out  (ref和out的区别,会在下一篇博客中讲到)
先尝试下代码:
 代码⑤:
   class Program
    {
        static void Main(string[] args)
        {
            Program pg = new Program();
            int x = 10;
            int y = 20;
            pg.GetValue(ref x, out y);
            Console.WriteLine("x={0},y={1}", x, y);

            Console.ReadLine();
          
        }

        public void GetValue(ref int x, out int y)
        {
            //  Console.WriteLine(x.ToString());
            //   Console.WriteLine(y.ToString());
            x = 521;
            y = 520;
        }

运行结果如下:
 
由此可见用ref和out得出的结果是一样的。但是,如果分别去掉注释,则会在去掉第2行注释时候出现如下错误:
使用了未赋值的out参数“y”.
这说明:
1,编译器允许在方法中的任意位置,任意时刻读取引用参数的 值;
2,编译器禁止在为输出参数赋值前读取它;
3,输出参数的初始值基本上是没意义的,因为它在使用前要被赋为新的值。(可以这么理解:如果把Console.WriteLine(y.ToString());这句代码写在y = 520;这句话后面则可以正确运行,并且运行结果为520;x=521,y=520
 
 
4,参数数组(params)
这个和“可选参数”有点像。
代码如下:
  代码⑥:
 1 class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             Program pg = new Program();
 6             pg.DoSth("a");
 7             pg.DoSth("a", 1, 2);
 8             pg.DoSth("a", 1, 2, 3);
 9             Console.ReadLine();
10           
11         }
12 
13         public void DoSth(string str, params int[] values)
14         {
15             if (null != values && values.Length > 0)
16             {
17                 for (int i = 0; i < values.Length; i++)
18                 {
19                     Console.WriteLine("{0},{1}", str, values[i]);
20                 }
21             }
22             else
23             {
24                 Console.WriteLine(str);
25             }
26         }
27     }

运行结果如下:

原文地址:https://www.cnblogs.com/yanhan/p/2877728.html