Is it possible to write this property:
string Error { get; }
in expression bodied syntax (=>
),
for example:
string Title
{
get
{
return title;
}
}
becomes:
string Title => title;
No, because this:
string Error { get; }
... is an automatically-implemented property. The compiler is generating a field for you behind the scenes, but you can't refer to that field within the code. If you need to use the backing field, you need to declare it yourself:
private readonly string error;
string Error => error;
That's basically what the compiler is generating for you - so if you want that, just write it yourself. It's pretty rare that that's useful though, IMO.
If you already have that field, you could either just write the property as above, or you could convert the field into the property, so use the property where you were previously using the field.
(It's more feasible if you want a property which is read-only, but backed by a mutable field - at which point it can't be auto-implemented if you want a genuinely read-only property. It could be a publicly-readable and privately-writable automatically-implemented property though.)
See more on this question at Stackoverflow