❔ Difference between using readonly or { get; }?
The only difference I can think of is that if we use
{ get; }
then we are converting from a field to a property; but I'm not so sure about this...11 Replies
Essentially yes.
readonly
is a field modifier, while { get; }
is a get-only property. The major difference usually comes in API. An interface can't declare a readonly field, but it can declare a get-only prop for example that the derived classes can implement however they want. They can be properties with a backing field or computed properties, ...
So your only major difference essentially is APIAlright, thanks!
{ grt; } is a readonly property which generates a readonly backing field
it's convention to use properties target than fields for public things
You can also have:
Which won't be quite read-only
thats is also a readonly property
yeah, but its compiled to just
get_Whatever
methodyeah but its a property
one that you cant set
so, readonly property
well its not same as readonly field
yes because its not
yeah that was the point
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.