Proposal: IDisposable.Dispose() can reassign readonly
fields
#6554
-
Oftentimes I initialize otherwise The official guidance for implementing IDisposable involves setting large fields to null, but that cannot be done if they are It's already taken for granted (even within the runtime) that using an disposed object yields undefined behavior dotnet/runtime#71548 (review), though double-disposing shouldn't throw and we try to throw an I propose special-casing Dispose() to have the ability to modify |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 9 replies
-
|
Beta Was this translation helpful? Give feedback.
-
You can set readonly fields by using System.Runtime.CompilerServices.Unsafe.AsRef - it works because those objects / fields are allocated in regular heap memory. |
Beta Was this translation helpful? Give feedback.
-
If you're going to mutate them, why not just make them mutable? |
Beta Was this translation helpful? Give feedback.
If you're going to mutate them, why not just make them mutable?