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

Named arguments in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What are named arguments in C#?
Named arguments allow you to specify the name of the parameter when calling a method, so you can pass arguments in any order and improve code readability.
Click to reveal answer
beginner
How do named arguments improve code readability?
By explicitly naming parameters in method calls, it becomes clear what each argument represents, making the code easier to understand without looking at the method definition.
Click to reveal answer
intermediate
Can you mix named and positional arguments in a method call?
Yes, but positional arguments must come before any named arguments in the call.
Click to reveal answer
beginner
What happens if you use a wrong parameter name in a named argument?
The compiler will show an error because the parameter name does not match any parameter in the method definition.
Click to reveal answer
beginner
Example: How to call a method with named arguments?
void PrintInfo(string name, int age) { Console.WriteLine($"Name: {name}, Age: {age}"); }
You can call it like this:
PrintInfo(age: 30, name: "Alice");
This prints: Name: Alice, Age: 30
Click to reveal answer
What is the correct way to use named arguments in C#?
AMethodName(paramName = value);
BMethodName(value: paramName);
CMethodName(paramName: value);
DMethodName(value);
Can you place positional arguments after named arguments in a method call?
ANo, positional arguments must come first.
BOnly if the named arguments are last.
COnly if the method has optional parameters.
DYes, always.
What error occurs if you use a wrong parameter name in a named argument?
ARuntime exception
BCompiler error
CWarning only
DNo error, it is ignored
Why use named arguments?
ATo allow arguments in any order and improve readability
BTo make methods run faster
CTo avoid using optional parameters
DTo declare variables inside methods
Which of these calls is valid if the method is void Foo(int x, int y)?
AFoo(x: 1, 2);
BFoo(x = 1, y = 2);
CFoo(1, y = 2);
DFoo(y: 2, x: 1);
Explain what named arguments are and how they help when calling methods in C#.
Think about how naming parameters in calls can make code clearer.
You got /3 concepts.
    Describe the rules for mixing positional and named arguments in a method call.
    Consider the order of arguments when calling a method.
    You got /3 concepts.