Recall & Review
beginner
What does the
params keyword do in C#?It allows a method to accept a variable number of arguments as an array, so you can pass zero or more arguments without explicitly creating an array.
Click to reveal answer
beginner
How do you declare a method that accepts any number of integers using
params?Use
public void MethodName(params int[] numbers). This means you can call the method with any number of integer arguments.Click to reveal answer
intermediate
Can a method have more than one
params parameter?No, a method can have only one
params parameter, and it must be the last parameter in the method signature.Click to reveal answer
beginner
What happens if you call a
params method without any arguments?The method receives an empty array for the
params parameter, so it still works without errors.Click to reveal answer
beginner
Example: What is the output of this code?<br>
void PrintNumbers(params int[] nums) { foreach(var n in nums) Console.Write(n + " "); }<br>PrintNumbers(1, 2, 3);The output is:
1 2 3 <br>The method prints each number passed as arguments separated by spaces.Click to reveal answer
What keyword allows a method to accept a variable number of arguments in C#?
✗ Incorrect
The
params keyword is used in C# to accept variable number of arguments.Where must the
params parameter be placed in the method signature?✗ Incorrect
The
params parameter must be the last parameter in the method signature.How many
params parameters can a method have?✗ Incorrect
A method can have only one
params parameter.What type must a
params parameter be?✗ Incorrect
The
params parameter must be declared as an array type.What happens if you call a
params method without arguments?✗ Incorrect
Calling a
params method without arguments passes an empty array to the method.Explain how the
params keyword works in C# and why it is useful.Think about how you can pass many values without creating an array yourself.
You got /4 concepts.
Describe the rules and limitations when using the
params keyword in method declarations.Focus on method signature and how the compiler expects the params parameter.
You got /4 concepts.