Content deleted Content added
Tag: Reverted |
m Reverted edit by 2603:7000:C13C:6B1D:78BA:5624:F2B9:75F3 (talk) to last version by Jarble |
||
Line 55:
# {{code|va_copy}} takes two arguments, both of them {{code|va_list}} objects. It clones the second (which must have been initialised) into the first. Going back to the "scan the variable arguments more than once" example, this could be achieved by invoking {{code|va_start}} on a first {{code|va_list}}, then using {{code|va_copy}} to clone it into a second {{code|va_list}}. After scanning the variable arguments a first time with {{code|va_arg}} and the first {{code|va_list}} (disposing of it with {{code|va_end}}), the programmer could scan the variable arguments a second time with {{code|va_arg}} and the second {{code|va_list}}. {{code|va_end}} needs to also be called on the cloned {{code|va_list}} before the containing function returns.
===In C#===
[[C Sharp (programming language)|C#]] describes variadic functions using the {{code|params}} keyword. A type must be provided for the arguments, although {{code|object[]}} can be used as a catch-all. At the calling site, you can either list the arguments one by one, or hand over a pre-existing array having the required element type. Using the variadic form is [[Syntactic sugar]] for the latter.
<syntaxhighlight lang="c#" highlight="5,16-17,19">
using System;
class Program
{
static int Foo(int a, int b, params int[] args)
{
// Return the sum of the integers in args, ignoring a and b.
int sum = 0;
foreach (int i in args)
sum += i;
return sum;
}
static void Main(string[] args)
{
Console.WriteLine(Foo(1, 2)); // 0
Console.WriteLine(Foo(1, 2, 3, 10, 20)); // 33
int[] manyValues = new int[] { 13, 14, 15 };
Console.WriteLine(Foo(1, 2, manyValues)); // 42
}
}
</syntaxhighlight>
===In C++===
|