I'm currently having a problem with unsafe pointers which appears to be a compiler bug.
Note: the problem is not with the fact that I am using pointers and unsafe code; the code works pretty well. The problem is related to a confirmed compiler bug that refuses to compile legal code under certain circumstances. If you're interested in that problem, visit my other question
Since my problem is with the declaration of pointer variables, I have decided to work around the problem, and use IntPtr
instead, and cast to actual pointers when needed.
However, I noticed that I cannot do something like this:
IntPtr a = something;
IntPtr b = somethingElse;
if (a > b) // ERROR. Can't do this
{
}
The >
and <
operators don't seem to be defined for IntPtr
. Notice that I can indeed compare two actual pointers.
IntPtr
has a .ToInt64()
method. However, this returns a signed value, which may return incorrect values when comparing with >
and <
when positive and negative values are involved.
To be honest, I don't really understand what use is there to a .ToInt64()
method that returns a signed value, considering that pointer comparisons are performed unsigned, but that's not my question.
One could argue that IntPtr
s are opaque handles, and it is therefore meaningless to compare with >
and <
. However, I would point out that IntPtr
has addition and subtraction methods, which mean that there is actually a notion of order for IntPtr
, and therefore >
and <
are indeed meaningful.
I guess I could cast the result of ToInt64()
to a ulong
and then compare, or cast the IntPtr
to a pointer and then do the comparison, but it makes me think why aren't >
and <
defined for IntPtr
.
Why can't I compare two IntPtr
s directly?