3 ms·
I would prefer static read-only properties over methods and always return the same instance. private static readonly BookingTypeId gold = new BookingTypeId
by danbruc 13y ago
I would prefer static read-only properties over methods and always return the same instance.
private static readonly BookingTypeId gold = new BookingTypeId(3);
public static BookingTypeId Gold
{
get { return BookingTypeId.gold; }
}
It becomes a bit inconsistent because of the method for the custom case but I would accept that for less noise in the common case.
- kevingadd 13y agoIf the field is readonly why did you make the property in the first place? Just do public static readonly BookingTypeId Gold. It's equivalent.
- Locke1689 13y agoIf this is public surface area, the change from field to property is a binary break.
- danbruc 13y ago...and you never know when you might have to add something to the getter. It's the same reason why you usually prefer properties over public fields even if there is no logic in the accessors. Similarly you usually want to expose constants by wrapping them in a read-only property because otherwise the compiler will inline the constant and you can not change it without recompiling all dependencies.
- klauserc 13y agoIt's a struct. It will always be returned by value. Furthermore, the struct has a single Int32 field. The code generated for "new BookingTypeId(3)" is about the same as for "3".