✅ Is there a difference when we use { get; init; } vs { get; } ?
Hello guys, if I omit the
init
keyword here, would it make a difference? From what I've understood with or without the init
keyword, we would still be able to "modify" the object during instantiation, no?13 Replies
this code only works if they are
init
it does not work if they are only get
oh ok, would it have work if we would have use the constructor instead of the object initializer?
i don't understand what you mean
I mean, without the init, we would only have been able to do this:
?
well, you can't, since you didn't declare a constructor at all
Ah I see, my bad sorry
Assume the constructor is there
then, yes, you would have to use it
that's 'the old way', i suppose
init
basically lets you use a setter but only once, during initializationyep I see
Functionally,
and
are about the same
yeah, it's just syntax sugar?
Lets you use initializer syntax with similar guarantees a constructor can give you
Especially paired with a
required
keywordyep I see
Thanks !