C# params关键字

来源:互联网 发布:windows时间服务未运行 编辑:程序博客网 时间:2024/06/04 18:44

使用 params 关键字可以指定采用数目可变的参数的方法参数

可以发送参数声明中所指定类型的逗号分隔的参数列表或指定类型的参数数组。还可以不发送参数。 如果未发送任何参数,则 params 列表的长度为零。

在方法声明中的 params 关键字之后不允许任何其他参数,并且在方法声明中只允许一个 params 关键字。

下面的示例演示可向 params 参数发送参数的各种方法。

    public class MyClass    {        public static void UseParams(params int[] list)        {            for (int i = 0; i < list.Length; i++)            {                Console.Write(list[i] + " ");            }            Console.WriteLine();        }        public static void UseParams2(params object[] list)        {            for (int i = 0; i < list.Length; i++)            {                Console.Write(list[i] + " ");            }            Console.WriteLine();        }        static void Main()        {            // You can send a comma-separated list of arguments of the             // specified type.            UseParams(1, 2, 3, 4);            UseParams2(1, 'a', "test");            // A params parameter accepts zero or more arguments.            // The following calling statement displays only a blank line.            UseParams2();            // An array argument can be passed, as long as the array            // type matches the parameter type of the method being called.            int[] myIntArray = { 5, 6, 7, 8, 9 };            UseParams(myIntArray);            object[] myObjArray = { 2, 'b', "test", "again" };            UseParams2(myObjArray);            // The following call causes a compiler error because the object            // array cannot be converted into an integer array.            //UseParams(myObjArray);            // The following call does not cause an error, but the entire             // integer array becomes the first element of the params array.            UseParams2(myIntArray);        }    }    /*    Output:        1 2 3 4        1 a test                       5 6 7 8 9        2 b test again        System.Int32[]    */


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 入盆了又出来了怎么办 大腿前突小腿后怎么办 英语不好高二了怎么办 断奶后又复吸怎么办 招联金融综合评定不足怎么办 git本地分支比远程高怎么办 娃儿上嘴唇里面破了怎么办 错过了各大招聘怎么办 优秀团员申请表没有获奖情况怎么办 大学生毕业学生登记表涂改了怎么办 c语言挂科了怎么办 吸入腐蚀性气体鼻子流血了怎么办 腰劳损痛的厉害怎么办 两个宝宝斜颈左边力量差怎么办 姿势不正确引起的习惯性斜颈怎么办 一岁宝宝有点斜颈怎么办 六个月宝宝有点斜颈怎么办 5月宝宝有点斜颈怎么办 半岁宝宝头偏怎么办 肌性斜颈成年后怎么办 5个月孩子脖子歪怎么办 宝宝一岁多头歪怎么办 四个月宝宝歪脖子怎么办 1岁宝宝脖子歪怎么办 宝宝脖子是歪的怎么办 小孩脖子睡歪了怎么办 脸部三角区肿了怎么办 面部三角区挤了怎么办 胳膊扭着了肿了怎么办 多囊卵巢综合症治不好怎么办 胳膊受了风发麻怎么办 胳膊抻筋了很疼怎么办 腰抻了怎么办最有效 孩子胳膊抻着了怎么办 胳膊伤筋了疼怎么办 宝宝胳膊抻了疼怎么办 练完普拉提头晕怎么办 生完孩子腰背疼怎么办 宝宝不肯把屎尿怎么办 存的电话删除了怎么办 脚踝的韧带断了怎么办