> ## 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.

# C# Expression-Bodied Property

An expression-bodied property is a syntactic shorthand in C# that utilizes the lambda arrow operator (`=>`) 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, the `get` keyword is omitted entirely. The lambda arrow operator is placed directly after the property identifier.

**Traditional Block Body:**

```csharp theme={"dark"}
public string FullName
{
    get { return $"{FirstName} {LastName}"; }
}
```

**Expression-Bodied Equivalent:**

```csharp theme={"dark"}
public string FullName => $"{FirstName} {LastName}";
```

## 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-bodied `set` accessors was introduced in C# 7, while support for `init` accessors was introduced in C# 9.

**Traditional Block Body:**

```csharp theme={"dark"}
private int _capacity;

public int Capacity
{
    get { return _capacity; }
    set { _capacity = value; }
}
```

**Expression-Bodied Equivalent:**

```csharp theme={"dark"}
private int _capacity;

public int Capacity
{
    get => _capacity;
    set => _capacity = value;
}

public int InitialCapacity
{
    get => _capacity;
    init => _capacity = value;
}
```

## Exception Throwing (Introduced in C# 7)

Expression-bodied accessors support `throw` expressions, allowing an accessor to immediately throw an exception without requiring a statement block.

```csharp theme={"dark"}
private string _status;

public string Status
{
    get => _status;
    set => _status = value ?? throw new ArgumentNullException(nameof(value));
}
```

## Syntax Constraints

* The body must consist of exactly one expression.
* It cannot contain block-level statements such as `if`, `switch`, `for`, or `try/catch` (though ternary operators `?:` and `switch` expressions are permitted as they evaluate to a single value).
* For `set` and `init` accessors, 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 `set` or `init` accessor expression invokes a method that returns a value, the compiler simply discards the returned value, identical to the behavior within a block-bodied accessor.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C# Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
