C
C#16mo ago
Sandy Stone

❔ 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
LPeter1997
LPeter199716mo ago
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 API
Sandy Stone
Sandy StoneOP16mo ago
Alright, thanks!
sibber
sibber16mo ago
{ grt; } is a readonly property which generates a readonly backing field it's convention to use properties target than fields for public things
basically, i am little cat
You can also have:
get {
return Random.Shared.Next(0, 64);
}
get {
return Random.Shared.Next(0, 64);
}
Which won't be quite read-only
sibber
sibber16mo ago
thats is also a readonly property
basically, i am little cat
yeah, but its compiled to just get_Whatever method
sibber
sibber16mo ago
yeah but its a property one that you cant set so, readonly property
basically, i am little cat
well its not same as readonly field
sibber
sibber16mo ago
yes because its not
basically, i am little cat
yeah that was the point
Accord
Accord16mo ago
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.
Want results from more Discord servers?
Add your server