Only one paras keyword is allowed in a method declaration, and this keyword can only be the last one.
using System; /****************************** * Chapter:C#难点逐个击破(三) * Author:王洪剑 * Date:2010-1-16 * Blog:http://www.51obj.cn/ * Email:walkingp@126.com * Description:数组参数params的使用 * ***************************/ namespace TestParams { class Program { public static class ParamsClass { /// <summary> /// 两个参数,最后一个参数为数组参数 /// </summary> /// <param name="num"></param> /// <param name="args"></param> public static void ParamsMethod(int num,params string[] args) { foreach (string _args in args) { Console.WriteLine(_args); } Console.WriteLine("Total Num is " + num); Console.ReadKey(); } } static void Main(string[] args) { string[] strArr ={"Wang Hongjian","ChenChen","Dodo" }; int personNum = strArr.Length; ParamsClass.ParamsMethod(personNum, strArr); } } }
Operating effect:
Summarize ref, out and params. ref is the parameter in the reference method that affects the result; out is the parameter in the reference method that returns the result to the main method; and params is the variable parameter of the same type (i.e. Array) passed in parameters. In use, ref is used to call the data in the reference method. out is used to operate the main method using reference methods, while prarams is used when the parameter length is unknown.
The above is the content of C# difficulties one by one (3): params array parameters. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!