How should the clean-up of Timers declared inside the scope of a function be managed?
问题 In the following code, a Timer is declared inside a function, where it also subscribes to the Elapsed event: void StartTimer() { System.Timers.Timer timer = new System.Timers.Timer(1000); timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed); timer.AutoReset = false; timer.Start(); } Once the function completes, the reference to the Timer is lost (I presume). Does the Elapsed event get unregistered automatically as the object is destroyed, or if not, can the event be