Arithmetic overflow exception in .net 2.0 and greater

I'm getting this error

System.OverflowException: Arithmetic operation resulted in an overflow.

when i ran my application on Windows Server 2008 R2 Standard compiled in .Net 2.0 or greater (.Net 4.0). From what i know, C# should ignore Arithmetic overflow if it is compiled without /checked parameter. In my app there are many places where overflow can happened, so i need to ignore it.

I traced one example:

using System;

namespace ArithmeticOverflow
{
    class Program
    {
        static void Main( string[] args )
        {
            GetTypeID( typeof( Program ) );
        }

        public static int GetTypeID( Type type )
        {
            return type.GetHashCode() ^ type.FullName.GetHashCode() ^ type.TypeHandle.Value.ToInt32();
        }
    }
}

Compiled with .net 2.0:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\csc.exe /out:test.exe Program.cs

When i run this program on my desktop computer, everything go fine. But when i run it on server, it crash. I cannot find the problem.

If i compile it with .net 1.1:

C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\csc.exe /out:test.exe Program.cs

It is alright on desktop PC and on server too. So where is the problem ? Please help.

UPDATE

Solved by using /platform:anycpu32bitpreferred

C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\csc.exe /unsafe /platform:anycpu32bitpreferred /out:test.exe Program.cs
Jon Skeet
people
quotationmark

I suspect you'll find the problem is that with .NET 1.1 you're running the x86 CLR in both cases, whereas with .NET 2.0 you're probably running with x86 on your desktop an x64 on your server.

IntPtr.ToInt32 is documented to throw OverflowException when:

On a 64-bit platform, the value of this instance is too large or too small to represent as a 32-bit signed integer.

Basically, it's dangerous to call that. Why not just use IntPtr.GetHashCode()? (It's not clear what exactly you're trying to achieve with this code anyway, to be honest.)

people

See more on this question at Stackoverflow