我最近正在使用一个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#陷阱分类会很有用。
重载==操作符和非类型化容器(数组列表、数据集等):
string my = "my ";
Debug.Assert(my+"string" == "my string"); //true
var a = new ArrayList();
a.Add(my+"string");
a.Add("my string");
// uses ==(object) instead of ==(string)
Debug.Assert(a[1] == "my string"); // true, due to interning magic
Debug.Assert(a[0] == "my string"); // false
解决方案?
总是使用字符串。当比较字符串类型时等于(a, b)
使用像List<string>这样的泛型来确保两个操作数都是字符串。
重载==操作符和非类型化容器(数组列表、数据集等):
string my = "my ";
Debug.Assert(my+"string" == "my string"); //true
var a = new ArrayList();
a.Add(my+"string");
a.Add("my string");
// uses ==(object) instead of ==(string)
Debug.Assert(a[1] == "my string"); // true, due to interning magic
Debug.Assert(a[0] == "my string"); // false
解决方案?
总是使用字符串。当比较字符串类型时等于(a, b)
使用像List<string>这样的泛型来确保两个操作数都是字符串。
海森堡表窗
如果你在做按需加载的事情,这会让你很难受,就像这样:
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>")]装饰你的属性。——克里斯蒂安·海特
下面的代码不会捕获. net中的异常。相反,它会导致StackOverflow异常。
private void button1_Click( object sender, EventArgs e ) {
try {
CallMe(234);
} catch (Exception ex) {
label1.Text = ex.Message.ToString();
}
}
private void CallMe( Int32 x ) {
CallMe(x);
}
对于评论(和反对票):
如此明显的堆栈溢出是极其罕见的。但是,如果发生异常,您将不会捕获异常,并且可能会花费几个小时试图查找问题的确切位置。如果SO出现在很少使用的逻辑路径中,特别是在web应用程序中,你可能不知道引发问题的确切条件,那么情况就会更加复杂。
这与这个问题的公认答案完全相同(https://stackoverflow.com/a/241194/2424)。答案上的属性getter本质上做的事情与上面的代码完全相同,并且在没有堆栈跟踪的情况下崩溃。