Does Stopwatch() need to be disposed?

Viewed 142

By reading the Stopwatch documentation, it looks like it does not need disposing of. But similar classes like Timer and Ticker do.

I think I have the right answer but because it didn't feel 100% right to me, I just wanted to hear someone else's opinion! Thank you!

1 Answers

Timer and Ticker register callbacks. If you don't explicitly dispose of them, those callbacks will still be registered somewhere (and therefore will still retain a reference to the Timer/Ticker objects, preventing them from being garbage collected), and the callbacks will still be invoked eventually if you wait long enough.

Stopwatch is completely different. It's just a convenience class to store a starting time and to compute a time difference. There are no callbacks involved. You can just let it be garbage collected.

Related