Proposal: Expression Bodies #9255
Replies: 5 comments 3 replies
-
C# is not a whitespace sensitive language. Blocks/scoping should have nothing to do with indentation. Also, see: #9243 (Oops, paste fail) |
Beta Was this translation helpful? Give feedback.
-
@HaloFour |
Beta Was this translation helpful? Give feedback.
-
@HaloFour, @jaredpar, @timcassell Additionally, the proposal now explicitly references and integrates insights from the existing discussion in #9242, ensuring continuity with prior syntax explorations. The updated rules focus on clarity, compatibility with existing block semantics, and minimal conceptual overhead—consistent with Mads Torgersen’s guidance. Your input has been invaluable in refining this proposal! |
Beta Was this translation helpful? Give feedback.
-
I'm just struggling to see the use case. Many examples could be rewritten to avoid using this feature and still be a one liner. If readability is such a concern and you want a clear assignment of a value to a variable, you can just put the logic in a method/local function and then it'll be even easier to read because it'll be named. |
Beta Was this translation helpful? Give feedback.
-
I advocate for this as a general concept in whatever the released form may be. Further elimination of braces and similarity to F# is good! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Proposal: Expression Bodies in C#
C# continues to evolve by embracing expressive, concise syntax that reduces boilerplate while maintaining clarity. A proposed feature, Expression Bodies, aims to further streamline code by allowing sequences of statements to be treated as single expressions, without the need for explicit braces ({ }). This builds on the success of existing expression-bodied members (like => for methods or properties) but extends their power to handle multi-step computations naturally.
How It Differs from Expression blocks (#3086 & #9242)
While both proposals aim to unify statements and expressions, Expression Bodies eliminate the need for curly braces
{ }
to denote block boundaries. Instead, expression bodies use a double semicolon;;
only on the final statement to signal the end of the expression body. This creates a more fluid, intuitive syntax that aligns with C#’s existing patterns. For instance:This contrasts with Expression Blocks, which require braces even for single-line expressions, adding visual noise.
Key Conclusions:
Expression Bodies Rules:
These rules and principles reinforce C#’s evolution toward concise, expressive syntax while maintaining clarity and alignment with existing language semantics.
Rule 1: The result of an expression body cannot be discarded.
The following is an expression body because
1;;
is an expression terminated with two semicolons.However, it does not compile because the result of an expression body cannot be discarded.
Rule 2: Only the last statement in an expression body ends with two semicolons.
The following is an expression body. The result of the expression body has been assigned to an integer variable named
a
:The following is not an expression body:
The following is neither an expression body because only the last statement in an expression body ends with a semicolon.
The following is an expression body:
The following is also an expression body:
Rule 3: All code paths of an expression body must return a value.
The following is not an expression body because not all code paths return a value.
Rule 4: The last statement of an expression body must not contain the
return
keywordThe following is not an expression body either because the last statement of an expression body must not contain the
return
keyword.The following code snippet is a valid expression body:
These are also valid expression bodies:
Rule 5: Expression bodies can be declared in a block of code.
The following methods are equivalent
The following is not a method with an expression body
This is the same method with an expression body:
However, preferred alternatives are:
The following is an expression body used in a block of code
Rule 6: Expression bodies can be declared inside expression bodies.
Some More Examples
See also
#377
#9242
LDM 2020-01-22
https://github.com/dotnet/csharplang/blob/main/meetings/2022/LDM-2022-09-26.md#discriminated-unions
https://github.com/dotnet/csharplang/blob/main/meetings/2024/LDM-2024-08-28.md#block-bodied-switch-expression-arms
Originally posted by @MadsTorgersen in #3086
Beta Was this translation helpful? Give feedback.
All reactions