# Expression bodied members

Wanneer je methoden, constructors of properties schrijft waar **exact 1 expressie** (*1 lijn code* die een resultaat teruggeeft) nodig is dan kan je gebruik maken van de **expression bodied member syntax** (EBM).

Deze is van de vorm:

```
member => expression
```

Dankzij EBM kan je veel kortere code schrijven.

Ik toon telkens een voorbeeld hoe deze origineel is en hoe deze naar EBM syntax kan omgezet worden.

## Methoden en EBM

Origineel:

```csharp
public void ToonGeboortejaar(int geboortejaarIn)
{
    Console.WriteLine(geboortejaarIn);
}
```

Met EBM:

```csharp
public void ToonGeboortejaar(int geboortejaarIn)
                     => Console.WriteLine(geboortejaarIn);
```

Nog een voorbeeld, nu met een return. Merk op dat we return niet moeten schrijven:

```csharp
public int GeefGewicht()
{
    return 4 * 34;
}
```

Met EBM:

```csharp
public int GeefGewicht() => 4 * 34;
```

## Constructors en EBM

Ook constructors die maar 1 expressie bevatten kunnen korter nu. Origineel:

```csharp
internal class Student
{
    public int Geboortejaar {get;set;}
    public Student(int geboorteJaarIn)
    {
        Geboortejaar = geboorteJaarIn;
    }
}
```

Met EBM wordt dit:

```csharp
internal class Student
{
    public int Geboortejaar {get;set;}
    public Student(int geboorteJaarIn) => Geboortejaar = geboorteJaarIn;
}
```

## Full Properties met EBM

Properties worden een soort mengeling tussen full en auto-properties:

```csharp
private int name;
public int Name
{
    get => name;
    set => name = value;
}
```

## Read-only properties met EBM

Bij read-only properties hoeft het `get` keyword zelfs niet meer getypt te worden bij EBM:

```csharp
private int name;
public int Name => name;
```

{% hint style="info" %}
Uiteraard had voorgaande zelfs nog korter geweest met behulp van een auto-property.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://apwt.gitbook.io/zie-scherp-scherper/appendix/6_exprbody.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
