C# 传不定参数
1 public class MyClass
2 {
3 public static void UseParams(params int[] list)
4 {
5 for (int i = 0; i < list.Length; i++)
6 {
7 Console.Write(list[i] + " ");
8 }
9 Console.WriteLine();
10 }
11
12 public static void UseParams2(params object[] list)
13 {
14 for (int i = 0; i < list.Length; i++)
15 {
16 Console.Write(list[i] + " ");
17 }
18 Console.WriteLine();
19 }
20
21 static void Main()
22 {
23 // You can send a comma-separated list of arguments of the
24 // specified type.
25 UseParams(1, 2, 3, 4);
26 UseParams2(1, 'a', "test");
27
28 // A params parameter accepts zero or more arguments.
29 // The following calling statement displays only a blank line.
30 UseParams2();
31
32 // An array argument can be passed, as long as the array
33 // type matches the parameter type of the method being called.
34 int[] myIntArray = { 5, 6, 7, 8, 9 };
35 UseParams(myIntArray);
36
37 object[] myObjArray = { 2, 'b', "test", "again" };
38 UseParams2(myObjArray);
39
40 // The following call causes a compiler error because the object
41 // array cannot be converted into an integer array.
42 //UseParams(myObjArray);
43
44 // The following call does not cause an error, but the entire
45 // integer array becomes the first element of the params array.
46 UseParams2(myIntArray);
47 }
48 }
链接:https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/params
C# 传不定参数的更多相关文章
- SE6 不定参数和默认参数详解和使用细节
在SE5以前我们通常通过arguments类数组对象来引用不定形参,SE6则使用了一种叫做不定参数的写法,比起隐式的arguments要直观的多. 不定参数使用...参数名来指定一个不定参数,参数名指 ...
- Java不定参数Object… obj 和 Object[] 的区别
Java不定参数Object… obj 和 Object[] 的区别 简述: java中方法重载可以实现参数不同自动匹配对应方法.但现实中也存在这种问题.普通传参对于形如下面的方法,却显得臃肿而失优雅 ...
- C和C++中的不定参数
在初学C的时候,我们都会用到printf函数来写Hello World的程序.在我们看printf函数的声明时,会看到类似于下面代码 int printf(const char * __restric ...
- C语言不定参数
最近,遇到一个c语言的不定参数问题.其实,对于c语言的不定参数问题,只需要三个函数就可以搞定了.这三个函数的头文件是<stdarg.h>,其实下面的三个函数都是一个宏定义(macro). ...
- args 、kwargs不定参数通过列表、元组、字典传递
*args .**kwargs是不定参数的表达方法,一般用于函数形参.*args .**kwargs 作为传递参数的函数,在使用过程中,可对*args .**kwargs多个输入,但是效率或者比较麻烦 ...
- 不定参数(rest 参数 ...)
不定参数 如何实现不定参数 使用过 underscore.js 的人,肯定都使用过以下几个方法: _.without(array, *values) //返回一个删除所有values值后的array副 ...
- 从js向Action传中文参数出现乱码问题的解决方法
Action获取jsp表单中的中文参数,只要整个项目都采用UTF-8编码格式都不会出现乱码问题:但JSP中用到JS,并从JS向Action传中文参数,就会出现中文乱的现象 做项目的时候,发现A ...
- (转) jsp页面 URL传中文参数到Action里面出现乱码
jsp页面 URL传中文参数到Action里面出现乱码,方法如下: 第一种:在Action中用 new String(str.getBytes("ISO8859_1"), &quo ...
- golang中不定参数与数组切片的区别
package main import "fmt" func main() { myfunc1(, , , ) //传递不定数量的参数 myfunc2([], , , }) //传 ...
随机推荐
- linux学习系列--初识Linux系统
### 认识Linux- Linux是一种类UNIX的系统,Unix是1965年在贝尔实验室开发的一个项目,用来开发操作系统- Linux之父-Linus Torvalds在1991年10月5日,他在 ...
- 2505-springboot使用spring.profiles.active来分区配置
参考文献: spring boot 入门 使用spring.profiles.active来分区配置 http://www.leftso.com/blog/111.html 很多时候,我们项目在开发环 ...
- jQuery基础入门(一)
jQuery是什么? jQuery是一个JavaScript常用的工具函数库.jQuery是一个轻量级的"写的少,做的多"的JavaScript库. jQuery当中包含有以下一些 ...
- Luogu1769 淘汰赛制_NOI导刊2010提高(01)(概率DP)
第\(i\)次位置在\(pos_0 / 2^{i - 1}\) #include <iostream> #include <cstdio> #include <cstri ...
- 快速创建springboot项目,并进行增删改
创建普通maven项目,pom依赖如下 <parent> <artifactId>spring-boot-starter-parent</artifactId> & ...
- docker启动失败问题
内核3.10,systemctl start docker 被阻塞,没有返回,查看状态为启动中. 某兄弟机器安装docker之后,发现systemctl start docker的时候阻塞,由于排查走 ...
- CAD二次开发---关于JoinEntity出现eNotApplicable的问题
作者在使用JoinEntity时出现eNotApplicable的问题,查阅了Autodesk论坛的相关帖子,发现大多数人都有遇到这个问题,但没有找到合适的解决方法,可能原因是进行Join时两Curv ...
- HDU 6222 Heron and His Triangle (pell 方程)
题面(本人翻译) A triangle is a Heron's triangle if it satisfies that the side lengths of it are consecutiv ...
- 自定义View3-水波纹扩散(仿支付宝咻一咻)实现代码、思想
PS:自定义view篇-水波纹实现 效果:水波纹扩散 场景:雷达.按钮点击效果.搜索等 实现:先上效果图,之前记得支付宝有一个咻一咻,当时就是水波纹效果,实现起来一共两步,第一画内圆,第二画多个外圆, ...
- HC32L110(四) HC32L110的startup启动文件和ld连接脚本
目录 HC32L110(一) HC32L110芯片介绍和Win10下的烧录 HC32L110(二) HC32L110在Ubuntu下的烧录 HC32L110(三) HC32L110的GCC工具链和VS ...