C# 是否支持可变数量的参数,以及如何支持?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9528276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 07:46:41  来源:igfitidea点击:

Does C# support a variable number of arguments, and how?

c#.netvariablesargumentsparams

提问by Rohit Vipin Mathews

Does C# support a variable number of arguments?

C# 是否支持可变数量的参数?

If yes, How does C# support variable no of arguments?

如果是,C# 如何支持变量 no 参数?

What are the examples?

有哪些例子?

How are variable arguments useful?

可变参数有什么用?

EDIT 1: What are the restrictions on it?

编辑 1:它有什么限制?

EDIT 2: The Question is not about Optional param But Variable Param

编辑 2:问题不是关于可选参数而是关于变量参数

采纳答案by hotS85

Yes. The classic example wourld be the params object[] args:

是的。经典的例子是params object[] args

//Allows to pass in any number and types of parameters
public static void Program(params object[] args)

A typical usecasewould be passing parameters in a command line environment to a program, where you pass them in as strings. The program has then to validate and assign them correctly.

一个典型的用例是将命令行环境中的参数传递给程序,在那里您将它们作为字符串传递。然后程序必须正确地验证和分配它们。

Restrictions:

限制:

  • Only one paramskeyword is permitted per method
  • It has to be the last parameter.
  • params每个方法只允许一个关键字
  • 它必须是最后一个参数。

EDIT: After I read your edits, I made mine. The part below also covers methods to achieve variable numbers of arguments, but I think you really were looking for the paramsway.

编辑:在我阅读你的编辑后,我做了我的。下面的部分还介绍了实现可变数量参数的方法,但我认为您确实在寻找params方法。



Also one of the more classic ones, is called method overloading. You've probably used them already a lot:

也是更经典的方法之一,称为方法重载。您可能已经经常使用它们:

//both methods have the same name and depending on wether you pass in a parameter
//or not, the first or the second is used.
public static void SayHello() {
    Console.WriteLine("Hello");
}
public static void SayHello(string message) {
    Console.WriteLine(message);
}


Last but not least the most exiting one: Optional Arguments

最后但并非最不重要的一个:可选参数

//this time we specify a default value for the parameter message
//you now can call both, the method with parameter and the method without.
public static void SayHello(string message = "Hello") {
    Console.WriteLine(message);
}

http://msdn.microsoft.com/en-us/library/dd264739.aspx

http://msdn.microsoft.com/en-us/library/dd264739.aspx

回答by Michael Stum

Yes, params:

是的,参数

public void SomeMethod(params object[] args)

params has to be the last argument and can be of any type. Not sure if it has to be an array or just an IEnumerable.

params 必须是最后一个参数并且可以是任何类型。不确定它是否必须是一个数组或只是一个 IEnumerable。

回答by recursive

C# supports variable length parameter arrays using the paramskeyword.

C# 支持使用params关键字的可变长度参数数组。

Here's an example.

这是一个例子。

public static void UseParams(params int[] list)
{
    for (int i = 0; i < list.Length; i++)
    {
        Console.Write(list[i] + " ");
    }
    Console.WriteLine();
}

There's more info here.

还有更多的信息在这里

回答by M.Babcock

I assume you mean a variable number of method parameters. If so:

我假设您的意思是可变数量的方法参数。如果是这样的话:

void DoSomething(params double[] parms)

(Or mixed with fixed parameters)

(或与固定参数混合)

void DoSomething(string param1, int param2, params double[] otherParams)

Restrictions:

限制:

  • They must all be the same type (or of a child type) as is true for arrays as well
  • There can only be one for each method
  • They must come last in the parameter list
  • 它们都必须是相同的类型(或子类型),对于数组也是如此
  • 每个方法只能有一个
  • 它们必须排在参数列表的最后

That's all I can think of at the moment though there could be others. Check the documentation for more information.

这就是我目前能想到的,虽然可能还有其他的。查看文档以获取更多信息。