image

编辑人: 桃花下浅酌

calendar2025-05-09

message7

visits338

params 有什么用?

params 关键字在方法成员的参数列表中使用,为该方法提供了参数个数可变的能力

它在只能出现一次并且不能在其后再有参数定义,之前可以

示例:

using System;

using System.Collections.Generic;

using System.Text;

namespace ConsoleApplication1

{

class App

{

//第一个参数必须是整型,但后面的参数个数是可变的。

//而且由于定的是object数组,所有的数据类型都可以做为参数传入

public static void UseParams(int id, params object[] list)

{

Console.WriteLine(id);

for (int i = 0; i < list.Length; i++)

{

Console.WriteLine(list[i]);

}

}

static void Main()

{

//可变参数部分传入了三个参数,都是字符串类型

UseParams(1, "a", "b", "c");

//可变参数部分传入了四个参数,分别为字符串、整数、浮点数和双精度浮点数数组

UseParams(2, "d", 100, 33.33, new double[] { 1.1, 2.2 });

Console.ReadLine();

}

}

}

结果:
1
a
b
c
2
d
100
33.33
System.Double[]



喵呜刷题:让学习像火箭一样快速,快来微信扫码,体验免费刷题服务,开启你的学习加速器!

创作类型:
原创

本文链接:params 有什么用?

版权声明:本站点所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明文章出处。
分享文章
share