Find The Largest Number in A Variable Length Argument List: Method Parameter
The document discusses finding the largest number in a variable length argument list in C#. It explains that the params keyword allows a method to take a variable number of arguments of a specified type. The FindLargestNumber method uses params to accept a variable number of long values, loops through them, and returns the largest one. It provides an example of calling the method with different numbers of arguments.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
30 views
Find The Largest Number in A Variable Length Argument List: Method Parameter
The document discusses finding the largest number in a variable length argument list in C#. It explains that the params keyword allows a method to take a variable number of arguments of a specified type. The FindLargestNumber method uses params to accept a variable number of long values, loops through them, and returns the largest one. It provides an example of calling the method with different numbers of arguments.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2
2.
Find the largest number in a variable length argument list
My next follow-up question is: Can you find the largest number in a variable length argument list? For those who come from the C/C++ background, this is the same as va_args list. In C#, this is referred by the the params keyword.
The params keyword lets you specify a method parameter that takes a variable number of arguments.
You can send a comma-separated list of arguments of the type specified in the parameter declaration, or an array of arguments of the specified type. You also can send no arguments.
No additional parameters are permitted after the params keyword in a method declaration, and only one params keyword is permitted in a method declaration.
For more details, refer to the MSDN article at http://msdn.microsoft.com/en- us/library/w5zay9db.aspx
/// <summary> /// Find the largest number in a variable list /// </summary> /// <param name="a">first parameter. Required.</param> /// <param name="numbers">variable list of longs</param> /// <returns></returns> public static long FindLargestNumber(long a, params long[] numbers) { // assume that the first value is the biggest long biggest = a;
// loop through all the arguments passed foreach (long x in numbers) { // check if the current number is the biggest if (x > biggest) biggest = x; }