Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Variadic function
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===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>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)