Files
freeCodeCamp/guide/chinese/csharp/indeterminate-parameters/index.md
2018-10-16 21:32:40 +05:30

36 lines
890 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: Indeterminate parameters
localeTitle: 不确定参数
---
# 不确定参数
让我们想象一下我们需要编写一个参数数量可变的方法。我们怎么能这样做好吧C和其他语言有一个简单的方法;通过在方法的参数上使用`params`关键字,我们可以使用可变数量的参数调用该方法。
## 例
```
public static void Main (string[] args) {
// Call PrintParams with 3 parameters
PrintParams(1, 2, 3);
// Call PrintParams with 1 parameter
PrintParams(4);
}
public static void PrintParams(params int[] values)
{
// Iterate through parameters
for (int i = 0; i < values.Length; i++)
{
Console.WriteLine("Parameter {0} is {1}", i, values[i]);
}
}
```
## 输出:
```
> Parameter 0 is 1
> Parameter 1 is 2
> Parameter 2 is 3
> Parameter 0 is 4
```