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