C# Params简介(附实例)

431 阅读3分钟

C# Params

C# Params简介

Params是C#中一个非常重要的关键字。当我们想把参数的数量作为变量时,我们使用param。所以当开发者不知道将要使用的参数数量时,就会使用它。在C#的Params关键字之后,函数中不允许有额外的param。如果我们不传递任何参数,那么params的长度将保持为零。我们可以发送逗号分隔的值或数组。

关键字:params

using System;
namespace Examples {
class Test {
// function containing params parameters
public static int Addittion(params int[] ListNumbers)
{
int total = 0;
// foreach loop
foreach(int i in ListNumbers)
{
total += i;
}
return total;
}
// Driver Code
static void Main(string[] args)
{
// Calling function by passing 5
// arguments as follows
int y = Addittion (12,13,10,15,56);
// Displaying result
Console.WriteLine(y);
}
}
}

输出

C# Params 1-7

有params 关键字

static public int add(params int[] args)
{
int add1 = 0;
foreach (var item in args)
add1= add1+item + 2;
return add1;
}

没有params 关键字

static public int add(int[] args)
{
int add = 0;
foreach (var item in args)
add1 = add1+item + 2;
return add1;
}

C#参数的例子

下面的例子展示了我们如何在C#中实现params。

例子 #1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Params
{
class Demo
{
public void Show(params int[] num) // Params Paramater
{
for (int a = 0; a < num.Length; a++)
{
Console.WriteLine(num[a]);
}
}
static void Main(string[] args) // main function
{
Demo program = new Demo();  // Creating Object
program.Show(20, 4, 64, 3, 20, 2, 14);  // Passing arguments of variable length
Console.ReadLine();
}
}
}

在上面的例子中,使用了一个param关键字,允许任何类型和数量。然后,在创建对象后,我们传递几个参数来显示。

输出

creating the object

例子 #2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Params
{
class Demo
{
public void Show(params object[] val) // Params Paramater
{
for (int a = 0; a < val.Length; a++)
{
Console.WriteLine(val[a]);
}
}
static void Main(string[] args) // main method
{
Demo program = new Demo(); // Creating Object
program.Show("Javascript", "Dotnet", 11, 10.50, "Param", 'h',"Example"); // Passing arguments of variable length
Console.ReadLine();
}
}
}

在上面的例子中,使用了一个param关键字,允许任何类型和数量的类型。在创建一个对象后,我们传递几个参数来显示。这里你可以看到不同数据类型的参数。

输出

number of types

例子 #3

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Params
{
class Demo
{
public static int Addition(params int[] num) // params parameter
{
int add = 0;
// foreach loop
foreach (int a in num)
{
add += a;
}
return add;
}
static void Main(string[] args)
{
int x = Addition(23, 45, 2, 36, 76);  // call function
// Displaying result
Console.WriteLine(x);
Console.ReadLine();
}
}
}

在上面的例子中,使用了一个数组,不需要提到数组的大小,因为使用了一个param关键字,它将允许任何类型和数量的参数。 数字将采用以下格式。

[0] 25
[1] 45
[2] 2
[3] 36
[4] 76

输出

C# params 1-3

例子 #4

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Params
{
class Demo
{
static void Main()
{
// Call params method with five integer type arguments
int cal1 = calParameters(7);
int cal2 = calParameters(11, 23);
int cal3 = calParameters(46, 8, 45);
int cal4 = calParameters(23, 31, 21, 45);
int cal5 = calParameters(12, 12, 54, 76);
// display result of each calculations
Console.WriteLine(cal1);
Console.WriteLine(cal2);
Console.WriteLine(cal3);
Console.WriteLine(cal4);
Console.WriteLine(cal5);
Console.ReadLine();
}
static int calParameters(params int[] values)
{
int sum = 0;
foreach (int value in values)    // foreach loop and sum of integers
{
sum += value;
}
return sum;
}
}
}

在上面的例子中,有五个参数使用了param关键字。所有的参数都是整数类型。一个foreach循环被用来显示每个参数的总和。

输出

Foreach loop

它与数组有什么不同?

public void test(params int[] args) { }
test(); // no compile-time error, args will be empty

但如果你使用一个数组:

public void test(int[] args) { }
test(); // error: No overload for 'Foo' takes 0 arguments

那么,如果我们不传递任何参数,那么params的长度将保持为零。Param关键字必须是参数列表中的最后一个,否则就会出现错误。

例子

public void test(params int[] args,int value) { }

这个声明是不允许的。