36 Replies
You can change the implementation of a property, but you can't with a field.
eg. a property allows you to in the future change what the property does, you might want some validation in the setter or that the getter does something different
also, properties give you more options for accessibility control
like being able to read a value but not set it
readonly fields?
let me be more specific: read a field but not set it outside of the class
like
public int Age { get; private set; }
im asking if readonly fields are the same thing
they are not
why
readonly fields can only be set once, in the constructor
and this can be set anywhere inside the class
a property with a private setter can be updated as many times as you want, but only from code inside the class it's defined in
but not outside of it?
right
the equivalent of a readonly field as a property is a property with just a getter, like
public int Age { get; }
can i suggest an example and you tell me if its a good way to use a get-only property vs readonly?
let me write it out and tell me if its a good usecase
my rule is anything that is exposed outside the class should be a property, with the exception of constants (literally
const
)
everytime this is accessed will it increment?
you don't want getters to change state, that's unexpected behavior
also, that's infinitely recursive
what if that's my intention? minus the recursiveness
i would suggest making it a method like
GetNextValue()
what if i want to read how many times a property was accessed
that would be acceptable i guess
okay i think i get it
and this cant be achieved with a field alone
just consider code like
if someone is coming across that for the first time what would they expect?
that a == b
right, so you want to avoid writing code that would break that expectation
i see that isn't ideal
but i could change the state of another field right?
sure
or can properties only interfere with their own backing fields
properties can do pretty much anything, they are essentially pairs of methods
(they literally compile down to get and set methods)
what would be the difference between const and readonly then
why would a const not need a property
(unless i misunderstood)
const is a value that is defined at compile time
readonly can be initialized to a value at runtime but can't be changed after that
i thought .net was always JIT
i think i might be confusing things
C# doesnt work like java where it's interpreted by the jvm, right? the CLR works differently?
C# is compiled to IL which is then JITed
It works exactly like Java
IL is like JAR and the CLR is like the JVM
then java isnt interpreted either
sorry about that
okay i understand now. i'm gonna set this to resolved. thank you jimmacle and thinker
is there a way to close/tag this as resolved
$close
Use the /close command to mark a forum thread as answered
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.