Recall & Review
beginner
What are optional parameters in C#?
Optional parameters are method parameters that have a default value. When calling the method, you can skip these parameters, and the default value will be used.
Click to reveal answer
beginner
How do you declare an optional parameter in C#?
You declare an optional parameter by assigning it a default value in the method signature, for example:
void Greet(string name = "Guest")
Click to reveal answer
intermediate
Can optional parameters be placed anywhere in the parameter list?
No. Optional parameters must come after all required parameters in the method signature.
Click to reveal answer
beginner
What happens if you call a method without providing an optional parameter?
The method uses the default value specified for that optional parameter.
Click to reveal answer
beginner
Example: What is the output of this code?<br>
void PrintMessage(string message = "Hello") { Console.WriteLine(message); }<br>PrintMessage();The output is:<br>
Hello<br>Because the method was called without an argument, so the default value "Hello" is used.
Click to reveal answer
How do you specify an optional parameter in C#?
✗ Incorrect
Optional parameters are declared by assigning them a default value in the method signature.
Where must optional parameters be placed in the parameter list?
✗ Incorrect
Optional parameters must come after all required parameters in the method signature.
What happens if you call a method without providing an optional parameter?
✗ Incorrect
If an optional parameter is not provided, the method uses its default value.
Which of these is a valid method declaration with an optional parameter?
✗ Incorrect
Optional parameters must come after required ones, so 'int y = 10' after 'int x' is valid.
Can optional parameters be used with named arguments?
✗ Incorrect
Named arguments allow you to specify only some parameters, skipping optional ones.
Explain what optional parameters are and how to use them in C# methods.
Think about how you can make some method inputs not required.
You got /4 concepts.
Describe the rules about the position of optional parameters in a method's parameter list.
Consider what happens if you put an optional parameter before a required one.
You got /3 concepts.