In C # 6.0, the new syntax allows you to write automatic read-only properties using an initializer:
public bool AllowsDuplicates { get; } = true;
Similarly, we can write it using the getter expression:
public bool AllowsDuplicates => true;
For simple types, these two should have the same effect: an automatic read-only property that returns true.
But is one of them preferable to the other? I suspect the first is using the support field:
private readonly bool _backingField = true;
public bool AllowsDuplicates {
get {
return _backingField;
}
}
While the latter turns into something like:
public bool AllowsDuplicates {
get {
return true;
}
}
Is this right, or is the compiler smarter than that?
source
share