0
0
C Sharp (C#)programming~10 mins

Optional parameters in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a method with an optional parameter.

C Sharp (C#)
void Greet(string name, string greeting = [1]) {
    Console.WriteLine($"{greeting}, {name}!");
}
Drag options to blanks, or click blank then click option'
A"Hey"
B"Welcome"
C"Hi"
D"Hello"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to assign a default value to the optional parameter.
Using a variable instead of a constant as the default value.
2fill in blank
medium

Complete the code to call the method using the optional parameter.

C Sharp (C#)
Greet("Alice", [1]);
Drag options to blanks, or click blank then click option'
A"Good morning"
BGood morning
CHello
Dgreeting
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a string without quotes.
Passing the parameter name instead of a value.
3fill in blank
hard

Fix the error in the method declaration with an optional parameter.

C Sharp (C#)
void Display(int count, string message [1]) {
    Console.WriteLine($"{message}: {count}");
}
Drag options to blanks, or click blank then click option'
A== "Default"
B: "Default"
C= "Default"
D=> "Default"
Attempts:
3 left
💡 Hint
Common Mistakes
Using : or => instead of = for default values.
Omitting the default value assignment.
4fill in blank
hard

Fill both blanks to declare a method with two optional parameters.

C Sharp (C#)
void ShowInfo(string name, int age = [1], string city = [2]) {
    Console.WriteLine($"{name}, {age}, {city}");
}
Drag options to blanks, or click blank then click option'
A30
Bnull
C"Unknown"
D"None"
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around integer default values.
Not using quotes around string default values.
5fill in blank
hard

Fill all three blanks to call the method using named and optional parameters.

C Sharp (C#)
ShowInfo(name: [1], city: [2], age: [3]);
Drag options to blanks, or click blank then click option'
A"Bob"
B"Paris"
C25
D"Alice"
Attempts:
3 left
💡 Hint
Common Mistakes
Using unnamed arguments in the wrong order.
Forgetting quotes around string arguments.