C
C#10mo ago
faetalize

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