5 ms·
I'm a C# dev and I had to lookup this new feature since it didn't ring a bell. I'm curious, why is this is a big improvement of just having "private set;" in t
by steven777400 11y ago
I'm a C# dev and I had to lookup this new feature since it didn't ring a bell. I'm curious, why is this is a big improvement of just having "private set;" in the auto-property? I'm not being sarcastic, I genuinely want to know.
(Incidentally, in terms of your filtering, I absolutely categorize myself as a "mediocre" or "medium-level" developer, but that doesn't mean I don't care about things. Just that their value may not be immediately obvious to me.)
- recursive 11y agoWith private set; you still have to invoke the setter in the constructor. For some cases, it's more readable to have the value of the property calculated in the same place that the property is declared. Also, when reading code, private set; doesn't guarantee that the property is reassigned elsewhere in the class. You have to read the whole class to verify that.
- briHass 11y agoIt is actually the equivalent of a private backing field that is marked readonly. public class Person{ private readonly string name; public Person(string name){ this.name = name; } } It's syntactical sugar that shaves a few lines of code off, particularly if you aren't setting values from parameters in the constructor and just want readonly values that are inherent to the class.
- DavidWoof 11y agoreadonly means the property is immutable. It can't be changed after construction, period. "private set" suggests it can be changed at any time, but only privately. Like I said, it's a small item, but if immutability is a primary concern to you during class design, then typing that "private set" constantly (or alternately, using a backing field) annoys you just a little bit multiple times a day. You're acutely aware of it and its workarounds. Most devs, even most "advanced" devs, don't think about immutability that much. I would never dream of using this as some kind of hiring filter when hiring devs, it's too obscure. But if I'm on the other side of the interview chair, I find it useful to know if your top devs, the people you rely on to evaluate others, care about things like that. OTOH, in practice, I think the question turned out to be not nearly as useful as some of the testing questions I've asked in the past.