📖 Scenario: You are creating a simple greeting program that can say hello to a person. Sometimes you want to include their title, and sometimes you just want to say hello with their name only.
🎯 Goal: Build a method called Greet that uses optional parameters to greet a person with or without their title.
📋 What You'll Learn
Create a method called Greet with two parameters: string name and an optional string title.
The optional parameter title should have a default value of an empty string.
If title is provided, the greeting should include it before the name.
If title is not provided, the greeting should just use the name.
Call the Greet method twice: once with both name and title, and once with only name.
Print the greetings to the console.
💡 Why This Matters
🌍 Real World
Optional parameters help make your methods flexible and easier to use when some information is not always needed.
💼 Career
Understanding optional parameters is important for writing clean, maintainable code in many C# applications, including web, desktop, and mobile development.
Progress0 / 4 steps
1
Create the Greet method with parameters
Write a method called Greet that takes two parameters: string name and string title. For now, do not make title optional. The method should return a string that says "Hello, " followed by the title and name separated by a space.
C Sharp (C#)
Hint
Define a method with two parameters and return the greeting string combining them.
2
Make the title parameter optional
Modify the Greet method so that the title parameter is optional with a default value of an empty string "".
C Sharp (C#)
Hint
Add = "" after the title parameter to make it optional.
3
Add logic to handle empty title
Change the Greet method so that if title is an empty string, it returns "Hello, " followed by just the name. Otherwise, it returns "Hello, " followed by the title and name separated by a space.
C Sharp (C#)
Hint
Use an if statement to check if title is empty and return the correct greeting.
4
Call Greet method and print greetings
In the Main method, call Greet with name as "Alice" and title as "Dr.". Then call Greet with only name as "Bob". Print both returned greetings using Console.WriteLine.
C Sharp (C#)
Hint
Call Greet with both parameters and with only the name. Use Console.WriteLine to print the results.
Practice
(1/5)
1. What is the main purpose of optional parameters in C# methods?
easy
A. To force all arguments to be passed every time
B. To make methods run faster
C. To allow skipping some arguments by providing default values
D. To declare variables inside methods
Solution
Step 1: Understand optional parameters
Optional parameters let you give default values so callers can skip them.
Step 2: Compare with other options
Options A, C, and D do not describe optional parameters correctly.
Final Answer:
To allow skipping some arguments by providing default values -> Option C
Quick Check:
Optional parameters = skip arguments with defaults [OK]
Hint: Optional parameters have default values to skip arguments [OK]
Common Mistakes:
Thinking optional parameters speed up code
Believing all arguments must always be passed
Confusing optional parameters with local variables
2. Which of the following method signatures correctly uses optional parameters in C#?
easy
A. void Print(string message, int count = 1)
B. void Print(int count, string message)
C. void Print(int count = 1, string message)
D. void Print(string message = "Hello", int count)
Solution
Step 1: Recall optional parameter rules
Optional parameters must come after all required parameters.
Step 2: Check each signature
A: optional first - invalid. B: no default value, so no optional parameter. C: optional first - invalid. D: optional last - correct.
Final Answer:
void Print(string message, int count = 1) -> Option A
Quick Check:
Optional parameters come last = D [OK]
Hint: Optional parameters must be last in method signature [OK]
Common Mistakes:
Placing optional parameters before required ones
Missing default values for optional parameters
Confusing parameter order rules
3. What is the output of this C# code?
void Greet(string name = "Guest", int times = 1) {
for (int i = 0; i < times; i++) {
Console.WriteLine($"Hello, {name}!");
}
}
Greet("Alice");
medium
A. Hello, Guest! printed multiple times
B. Hello, Guest! printed once
C. Hello, Alice! printed zero times
D. Hello, Alice! printed once
Solution
Step 1: Analyze method call
Greet is called with "Alice" only, so name = "Alice", times uses default 1.
Step 2: Understand loop behavior
The loop runs once (times = 1), printing "Hello, Alice!" once.
Final Answer:
Hello, Alice! printed once -> Option D
Quick Check:
Passed name, default times = 1 [OK]
Hint: Passed args override defaults; missing ones use default [OK]
Common Mistakes:
Assuming default name is used when argument is passed
Thinking loop runs zero or multiple times incorrectly
Confusing parameter order and values
4. Identify the error in this method declaration:
public void Display(int x = 5, int y) {
Console.WriteLine(x + y);
}
medium
A. Missing return type
B. Optional parameter must come after all required parameters
C. Cannot use Console.WriteLine inside methods
D. Parameters must have different names
Solution
Step 1: Check parameter order
Parameter x is optional but comes before required parameter y, which is invalid.
Step 2: Verify other options
Return type is present (void), Console.WriteLine is valid, parameter names are unique.
Final Answer:
Optional parameter must come after all required parameters -> Option B
Quick Check:
Optional params last rule violated [OK]
Hint: Optional parameters must be last in parameter list [OK]
Common Mistakes:
Placing optional parameters before required ones
Thinking Console.WriteLine is invalid in methods
Confusing parameter naming rules
5. You want to create a method CalculateTotal that adds two numbers but allows skipping the second number, which defaults to 10. Which method signature is correct and how would you call it to add 5 and the default 10?
hard
A. public int CalculateTotal(int a, int b = 10) { return a + b; } Call: CalculateTotal(5);
B. public int CalculateTotal(int a = 10, int b) { return a + b; } Call: CalculateTotal(5);
C. public int CalculateTotal(int a, int b) { return a + b; } Call: CalculateTotal(5);
D. public int CalculateTotal(int a = 10, int b = 5) { return a + b; } Call: CalculateTotal();
Solution
Step 1: Check method signature rules
Optional parameter b must come after required a, with default 10.
Step 2: Check method call
Calling CalculateTotal(5) passes a=5, b uses default 10, sum is 15.
Final Answer:
public int CalculateTotal(int a, int b = 10) { return a + b; } Call: CalculateTotal(5); -> Option A
Quick Check:
Optional param last, call with one arg uses default [OK]
Hint: Put optional params last and call with fewer args to use defaults [OK]
Common Mistakes:
Placing optional parameter before required one
Calling method without required arguments
Assuming default values apply when arguments are passed
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.