c#参数传递

 

一、使用params 关键字后可以在调用时用逗号分隔开,用 params  还可以动态传递参数,参数数量不受限制,在数据库读写层就有类似。

//param 关键字可以在调用时用逗号分隔开
public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            
            
            var result=GetTotal(1,2,3);
            Console.Write("Press any key to continue . . . "+result);
    
            Console.ReadKey(true);
        }
        public static int GetTotal(params int[] inputArry)
        {
            int value=0;
            for(int i=0;i<inputArry.Length;i++)
                value+=inputArry[i];
            return value;
        }

二、参数传递按设置默认值

  public static int GetTotal(int  y=0,int x)
        {
           value=x+y;//y可以不传参,按默认0算
            return value;
        }

 三、使用命名参数传递不需要考虑参数顺序:

public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");    
            var result=GetTotal(x:2,y:3);//命名参数传递
            Console.Write("Press any key to continue . . . "+result);
            Console.ReadKey(true);
        }
        public static int GetTotal(int x,int y)
        {
            int value=0;
            value=x+y;
            return value;
        }
原文地址:https://www.cnblogs.com/musexiaoluo/p/6457872.html