【发布时间】:2010-11-11 21:29:09
【问题描述】:
所以我写了一个简单的计时器类
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;
}
}
如您所见,我接受可选的构造函数参数 (IList),因此当该列表在我的计时器范围内发生更改时,我可以自动写出添加了多少项(行/记录/实体等)。
如下使用:
using (ConsoleTimer t = new ConsoleTimer(_values)) {
_values = GetValues(filter);
}
即使 _values 有 955 个项目,我的计时器的 Dispose 方法仍然将 _items 视为在构造函数中传递的值(无论是 0 还是 null)
_items = items 分配不是参考分配吗?
【问题讨论】:
标签: c# .net oop inheritance