我最近正在使用一个DateTime对象,并写了这样的东西:
DateTime dt = DateTime.Now;
dt.AddDays(1);
return dt; // still today's date! WTF?
AddDays()的智能感知文档说它在日期后添加了一天,但它并没有这样做——它实际上返回了一个添加了一天的日期,所以你必须这样写:
DateTime dt = DateTime.Now;
dt = dt.AddDays(1);
return dt; // tomorrow's date
这个问题以前已经困扰过我很多次了,所以我认为将最糟糕的c#陷阱分类会很有用。
可枚举对象可以计算不止一次
当您有一个惰性枚举的可枚举对象,并且对其进行两次迭代并得到不同的结果时,它会咬您一口。(或者你得到相同的结果,但它执行两次不必要的)
例如,在编写某个测试时,我需要一些临时文件来测试逻辑:
var files = Enumerable.Range(0, 5)
.Select(i => Path.GetTempFileName());
foreach (var file in files)
File.WriteAllText(file, "HELLO WORLD!");
/* ... many lines of codes later ... */
foreach (var file in files)
File.Delete(file);
想象一下当file . delete (file)抛出FileNotFound时我的惊讶!!
这里发生的情况是,可枚举的文件被迭代了两次(第一次迭代的结果根本没有被记住),在每次新的迭代中,您将重新调用Path.GetTempFilename(),因此您将得到一组不同的临时文件名。
当然,解决方案是使用ToArray()或ToList()快速枚举值:
var files = Enumerable.Range(0, 5)
.Select(i => Path.GetTempFileName())
.ToArray();
当你在做一些多线程的事情时,这甚至更可怕,比如:
foreach (var file in files)
content = content + File.ReadAllText(file);
你会发现内容。在所有写入之后,长度仍然为0 !!然后,当....时,您开始严格检查是否没有竞态条件浪费了一个小时之后……你发现这只是一个微小的可枚举的东西,你忘记了....
海森堡表窗
如果你在做按需加载的事情,这会让你很难受,就像这样:
private MyClass _myObj;
public MyClass MyObj {
get {
if (_myObj == null)
_myObj = CreateMyObj(); // some other code to create my object
return _myObj;
}
}
现在让我们假设你有一些代码在其他地方使用这个:
// blah
// blah
MyObj.DoStuff(); // Line 3
// blah
现在您需要调试CreateMyObj()方法。因此,您在上面的第3行上放置了一个断点,目的是进入代码。为了更好地度量,您还在上面的行中放置了一个断点,表示_myObj = CreateMyObj();,甚至在CreateMyObj()本身中也放置了一个断点。
代码在第3行碰到断点。你进入代码。您希望输入条件代码,因为_myObj显然是空的,对吗?嗯…所以…为什么它跳过条件直接返回_myObj?!将鼠标悬停在_myObj上…事实上,它确实有价值!怎么会这样?!
The answer is that your IDE caused it to get a value, because you have a "watch" window open - especially the "Autos" watch window, which displays the values of all variables/properties relevant to the current or previous line of execution. When you hit your breakpoint on Line 3, the watch window decided that you would be interested to know the value of MyObj - so behind the scenes, ignoring any of your breakpoints, it went and calculated the value of MyObj for you - including the call to CreateMyObj() that sets the value of _myObj!
这就是为什么我称其为海森堡观察窗口-你不能观察值而不影响它…:)
明白了!
编辑-我觉得@ChristianHayter的评论应该包含在主要答案中,因为它看起来是解决这个问题的有效方法。所以任何时候你有一个惰性加载的属性…
用[DebuggerBrowsable(DebuggerBrowsableState.Never)]或[DebuggerDisplay("<loaded on demand>")]装饰你的属性。——克里斯蒂安·海特