so i wrote a simple timer class
public class ConsoleTimer : IDisposable
{
private Stopwatch _watch;
private IList _items;
public object Count = "0";
public ConsoleTimer(IList items) {
_watch = new Stopwatch();
_items = items;
_watch.Start();
}
public void Dispose() {
var c = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Green;
_watch.Stop();
TimeSpan ts = _watch.Elapsed;
Console.WriteLine(String.Format("{0} items in {1}m {2}s", _items != null ? _items.Count : Count, ts.Minutes, ts.Seconds));
Console.ForegroundColor = c;
}
}
as you can see, i accept optional constructor parameter (IList) so that when that list changes during the course of my timer scope, i can automatically write out how many items (rows/records/entities etc) were added.
using it as follows:
using (ConsoleTimer t = new ConsoleTimer(_values)) {
_values = GetValues(filter);
}
Even though _values has a 955 items, the Dispose method of my timer still sees _items as the value that was passed in the constructor (whether it be 0 or null)
is the _items = items assignment not a reference assignment?
But you’re assigning a new reference to
_valueswhich won’t be seen by_items! Better to sayNow you’re modifying the original referent so that both
_valuesand_itemssee the changes.