C# 传不定参数
2024-09-08 17:28:29
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
最新文章
- Mac读取Andriod屏幕截图
- jackson官方快速入门文档
- 使用MyEclipse生成Java注释时,使用的Code Template
- 新建android工程的时候eclipse没有生成MainActivity和layout布局
- nrf51822裸机教程-GPIO
- 回到顶部js代码
- WinForm开发中针对TreeView控件改变当前选择节点的字体与颜色
- <;runtime>; 的 <;assemblyBinding>; 元素
- ASP.NET jQuery 随笔 显示RadioButtonList成员选中的内容和值
- ";《算法导论》之‘图’";:不带权二分图最大匹配(匈牙利算法)
- koa 中间件
- 【Selenium】【BugList5】chrom窗口未关闭,又新开窗口,报错:[8564:8632:0522/111825.341:ERROR:persistent_memory_allocator.cc(845)] Corruption detected in shared-memory segment.
- poj2456 Aggressive cows(二分查找)
- Python3 tkinter基础 Listbox for+insert 将list中元素导入listbox中
- Effective Java Methods Common to All Objects
- 使用Spring MockMVC对controller做单元测试(转)
- C# 出现base-64 字符数组的无效长度的解决办法
- CentOS 6.5 下HeartBeat的安装与配置
- java 面试大全
- Flask项目中数据库迁移的使用
热门文章
- 多表查询_子查询概述和多表查询_子查询情况1&;情况2&;情况3
- RabbitMQ延迟消息:死信队列 | 延迟插件 | 二合一用法+踩坑手记+最佳使用心得
- PHP几个常见不常用的方法
- YII的延迟加载
- 面试突击71:GET 和 POST 有什么区别?
- CC2530_ZigBee+华为云IOT:设计一套属于自己的冷链采集系统
- Apache DolphinScheduler 项目笔记 — 1. 问题定位和排查问题
- Luogu1993 小K的农场 (差分约束)
- 【Go实战基础】GO语言是什么,有哪些优势
- Blazor预研与实战