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([], , , }) //传 ...
随机推荐
- 2535-springsecurity系列--关于授权角色“ROLE”前缀的问题
版本信息 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring ...
- Angular 新建项目错误:The Schematic workflow failed. See above
记录踩坑填坑,有不正之处请指出 错误 解决方法1 npm config set registry https://registry.npmjs.org/ 也可使用淘宝镜像 npm config set ...
- BZOJ1305/Luogu3153 [CQOI2009]dance跳舞 (network flow)
#define T 1001 #define S 0 struct Edge{ int nxt,pre,w; }e[500007]; int cntEdge,head[N]; inline void ...
- leetcode之二叉树
专题:二叉树遍历 987. 二叉树的垂序遍历 给你二叉树的根结点 root ,请你设计算法计算二叉树的 垂序遍历 序列. 对位于 (row, col) 的每个结点而言,其左右子结点分别位于 (row ...
- Express 使用 Cookie
在使用 Cookie 之前,需要给 Express 加载中间件,cookie-parser: npm i cookie-parser Express 使用中间件: import express fro ...
- Hadoop集群搭建的详细过程
Hadoop集群搭建 一.准备 三台虚拟机:master01,node1,node2 时间同步 1.date命令查看三台虚拟机时间是否一致 2.不一致时间同步:ntpdate ntp.aliyun.c ...
- docker hung住问题排查
背景:这个是之前遇到的老问题. # systemctl status lxcfs● lxcfs.service - FUSE filesystem for LXC Loaded: loaded (/u ...
- PHP代码审计学习-php安全基础
PHP代码审计-php安全基础 php.ini选项 register_globals php>=4.2.0,php.ini 的 register_globals 选项的默认值预设为 Off,当 ...
- 轻量级消息队列 Django-Q 轻度体验
前言 最近做的这个项目(基于Django),需要做个功能,实现定时采集车辆定位. 这让我想起来几年前那个OneCat项目,当时我用的是Celery这个很重的组件 Celery实在是太重了,后来我做公众 ...
- ERROR: column "xxxxxx" does not exist解决办法
今天在写PostgreSQL语句时候发现运行这个代码 SELECT t1.equipid, t2.equipname, t1.bigtype, t1.smalltype FROM pdw_gh_pro ...