什么是最糟糕的疑难杂症在C#或.NET? [英] What is the worst gotcha in C# or .NET?
问题描述
我最近正在与的DateTime
对象,并写了这样的事情:
DateTime的DT = DateTime.Now;
dt.AddDays(1);
返回DT; //还是今天的日期!跆拳道?
智能感知文档 AddDays()
表示,它增加了一个一天的日期,它没有 - 它实际上的返回的日期用了一天添加到它,所以你必须把它写这样的:
DateTime的DT = DateTime.Now;
dt的= dt.AddDays(1);
返回DT; //明天的日期
这其中有前咬了我很多次,所以我认为编目最差的C#陷阱将是有益的。
私人诠释myVar的;
公众诠释为MyVar
{
{返回MyVar的; }
}
Blammo。您的应用程序崩溃,没有堆栈跟踪。发生的时间。
(注意资金为MyVar
,而不是在吸气小写 myVar的
)
I was recently working with a DateTime
object, and wrote something like this:
DateTime dt = DateTime.Now;
dt.AddDays(1);
return dt; // still today's date! WTF?
The intellisense documentation for AddDays()
says it adds a day to the date, which it doesn't - it actually returns a date with a day added to it, so you have to write it like:
DateTime dt = DateTime.Now;
dt = dt.AddDays(1);
return dt; // tomorrow's date
This one has bitten me a number of times before, so I thought it would be useful to catalog the worst C# gotchas.
private int myVar;
public int MyVar
{
get { return MyVar; }
}
Blammo. Your app crashes with no stack trace. Happens all the time.
(Notice capital MyVar
instead of lowercase myVar
in the getter.)
这篇关于什么是最糟糕的疑难杂症在C#或.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!