C
C#5mo ago
Pandetthe

Read-only question

Today visual studio 2022 suggested IDE0251 on property getter, it's suggestion that element can be readonly. What's the difference between a normal getter and a readonly one?
9 Replies
Jimmacle
Jimmacle5mo ago
can you share the related code?
SleepWellPupper
SleepWellPupper5mo ago
It probably suggested you remove the setter on an auto property that is never set (after the ctor). There's no such thing as a readonly getter, it's for reading not setting
Pandetthe
PandettheOP5mo ago
Now I am not at home, I will send later I thought that too But it's building normally on .net 8.0 It only suggests on getter where I modify fields value For example winapi union struct with uint value, first bit is working as Boolean. In my getter I am extracting this one bit and comparin to 0 to get boolean
Jimmacle
Jimmacle5mo ago
This rule flags members of non-readonly structs that aren't marked readonly that could be marked as readonly.
SleepWellPupper
SleepWellPupper5mo ago
Ah yeah That's something else from the remove setter pls fix
Jimmacle
Jimmacle5mo ago
it's just telling you that you don't mutate the field so you can set it as readonly which for structs may have implications for preventing defensive copies etc, i don't know the details
Pandetthe
PandettheOP5mo ago
Thanks I need to get more deeper into it I haven't thought that it's possible because it sounds funny Okay I am back and I found this for anyone else interested in it
Pandetthe
PandettheOP5mo ago
There can be also readonly methods
Want results from more Discord servers?
Add your server