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
can you share the related code?
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
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
This rule flags members of non-readonly structs that aren't marked readonly that could be marked as readonly.
Ah
yeah
That's something else from the remove setter pls fix
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
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
There can be also readonly methods