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

Did you find this page helpful?