IntPtr gets operators !!!

Posted Thu, Nov 5 2009 11:48 by bill

This seems so incredibly long overdue, but at last as of .NET 4, IntPtr has + and – operators added to it !!

This means you can now easily write code such as :

    Dim ptr As IntPtr

    . . . .
    ptr += 4


this is great when dealing with offsets etc.


The actual implementation is kind of interesting. Here I’m seeing  the implementation as :

Public Shared Operator -(ByVal pointer As IntPtr, ByVal offset As Integer) As IntPtr
       Return New
IntPtr((pointer.ToInt32 - offset))
End Function


I guess this is because it is the 32 bit version of the library. Hopefully in the 64 bit version it calls on IntPtr.ToInt64 ;)

Filed under: , , , , ,


# re: IntPtr gets operators !!!

Thursday, November 05, 2009 12:13 PM by JaredPar

Yes, in the 64 bit version of mscorlib it uses .ToInt64