C
C#15mo ago

❔ ✅ why properties instead of public fields

in this case
class Person
{
public int age;
//vs
public int Age {get; set;}
}
class Person
{
public int age;
//vs
public int Age {get; set;}
}
36 Replies
Thinker
Thinker15mo ago
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
Jimmacle
Jimmacle15mo ago
also, properties give you more options for accessibility control like being able to read a value but not set it
fæ
OP15mo ago
readonly fields?
Jimmacle
Jimmacle15mo ago
let me be more specific: read a field but not set it outside of the class like public int Age { get; private set; }
fæ
OP15mo ago
im asking if readonly fields are the same thing
Jimmacle
Jimmacle15mo ago
they are not
fæ
OP15mo ago
why
Jimmacle
Jimmacle15mo ago
readonly fields can only be set once, in the constructor
fæ
OP15mo ago
and this can be set anywhere inside the class
Jimmacle
Jimmacle15mo ago
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
fæ
OP15mo ago
but not outside of it?
Jimmacle
Jimmacle15mo ago
right the equivalent of a readonly field as a property is a property with just a getter, like public int Age { get; }
fæ
OP15mo ago
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
Jimmacle
Jimmacle15mo ago
my rule is anything that is exposed outside the class should be a property, with the exception of constants (literally const)
fæ
OP15mo ago
public int incrementMe { get => incrementMe++ }
public int incrementMe { get => incrementMe++ }
everytime this is accessed will it increment?
Jimmacle
Jimmacle15mo ago
you don't want getters to change state, that's unexpected behavior also, that's infinitely recursive
fæ
OP15mo ago
what if that's my intention? minus the recursiveness
Jimmacle
Jimmacle15mo ago
i would suggest making it a method like GetNextValue()
fæ
OP15mo ago
what if i want to read how many times a property was accessed
Jimmacle
Jimmacle15mo ago
that would be acceptable i guess
fæ
OP15mo ago
okay i think i get it and this cant be achieved with a field alone
Jimmacle
Jimmacle15mo ago
just consider code like
var a = x.incrementMe;
var b = x.incrementMe;
var a = x.incrementMe;
var b = x.incrementMe;
if someone is coming across that for the first time what would they expect?
fæ
OP15mo ago
that a == b
Jimmacle
Jimmacle15mo ago
right, so you want to avoid writing code that would break that expectation
fæ
OP15mo ago
i see that isn't ideal but i could change the state of another field right?
Jimmacle
Jimmacle15mo ago
sure
fæ
OP15mo ago
or can properties only interfere with their own backing fields
Jimmacle
Jimmacle15mo ago
properties can do pretty much anything, they are essentially pairs of methods (they literally compile down to get and set methods)
fæ
OP15mo ago
what would be the difference between const and readonly then why would a const not need a property (unless i misunderstood)
Jimmacle
Jimmacle15mo ago
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
fæ
OP15mo ago
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?
Thinker
Thinker15mo ago
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
fæ
OP15mo ago
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
Jimmacle
Jimmacle15mo ago
$close
MODiX
MODiX15mo ago
Use the /close command to mark a forum thread as answered
Accord
Accord15mo 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