如何正确编写这个函数?每次都让我失忆
我的 While 循环中运行了一些代码:
while (Time <= EndPeriod)
{
... code ...
Tine.AddTime(Time);
}
我遇到的问题是 Time.AddTime(Time) 执行的位置会造成内存泄漏。 AddTime 函数检查类属性,并根据开关中的 DateFormat 变量通过正确的方法(例如 AddDays、AddMonths 等)添加时间。如果我将开关放入循环中,一切都很好,但是如果我尝试引用此函数,则会出现堆栈溢出。
我的 AddTime 函数如下:
protected DateTime AddTime(DateTime Time)
{
DateTime myTime = Time;
switch (DatePart) {
case "yy":
myTime.AddYears(1);
break;
case "qq":
myTime.AddDays(1);
break;
case "mm":
myTime.AddMonths(1);
break;
case "dd":
myTime.AddDays(1);
break;
case "hh":
myTime.AddHours(1);
break;
case "ss":
myTime.AddSeconds(1);
break;
default:
myTime.AddMinutes(1);
break;
}
return myTime;
}
我可能做错了什么?
谢谢
I have some code running in my While loop:
while (Time <= EndPeriod)
{
... code ...
Tine.AddTime(Time);
}
The problem I am having is where Time.AddTime(Time) executes it creates a memory leak. The AddTime function checks a class property and adds time via the correct method (e.g. AddDays, AddMonths, etc...) based on the DateFormat variable in a switch. If I put the switch into the loop everything is fine, however if I try to reference this function it does a stack overflow.
My AddTime function is below:
protected DateTime AddTime(DateTime Time)
{
DateTime myTime = Time;
switch (DatePart) {
case "yy":
myTime.AddYears(1);
break;
case "qq":
myTime.AddDays(1);
break;
case "mm":
myTime.AddMonths(1);
break;
case "dd":
myTime.AddDays(1);
break;
case "hh":
myTime.AddHours(1);
break;
case "ss":
myTime.AddSeconds(1);
break;
default:
myTime.AddMinutes(1);
break;
}
return myTime;
}
What could I be doing wrong?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该更改:
以
这种方式,条件
Time
的值将发生更改,以便条件(Time <= EndPeriod)
将永远被通过。所有方法,例如 DateTime.AddYears() DateTime.AddDays() 不修改对象的值本身但返回新值。
微软软件定义网络:
You should change:
to
In this way condition a value of
Time
would be changes so you have a chance that condition(Time <= EndPeriod)
will be passed ever.All methods like DateTime.AddYears() DateTime.AddDays() does not modify a value of object itself but returns new value.
MSDN:
堆栈很可能因大量 DateTime 分配而溢出。尝试将 DateTime 移到循环之外 &不断地覆盖它。
这里的想法是,b/c DateTime 分配到堆栈而不是堆,这可能会导致 Caling thead(while 循环)溢出。
It's likely that the stack is overflowing from numerous DateTime allocations. Try moving the DateTime outside of the loop & constantly overwriting it.
The thought here is that b/c DateTime is allocated to the stack rather than heap it may cause the caling thead (while loop) to overflow.
(假设代码上有拼写错误并在此处更正它...
这会修改
Time
吗?看起来不像,至少不是直观上的。如果您不更改的值Time
那么循环将无限地运行,永远不会达到其终止条件:此外,在方法内部使用诸如
myTime.AddYears()
和之类的东西。其他此类方法,您需要分配它们的返回值 。作为一般的设计问题,您应该小心哪些方法会修改哪些对象,我承认
DateTime
上的.AddXX()
方法是 在这方面不是很直观。(assuming a typo on the code and correcting it here...
Does this modify
Time
? Doesn't look like it, at least not intuitively. If you're not changing the value ofTime
then the loop is going to run endlessly, never reaching its terminating condition. Try assigning the return value:Additionally, inside of the method where you use things like
myTime.AddYears()
and other such methods, you need to assign their return values to your variable as well.As a general matter of design you should be careful with which methods modify which objects. I admit that the
.AddXX()
methods onDateTime
aren't very intuitive in that regard.