90 Replies
theres no backing field
ijust ommited it
that property calls the getter each time its called
ah
i meant no auto generated backing field
yes this one generates a backing field
oh
so the field that you defined isnt used
okay so, my question pertains the the first one
it also has a setter
that syntax
i dont get this
that syntax is short for this:
so its an even shorter version of an autogetter
even shorter?
wdym by auto getter
auto getter
Sex {get;}
no
this one generates a backing field
ah right
is there a similar shorter syntax for a setter, for the case where i defined my own field?
can i do
when theres a property with a getter/initializer/setter with no body, it means it generates a backing field
Sex => sex = value
youd have to do this
because a property with no getter makes no sense
makes sense
one last question
how come i can define a setter for a readonly variable? isnt it meant to be only set once (in the class)?
you can't, you either have a property with a setter or a field that's readonly
you can't have readonly properties or fields with setters
the linter isnt complaining nor throwing an exception when running this
look at it, i think you'll see why
hint:
sex
is grayed outi wonder why this is legal, i thought readonly is supposed to prevent this
oh
i understaand
compiler :) we dont have linters
because i used the {get; set;} syntax
the field isnt actually
sex
correct, your property is not interacting with your field at all
which does what?
creates a field implicitly
yes
prevent what exactly?
then, how do i make it readonly while also using auto properties
make Sex readonly?
you remove the setter
attempted that
delelte
readonly
thats not for proerties*ah
*in most normal use cases
readonly
only applies to fields, { get; }
is the equivalent of making a property readonly
the backing field is private
yes but you cant* access it
(directly)
auto-properties generate magic fields that you can't access at all in normal code
i see
that's the
auto
partis there a way to make my property accessible ONLY in the constructor?
no, that doesn't make sense
like, make my setter method private to it, but not the rest of the class
then you'd just make a local variable in your constructor
you mean settable?
sry settable yes
readonly props (and fields) are always settable in the ctor
if you mean initializer, you need to add
init;
i think there's a misunderstanding
my fault i made a mistake
so, my goal is to make the property unsettable outside of the class, but ok inside
ah
not just int he ctor?
public string Sex { private set; get; }
?Yeah
ohhh
that works!
you can add any accessibility mod to setters
that should be default 😆
that would be confusing
this was useful, thank you
np :)
why isnt readonly for properties
or at least, i guess it makes more sense for fields to be readonly
because no setter already means readonly
it does
why do you think that?
however what if i want another class to set the sex for example
just that class?
and then, that sex to stay readonly
you cant
it's not read-only if you can change it after the class is created
like, imagine, a doctor assessing the birth of an animal and declaring a sex
oh
just semantically, read-only means you can't write a new value to it
so if you want that, you don't want it to be read-only
yes but if it doesnt have a value initially, then i should be able to set something to it
thats not really a thing you can do
its either set as many times as you want, or set it just once when creating it
that kind of logic would belong in a method in the class that checks those conditions
unless you check if its been set at runtime and throw or something like that
"creating" is just merely declaring the variable or does that meaan assigning an object to it
cuz strings are null by default right?
there are no variables here, we have fields and properties in a class
by creating i meant constructing the object
if you want to provide a value once, you should pass it in the constructor and set the value of your read-only property/field there
(or initialzier)
ah, well, the more i think about it, the more what i seek to do makes less sense
just because a vet doesnt know the sex of a newborn pup, doesnt mean the sex doesnt already exist
so another class shouldnt be responsible of setting that
this gets more into what the requirements for your application are
is it valid to have an animal with no/unassigned sex?
it should be decided at the creation of the object
you wouldnt create a record for an animal when you only know half details about it
Make it an enum
you check all its properties, then create the record
Would work better than a nullable magic string
yeah, if the value has a fixed number of options an enum would be better
well this is for a video game
So?
so at the creation of the Pup or Human object (which represent the birth) the game logic should decide a sex immediately
i guess, the interaction i was looking for (doctor/vet assessing a newborn's sex) doesnt make sense
this is also ok.
yeah this gets complicated if i wanna add more sexes that arent predefined
ok, this has gone offtopic. thank you all for the help. if i need help aabout something else i will make a new topic.