简体   繁体   English

C# UInt64 减法问题

[英]C# UInt64 subtract issue

Why subtraction with UInt64 data type sometimes error occurs.为什么 UInt64 数据类型的减法有时会发生错误。 I reduce the number of seconds timespan with the result should be between 1 - 10, but the results of the system can be millions or even billions?我减少时间跨度的秒数,结果应该在 1 - 10 之间,但是系统的结果可以是数百万甚至数十亿? Bugs?虫子?

private bool isReplayRequest(string kode, string rTs)
{
    if (System.Runtime.Caching.MemoryCache.Default.Contains(kode))
    {
        return true;
    }
    
    DateTime eStart = new DateTime(1970, 01, 01, 0, 0, 0, 0, DateTimeKind.Utc);
    TimeSpan cTs = DateTime.UtcNow - eStart;
    var stotalSeconds = Convert.ToUInt64(cTs.TotalSeconds);
    var rTotalSeconds = Convert.ToUInt64(rTs);
    
    if ((stotalSeconds - rTotalSeconds) > maxSeconds)
    {
        return true;
    }
    
    .... etc ....
}

Issue on line (stotalSeconds - rTotalSeconds)在线问题 (stotalSeconds - rTotalSeconds)

在此处输入图像描述

Not sure this is the solution to your problem – just my first thought不确定这是您问题的解决方案——只是我的第一个想法

By default the C# compiler does not check for arithmetic overflow默认情况下,C# 编译器不检查算术溢出

In Visual Studio you can enable this option in the properties of the project, build section, advanced settings在 Visual Studio 中,您可以在项目的属性、构建部分、高级设置中启用此选项

推进构建设置

Outside Visual Studio here are the compiler settings在 Visual Studio 之外,这里是编译器设置

-checked

or或者

<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>

Fabio法比奥

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM