The operation overflows at compile time in checked mode

本文讨论了在使用C#进行编程时遇到的一个关于长整型变量溢出的问题。作者发现当尝试将两个大的整数相乘并赋值给一个长整型变量时出现了错误,但在数值后面加上'L'后问题得到了解决。文章探讨了可能的原因,并提出了疑问。

Hello  Everybody
Line 19:         long long2 = 9223372036854775807;
Line 20: long imonth2 = 10 * 10000000000;
Line 21: long imonth = 10 * 1000000000;
Why 21 Line Error?  Line 19, 20 is no problem. I am using ASP2.0 and IIS6.
Many thanks. 

Hi,

If you append the character "L", it will work:

long imonth = 10 * 1000000000L;

I am still looking for the exact reason, but as far as I know, any literal numeric value (like 10000000) which is greater than an "int" is assumed a "long" by the compiler.
So why not this one?
In my opinion, the reason could be that the C# compiler is assuming a default value for the literal 1000000000 which could me something greater than what a long fits in, and unless we force it to makeit a long (by appending an "L"), an overflow error will occur. Ofcourse this is illogical, as in the case above this line, its working fine (even with an extra digit).

I'll post if I find something useful,

Vivek

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值