What's the difference between a struct
with ALL readonly
fields, and a readonly struct
, which requires you to explicitly state all fields as readonly
?
This just seems like an extra KW for no reason, if it's not implicitly making things readonly
.
Thanks!
Cheers!
Cheers!
Top comments (3)
Why doesn't it just implicitly make everything
readonly
for me, then?Surely it would be thought through that having a bunch of
readonly
s, or{get;}
s in astruct
that has already been declaredreadonly
is kind of redundant, and (in my opinion) ugly.It makes it clearer to other programmers that they shouldn’t try and add mutable fields. It also has the added benefit of allowing better compiler optimisation. I would recommend using a record as opposed to a read only struct in most situations.
docs.microsoft.com/en-us/dotnet/st...
Huh? But isn't readonly like
final
(from Java)???I think I'm confused.