I do sometimes see abbreviations in properties for the getter. E.g. those two types:
public int Number { get; } = 0
public int Number => 0;
Can someone please tell me if there are any differences between those two. How do they behave? Are both of them read-only?
Yes, both of them are read-only, but there is a difference. In the first one, there's a backing field which is initialized to 0 before the constructor is executed. You can change the value only in the constructor, just like a regular read-only field. The getter itself just returns the value of the field.
In the second one, the getter just returns 0 every time, with no field involved.
So to avoid using any automatically implemented properties or expression-bodied members at all, we have:
First version
private readonly int _number = 0;
public int Number { get { return _number; } }
Second version
public int Number { get { return 0; } }
A clearer example of the difference might be seen like this:
public DateTime CreationTime { get; } = DateTime.UtcNow;
public DateTime CurrentTime => DateTime.UtcNow;
If you create a single object, its CreationTime
property will always give the same result - because it's stored in a readonly field, initialized on object construction. However, every time you access the CurrentTime
property, that will cause DateTime.UtcNow
to be evaluated, so you'll get a potentially different result.
See more on this question at Stackoverflow