I have a class that represents a symmetric key in C#. Is it correct that when an object is released, all it does is it makes the memory used by the object available, but it doesn't actually zero out anything that may have been in memory in the first place, yes? With that assumption, I decided to create a Clear() method that simply writes random data into the fields and properties of the object, with the intention that Clear() should be called somehow to make sure whatever was in memory has been overwritten before its resources are released. However, I'm not sure how exactly that should be done:
- My first thought was to put Clear(); in the class's destructor, so it would be called to 'clean up' its fields and properties before it is released. However, from what I understand right now, when it's actually collected by the GC is another story.
- Thus, I researched IDisposable and considered using statements to explicitly define when the object will no longer be used, thus its assets would be overwritten with random bits before being disposed. Apparently, though, the overhead of using IDisposable is potentially expensive.