dstang2000

分不清正数负数,是我错了,还是VS.NET错了

三年前做的一个ASP.NET的项目(www.SurveyStar.net),一直运行好好的,
前几天,客户打电话来说,出了问题。
反复跟踪,发现是一个正数、负数的判断与ToString()的问题。
将问题简化后,源程序可表示为只有4句的程序,如下:

class Test
{
    
static void Main()
    
{
        
int a = 0x79de61c0//2044617152;
        a +=    0x12345678;    
        
//a 应为 0x8c12b838;  //-1944930248

        
if( a< 0 ) a = -a;

        
//string str1 = a.ToString();
        System.Console.WriteLine( a );
        
//string str2 = a.ToString();
    }

}

按理说,这里的if(a<0) a=-a 应当将之转成了正数,但是问题来了:
如果在前面加上了str1一句,或者后面加上了str2一句,或者前后都加上,
显示的a居然是负数 -1944930248。这真是一个奇怪的问题。
是我错了,还是微软错了?

我查看了IL代码,没有找到问题,不知有无高人能指出其中的原因。
附:IL代码1:
#regin 不加str1及str2
.method 
private hidebysig static void  Main() cil managed
 // end of method Test::Main
#endregion
#regin 加上str1
.method 
private hidebysig static void  Main() cil managed
 // end of method Test::Main
#endregion

#regin 加上str2
.method 
private hidebysig static void  Main() cil managed
 // end of method Test::Main
#endregion



分类:

技术点:

相关文章:

  • 2021-12-12
  • 2021-10-16
  • 2021-06-16
  • 2021-04-23
  • 2018-04-28
  • 2021-06-14
  • 2021-08-03
  • 2021-07-29
猜你喜欢
  • 2021-09-16
  • 2021-12-12
  • 2021-10-08
  • 2020-03-11
  • 2021-09-08
  • 2021-05-28
  • 2022-01-23
相关资源
相似解决方案