Junior — Middle
How is data overwritten in C#?
sobes.tech AI
Answer from AI
In C#, data wiping is usually implemented by repeatedly filling memory with random or fixed values to prevent sensitive information from being recovered.
For strings, this is difficult because strings in C# are immutable, but for byte arrays or char arrays, methods like Array.Clear or filling with a loop can be used.
Example of wiping a byte array:
void WipeData(byte[] data)
{
if (data == null) return;
for (int i = 0; i < data.Length; i++)
{
data[i] = 0;
}
}
For more secure wiping, the System.Security.Cryptography.RandomNumberGenerator class can be used to fill with random data before clearing.
It is important to remember that due to the characteristics of garbage collection and compiler optimizations, guaranteed complete removal of data from memory in managed environments is difficult.