Complete the code to declare a method with an optional parameter.
void Greet(string name, string greeting = [1]) { Console.WriteLine($"{greeting}, {name}!"); }
The optional parameter greeting is given a default value "Hello". This means if no argument is passed for greeting, it uses "Hello".
Complete the code to call the method using the optional parameter.
Greet("Alice", [1]);
When calling the method, the optional parameter can be passed as a string literal in quotes.
Fix the error in the method declaration with an optional parameter.
void Display(int count, string message [1]) { Console.WriteLine($"{message}: {count}"); }
Optional parameters require an equals sign (=) followed by the default value.
Fill both blanks to declare a method with two optional parameters.
void ShowInfo(string name, int age = [1], string city = [2]) { Console.WriteLine($"{name}, {age}, {city}"); }
The optional parameters age and city have default values 30 and "Unknown" respectively.
Fill all three blanks to call the method using named and optional parameters.
ShowInfo(name: [1], city: [2], age: [3]);
The method is called with named arguments: name="Alice", city="Paris", age=25.