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

Expression-bodied methods in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an expression-bodied method in C#?
An expression-bodied method is a concise way to write methods using the => syntax to return a single expression instead of a full block with braces.
Click to reveal answer
beginner
Rewrite this method using expression-bodied syntax:<br>
int Square(int x) { return x * x; }
int Square(int x) => x * x;
Click to reveal answer
beginner
Can expression-bodied methods have multiple statements inside?
No, expression-bodied methods can only have a single expression. For multiple statements, use the regular method body with braces.
Click to reveal answer
intermediate
How do expression-bodied methods improve code readability?
They make simple methods shorter and clearer by removing unnecessary braces and return keywords, making the code easier to read and write.
Click to reveal answer
intermediate
Which C# version introduced expression-bodied methods?
Expression-bodied methods were introduced in C# 6.0.
Click to reveal answer
Which symbol is used to define an expression-bodied method in C#?
A->
B=>=
C::
D=>
Can an expression-bodied method contain multiple statements?
AYes, with semicolons separating them
BNo, only a single expression is allowed
CYes, if enclosed in parentheses
DOnly if marked async
What is the benefit of using expression-bodied methods?
AThey allow multiple return values
BThey enable method overloading
CThey make code shorter and more readable for simple methods
DThey improve runtime performance
Which C# version first introduced expression-bodied methods?
AC# 6.0
BC# 5.0
CC# 7.0
DC# 8.0
How would you convert this method to expression-bodied syntax?<br>
string GetName() { return "Alice"; }
Astring GetName() => "Alice";
Bstring GetName() => { return "Alice"; };
Cstring GetName() => return "Alice";
Dstring GetName() => { "Alice"; };
Explain what expression-bodied methods are and when you would use them.
Think about how to write short methods more clearly.
You got /4 concepts.
    Show how to convert a normal method with a return statement into an expression-bodied method.
    Focus on replacing the method body with a single expression.
    You got /4 concepts.