C
C#14mo ago

✅ What does this property do?

The backing field is readonly
No description
90 Replies
sibber
sibber14mo ago
theres no backing field
fæ
OP14mo ago
ijust ommited it
sibber
sibber14mo ago
that property calls the getter each time its called ah i meant no auto generated backing field
fæ
OP14mo ago
No description
sibber
sibber14mo ago
yes this one generates a backing field
fæ
OP14mo ago
oh
sibber
sibber14mo ago
so the field that you defined isnt used
fæ
OP14mo ago
okay so, my question pertains the the first one
sibber
sibber14mo ago
it also has a setter
fæ
OP14mo ago
that syntax
fæ
OP14mo ago
i dont get this
No description
sibber
sibber14mo ago
that syntax is short for this:
public string? Sex
{
get { return sex; }
}
public string? Sex
{
get { return sex; }
}
fæ
OP14mo ago
so its an even shorter version of an autogetter
sibber
sibber14mo ago
even shorter? wdym by auto getter
fæ
OP14mo ago
auto getter Sex {get;}
sibber
sibber14mo ago
no this one generates a backing field
fæ
OP14mo ago
ah right is there a similar shorter syntax for a setter, for the case where i defined my own field? can i do
sibber
sibber14mo ago
when theres a property with a getter/initializer/setter with no body, it means it generates a backing field
fæ
OP14mo ago
Sex => sex = value
sibber
sibber14mo ago
youd have to do this
public string? Sex
{
get => sex;
set => sex = value;
}
public string? Sex
{
get => sex;
set => sex = value;
}
because a property with no getter makes no sense
fæ
OP14mo ago
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)?
Jimmacle
Jimmacle14mo ago
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
fæ
OP14mo ago
the linter isnt complaining nor throwing an exception when running this
No description
Jimmacle
Jimmacle14mo ago
look at it, i think you'll see why hint: sex is grayed out
fæ
OP14mo ago
i wonder why this is legal, i thought readonly is supposed to prevent this oh i understaand
sibber
sibber14mo ago
compiler :) we dont have linters
fæ
OP14mo ago
because i used the {get; set;} syntax the field isnt actually sex
Jimmacle
Jimmacle14mo ago
correct, your property is not interacting with your field at all
sibber
sibber14mo ago
which does what?
fæ
OP14mo ago
creates a field implicitly
sibber
sibber14mo ago
yes prevent what exactly?
fæ
OP14mo ago
then, how do i make it readonly while also using auto properties
sibber
sibber14mo ago
make Sex readonly? you remove the setter
fæ
OP14mo ago
attempted that
No description
sibber
sibber14mo ago
delelte readonly thats not for proerties*
fæ
OP14mo ago
ah
sibber
sibber14mo ago
*in most normal use cases
Jimmacle
Jimmacle14mo ago
readonly only applies to fields, { get; } is the equivalent of making a property readonly
fæ
OP14mo ago
the backing field is private
sibber
sibber14mo ago
yes but you cant* access it (directly)
Jimmacle
Jimmacle14mo ago
auto-properties generate magic fields that you can't access at all in normal code
fæ
OP14mo ago
i see
Jimmacle
Jimmacle14mo ago
that's the auto part
fæ
OP14mo ago
is there a way to make my property accessible ONLY in the constructor?
Jimmacle
Jimmacle14mo ago
no, that doesn't make sense
fæ
OP14mo ago
like, make my setter method private to it, but not the rest of the class
Jimmacle
Jimmacle14mo ago
then you'd just make a local variable in your constructor
sibber
sibber14mo ago
you mean settable?
fæ
OP14mo ago
sry settable yes
sibber
sibber14mo ago
readonly props (and fields) are always settable in the ctor if you mean initializer, you need to add init;
public string? Sex { get; init; }
public string? Sex { get; init; }
fæ
OP14mo ago
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
sibber
sibber14mo ago
ah not just int he ctor?
public string? Sex { get; private set; }
public string? Sex { get; private set; }
Angius
Angius14mo ago
public string Sex { private set; get; } ?
fæ
OP14mo ago
No description
Angius
Angius14mo ago
Yeah
fæ
OP14mo ago
ohhh that works!
sibber
sibber14mo ago
you can add any accessibility mod to setters
fæ
OP14mo ago
that should be default 😆
sibber
sibber14mo ago
that would be confusing
fæ
OP14mo ago
this was useful, thank you
sibber
sibber14mo ago
np :)
fæ
OP14mo ago
why isnt readonly for properties or at least, i guess it makes more sense for fields to be readonly
sibber
sibber14mo ago
because no setter already means readonly it does why do you think that?
fæ
OP14mo ago
however what if i want another class to set the sex for example
sibber
sibber14mo ago
just that class?
fæ
OP14mo ago
and then, that sex to stay readonly
sibber
sibber14mo ago
you cant
Jimmacle
Jimmacle14mo ago
it's not read-only if you can change it after the class is created
fæ
OP14mo ago
like, imagine, a doctor assessing the birth of an animal and declaring a sex oh
Jimmacle
Jimmacle14mo ago
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
fæ
OP14mo ago
yes but if it doesnt have a value initially, then i should be able to set something to it
sibber
sibber14mo ago
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
Jimmacle
Jimmacle14mo ago
that kind of logic would belong in a method in the class that checks those conditions
sibber
sibber14mo ago
unless you check if its been set at runtime and throw or something like that
fæ
OP14mo ago
"creating" is just merely declaring the variable or does that meaan assigning an object to it cuz strings are null by default right?
Jimmacle
Jimmacle14mo ago
there are no variables here, we have fields and properties in a class
sibber
sibber14mo ago
by creating i meant constructing the object
Jimmacle
Jimmacle14mo ago
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
sibber
sibber14mo ago
(or initialzier)
fæ
OP14mo ago
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
Jimmacle
Jimmacle14mo ago
this gets more into what the requirements for your application are is it valid to have an animal with no/unassigned sex?
fæ
OP14mo ago
it should be decided at the creation of the object
sibber
sibber14mo ago
you wouldnt create a record for an animal when you only know half details about it
Angius
Angius14mo ago
Make it an enum
enum Sex
{
Unknown,
Male,
Female
}
enum Sex
{
Unknown,
Male,
Female
}
sibber
sibber14mo ago
you check all its properties, then create the record
Angius
Angius14mo ago
Would work better than a nullable magic string
Jimmacle
Jimmacle14mo ago
yeah, if the value has a fixed number of options an enum would be better
fæ
OP14mo ago
well this is for a video game
Angius
Angius14mo ago
So?
fæ
OP14mo ago
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.
Want results from more Discord servers?
Add your server