如何正确编写这个函数?每次都让我失忆

发布于 2024-12-15 11:56:13 字数 989 浏览 2 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷血 2024-12-22 11:56:13

您应该更改:

myTime.AddSeconds(1);

// The same for all other methods which considered to modify 
// a DateTime value including your custom AddTime() method
myTime = myTime.AddSeconds(1);

这种方式,条件 Time 的值将发生更改,以便条件 (Time <= EndPeriod) 将永远被通过。

所有方法,例如 DateTime.AddYears() DateTime.AddDays() 不修改对象的值本身但返回新值。

微软软件定义网络:

此方法不会更改此 DateTime 对象的值。
相反,它返回一个新的 DateTime 对象,其值是以下结果
此操作。

You should change:

myTime.AddSeconds(1);

to

// The same for all other methods which considered to modify 
// a DateTime value including your custom AddTime() method
myTime = myTime.AddSeconds(1);

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:

This method does not change the value of this DateTime object.
Instead, it returns a new DateTime object whose value is the result of
this operation.

﹎☆浅夏丿初晴 2024-12-22 11:56:13

堆栈很可能因大量 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.

我是有多爱你 2024-12-22 11:56:13

(假设代码上有拼写错误并在此处更正它...

Time.AddTime(Time);

这会修改 Time 吗?看起来不像,至少不是直观上的。如果您不更改 的值Time 那么循环将无限地运行,永远不会达到其终止条件:

Time = Time.AddTime(Time);

此外,在方法内部使用诸如 myTime.AddYears() 和之类的东西。其他此类方法,您需要分配它们的返回值 。

作为一般的设计问题,您应该小心哪些方法会修改哪些对象,我承认 DateTime 上的 .AddXX() 方法是 在这方面不是很直观。

(assuming a typo on the code and correcting it here...

Time.AddTime(Time);

Does this modify Time? Doesn't look like it, at least not intuitively. If you're not changing the value of Time then the loop is going to run endlessly, never reaching its terminating condition. Try assigning the return value:

Time = Time.AddTime(Time);

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 on DateTime aren't very intuitive in that regard.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文