Why can't I compare IntPtr.Zero and default (IntPtr)?

I just learned that IntPtr.Zero cannot match the default (IntPtr). Can someone tell me why?

IntPtr.Zero == new IntPtr(0) -> "could not evaluate expression" IntPtr.Zero == default(IntPtr) --> "could not evaluate expression" IntPtr.Zero == (IntPtr)0 -> "could not evaluate expression" IntPtr.Zero.Equals(IntPtr.Zero) --> "Enum value was out of legal range" exception IntPtr.Zero.Equals(default(IntPtr)) --> "Enum value was out of legal range" exception IntPtr.Zero == IntPtr.Zero --> true new IntPtr(0) == new IntPtr(0) --> true 
+6
source share
1 answer

Works for me in compiled code in VS 2010, VS 2008, VS 2005 SP1, Mono 1.2.6. It is controlled by reproducing both problems in the window of the Visual Studio 2005 viewer window (I tried with VS 2005 SP1), the compiled code works as expected. (For both tasks, I mean problem 1: β€œThe expression could not be evaluated,” problem 2: β€œEnum was out of range”.) Thus, as some commentators noted, this is a VS 2005 window that you stumbled upon . It is surprisingly difficult to quickly find a link to the corresponding error report ...

Otherwise, I would start with a reflection to see what types you are trying to compare (replace Console.Out with any meaningful stream you have access to):

 Console.WriteLine("new IntPtr(0) type is: " + new IntPtr(0).GetType()); Console.WriteLine("IntPtr.Zero type is: " + IntPtr.Zero.GetType()); 
+2
source

Source: https://habr.com/ru/post/917451/


All Articles