I’m trying to learn more about the Dispose method and how things happening in this might stall the Garbage collection of this object.
So, if I have:
DateTime _date = DateTime.UtcNow;
public void Dispose()
{
bool append = true;
using(var log = new System.IO.StreamWriter("log.txt", append))
{
log.WriteLine("Logged on: " + _date);
}
}
Will the fact that I’m calling the _date in the Dispose method re-root the variable and not allow the GC to collect this?
This is leading on from my question here.
The garbage collector has nothing whatsoever to do with
Dispose(), so the simple answer is: not at all. GC involves the finalizer (~YourType()), but notDispose(), unless you mean the commonDispose(bool disposing) {...}pattern.Secondly, a
DateTimeis astruct, and is thus not garbage collected.