Sometimes I see classes defined with readonly members like so:
class Foo
{
private readonly string bar;
public Foo(string bar)
{
this.bar = bar;
}
public string Bar => bar;
}
and other times I see classes defined with readonly members like so:
class Foo
{
public Foo(string bar)
{
Bar = bar;
}
public string Bar { get; private set; }
}
The second example looks a lot more readable/succinct to me, but I was wondering if there is any legitimate reason for explicitly defining a readonly
backing field (as per the first example) ?
Qudos for an in-depth analysis of the two examples, ideally covering what's really going on under the hood, in the CLR.