当前位置:网站首页>c#可变参数params的介绍
c#可变参数params的介绍
2022-04-23 03:02:00 【dotNET跨平台】
c#可变参数params
的介绍
作为一个netUp主,今天在b站刷到了java的一个视频,可变参数的介绍,所以今天给大家介绍一下c#中可变参数params的使用介绍,
我们首先看一下官方解释: 使用 params 关键字可以指定采用数目可变的参数的params。参数类型必须是一维数组。在方法声明中的 params 关键字之后不允许有任何其他参数,并且在方法声明中只允许有一个 params 关键字。如果 params 参数的声明类型不是一维数组,则会发生编译器错误 params。使用 params 参数调用方法时,可以传入:
数组元素类型的参数的逗号分隔列表。
指定类型的参数的数组。
无参数。如果未发送任何参数,则 params 列表的长度为零。
例子
static void Main(string[] args)
{
string[] lst = new string[] { "张三", "李四", "王五" };
AddName(lst);
AddName("张三","李四","王五");
}
static void AddName(params string[] args)
{
string allName= string.Join(",", args);
}
可变参数就是像上面这样使用的,当我们在定义函数的时候对数组使用了关键字params,数组就会变成一个可变参数,在调用的时候我们传入的数据会形成一个数组,在调用的时候我们就可以传入无论多少个元素。
自己理解的可变参数的应用场景,可变参数(数组)不仅可以将数组传入,也可以在调用的时候用形参直接赋值的形式传入,在方法中还是会当成一个数组处理,这才是可变参数的先进性。
个人认为只是方便了调用该方法前,减少数组实例化的过程,由于up主在工作中几乎没有用过可变参数,可能理解有限有知道的小伙伴可以告诉我更丰富的使用场景,今天的介绍就到此结束了!
版权声明
本文为[dotNET跨平台]所创,转载请带上原文链接,感谢
https://blog.csdn.net/sd7o95o/article/details/124357635
边栏推荐
- Reverse a linked list < difficulty coefficient >
- MySQL complex query uses temporary table / with as (similar to table variable)
- Shell script learning notes - regular expressions
- 《信息系统项目管理师总结》第四章 项目成本管理
- Codeforces round 784 (Div. 4) (a - H)
- Detailed explanation of distributed things
- Opencv fills the rectangle with a transparent color
- Shell script learning -- practical case
- In redis cluster, the master node fails, and the IP changes after the master-slave switch. The client does not need to deal with it
- Traversal of l2-006 tree (middle and later order determination binary tree & sequence traversal)
猜你喜欢
tf. keras. layers. Timedistributed function
PDH optical transceiver 4-way E1 + 4-way 100M Ethernet 4-way 2m optical transceiver FC single fiber 20km rack type
Introduction to ACM [TSP problem]
The input of El input input box is invalid, and error in data(): "referenceerror: El is not defined“
Plug in for vscode
tf. keras. layers. Density function
Cloud computing learning 1 - openstack cloud computing installation and deployment steps with pictures and texts (Xiandian 2.2)
Huawei machine test question -- deformation of hj53 Yang Hui triangle
Actual combat of industrial defect detection project (IV) -- ceramic defect detection based on hrnet
Android 高阶面试必问:全局业务和项目的架构设计与重构
随机推荐
Error installing Mongo service 'mongodb server' on win10 failed to start
【Hcip】OSPF常用的6种LSA详解
First knowledge of C language ~ branch statements
Slave should be able to synchronize with the master in tests/integration/replication-psync.tcl
Android high-level interview must ask: overall business and project architecture design and reconstruction
Codeforces Round #784 (Div. 4) (A - H)题解
Huashu "deep learning" and code implementation: 01 Linear Algebra: basic concepts + code implementation basic operations
Classification of technology selection (2022)
[format] simple output (2)
Résumé du gestionnaire de projet du système d'information Chapitre VI gestion des ressources humaines du projet
Encapsulation of ele table
Learn regular expression options, assertions
Shell script learning notes - regular expressions
tf. keras. layers. Timedistributed function
Liunx foundation - zabbix5 0 monitoring system installation and deployment
Encapsulate components such as pull-down menu based on ele
tf. keras. layers. Inputlayer function
Log cutting - build a remote log collection server
基于多态的职工管理系统源码与一些理解
Introduction to ACM [inclusion exclusion theorem]