-1

In new versions of C# I have discovered recently that exist what is called expression-bodied-members as explained here.

An example of expression-bodied-members would be (for a getter property only):

private int _x;
public int X
{
    get => _x;
}

Is above expression-bodied-member equivalent to below old C# versions?

private int _x;
public int X
{
    get
    {
       return _x;
    }
}
Charlieface
  • 52,284
  • 6
  • 19
  • 43
Willy
  • 9,848
  • 22
  • 141
  • 284
  • They aren't anything "new" - they've been around since C# 6.0 which was almost a decade ago. – Dai Jan 09 '22 at 21:27
  • 1
    Everytime you wonder whether a feature is compiled identically, just ask Sharplab https://sharplab.io/#v2:CYLg1APgAgTAjAWAFBQMwAJboMLoN7LpGYZQAs6AsgBQCU+hxAvo0QA4BOAlgG4CGAFwCm6LgDsB6APoAPANysSoiegAaiJMQaatxAOZDJAXgB80+YpY6iaZZNUxFBa+gMCni4lADs5hS6tAoA== – Martheen Jan 09 '22 at 21:28
  • @Martheen Great tool! a lot of thanks. I didn't know it. I will take into account from now on. – Willy Jan 09 '22 at 22:59
  • @Dai Ops, I didn't heard about it until just now ;) – Willy Jan 09 '22 at 22:59

2 Answers2

3

These are all equivalent:

private int _x;
public int X => _x;

private int _x;
public int X
{
    get => _x;
}

private int _x;
public int X
{
    get
    {
        return _x;
    }
}
0

Yes, these expressions are equivalent. Because expression-bodied-members do the same, but only for the single expression.