C# 相同类型的无止境方法参数

C# 相同类型的无止境方法参数,c#,methods,arguments,C#,Methods,Arguments,我记得我有一个红色的地方,你可以创建一个方法,它接受无休止的参数。问题是我不记得怎么做了。我记得是这样的: private void method(int arguments...) { //method body } 我肯定有“…”。我记得当你调用方法时,你可以这样调用它: 方法(3232)或方法(123,23,12) 如果有人知道我在说什么,请告诉我怎么做。你的意思是(对于vb.net) 对于c#它似乎是你会使用关键词: 您可以这样调用您的方法:method(1,2,3,4,5,6,7,8

我记得我有一个红色的地方,你可以创建一个方法,它接受无休止的参数。问题是我不记得怎么做了。我记得是这样的:

private void method(int arguments...)
{
//method body
}
我肯定有“
”。我记得当你调用
方法时,你可以这样调用它:
方法(3232)
方法(123,23,12)
如果有人知道我在说什么,请告诉我怎么做。

你的意思是(对于vb.net)

对于c#它似乎是

你会使用关键词:


您可以这样调用您的方法:
method(1,2,3,4,5,6,7,8,9)和数组将包含这些数字。params关键字必须位于数组中,如果它不是方法中的唯一参数,则必须是最后一个参数。只有一个参数可以具有param声明。

您正在寻找一个函数的无穷多个参数的c/c++定义。 你可以在这里看到-

实现此功能的简单方法如下:

1-例如,定义您的函数

void logging(const char *_string, int numArgs, ...)
第一个参数是要使用的字符串

第二个参数是要给出的无限个参数的数目。如果要计算开关中的占位符(如printf中的%d,%f),则不必使用此参数-提示:在循环中获取每个字符并查看它是否是占位符-

我想首先给出一个示例,说明如何调用这样的函数:

logging("Hello %0. %1 %2 %3", "world", "nice", "to", "meet you"); // infinite arguments are "world", "nice", ... you can give as much as you want
如你所见,我的占位符是数字。你想用什么就用什么

2-存在宏,用于初始化列表变量并获取参数值:

va_list arguments; // define the list
va_start(arguments, numArgs); // initialize it, Note: second argument is the last parameter in function, here numArgs

for (int x = 0; x < numArgs; x++) // in a loop
{ 
      // Note : va_arg(..) gets an element from the stack once, dont call it twice, or else you will get the next argument-value from the stack
      char *msg = va_arg(arguments, char *); // get "infinite argument"-value Note: Second parameter is the type of the "infinite argument".
      ... // Now you can do whatever you want - for example : search "%0" in the string and replace with msg
}
va_end ( arguments ); // we must end the listing
va_列表参数;//定义列表
va_start(参数,numArgs);//初始化它,注意:第二个参数是函数中的最后一个参数,这里是numArgs
for(int x=0;x
如果将每个占位符替换为无限参数值并打印新字符串,则应看到:

你好,世界。很高兴认识你


我希望这有助于

那是VB;问题被标记为C#.@phoog,注意到并添加了C#OK,但我为什么记得使用…?@Bosak:Java就是这样做的(尽管
在类型名称之后)。哦,你可能是对的。我不懂Java,但有一次我看了一段关于Java的视频,因为它很像C语言,我把它弄糊涂了。
va_list arguments; // define the list
va_start(arguments, numArgs); // initialize it, Note: second argument is the last parameter in function, here numArgs

for (int x = 0; x < numArgs; x++) // in a loop
{ 
      // Note : va_arg(..) gets an element from the stack once, dont call it twice, or else you will get the next argument-value from the stack
      char *msg = va_arg(arguments, char *); // get "infinite argument"-value Note: Second parameter is the type of the "infinite argument".
      ... // Now you can do whatever you want - for example : search "%0" in the string and replace with msg
}
va_end ( arguments ); // we must end the listing