---
title: "Expression-bodied members in properties"
description: "Are these two expressions the same?\n```csharp\npublic class MyClass\n{\n    public int A { get; } = Random.Shared.Next(1000);\n    public int B => Random.Shared.Next(1000);\n}\n```"
updated: 2023-08-21
tags: ["C#", ".NET"]
canonical: "https://steven-giesel.com/blogPost/a15563a2-257d-469a-a359-830debfba4ce/expressionbodied-members-in-properties"
---

# Expression-bodied members in properties

Are these two expressions the same?
```csharp
public class MyClass
{
    public int A { get; } = Random.Shared.Next(1000);
    public int B => Random.Shared.Next(1000);
}
```

The short answer is, **no** they are not! If we run the example:
```csharp
var m = new MyClass();
Console.WriteLine(m.A);
Console.WriteLine(m.A);

Console.WriteLine(m.B);
Console.WriteLine(m.B);


public class MyClass
{
    public int A { get; } = Random.Shared.Next(1000);
    public int B => Random.Shared.Next(1000);
}
```

We get the following result:
```no-class
803
803
912
431
```

So `{ get; } = ` evaluates the expression on etime and puts it into the backing field, whereas the lambda expression "calls" the member on every access. The lowered code shows this pretty obviously:

```csharp
public class MyClass
{
    [CompilerGenerated]
    private readonly int <A>k__BackingField = Random.Shared.Next(1000);

    public int A
    {
        [CompilerGenerated]
        get
        {
            return <A>k__BackingField;
        }
    }

    public int B
    {
        get
        {
            return Random.Shared.Next(1000);
        }
    }
}
```