An expression-bodied property is a syntactic shorthand in C# that utilizes the lambda arrow operator (Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
=>) to implement property accessors as a single expression, eliminating the need for explicit block bodies ({}) and return statements.
Under the hood, the compiler translates this syntax into standard block-bodied accessors. The expression on the right side of the => operator must evaluate to a type implicitly convertible to the property’s return type for a get accessor. For a set or init accessor, the expression must be a valid statement expression, which includes assignments, method invocations, object creations (new), and increment/decrement operations.
Read-Only Properties (Introduced in C# 6)
For a read-only property, theget keyword is omitted entirely. The lambda arrow operator is placed directly after the property identifier.
Traditional Block Body:
Read/Write and Init Properties (Introduced in C# 7 and C# 9)
For properties requiring explicit accessors, the expression-body syntax is applied individually to each accessor within the property block. Support for expression-bodiedset accessors was introduced in C# 7, while support for init accessors was introduced in C# 9.
Traditional Block Body:
Exception Throwing (Introduced in C# 7)
Expression-bodied accessors supportthrow expressions, allowing an accessor to immediately throw an exception without requiring a statement block.
Syntax Constraints
- The body must consist of exactly one expression.
- It cannot contain block-level statements such as
if,switch,for, ortry/catch(though ternary operators?:andswitchexpressions are permitted as they evaluate to a single value). - For
setandinitaccessors, the compiler does not enforce state mutation. The accessor can execute any valid statement expression, such as logging a value or raising an event. - If a
setorinitaccessor expression invokes a method that returns a value, the compiler simply discards the returned value, identical to the behavior within a block-bodied accessor.
Master C# with Deep Grasping Methodology!Learn More





