您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

如何在 C# 方法中使用 param 数组传递参数?

2025/6/11 8:24:29发布21次查看
声明方法时,您不确定作为形参传递的参数数量。 c# 参数数组(或参数数组)在这种时候会派上用场。
这就是如何使用参数 -
public int addelements(params int[] arr) {}
以下是完整的示例 -
示例using system;namespace program { class paramarray { public int addelements(params int[] arr) { int sum = 0; foreach (int i in arr) { sum += i; } return sum; } } class demo { static void main(string[] args) { paramarray app = new paramarray(); int sum = app.addelements(300, 250, 350, 600, 120); console.writeline("the sum is: {0}", sum); console.readkey(); } }}
输出the sum is: 1620
以上就是如何在 c# 方法中使用 param 数组传递参数?的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product