C
C#10mo ago
faetalize

✅ What does this property do?

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