Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't add an IntPtr and an Int

I have this lines in C# Visual Studio 2010:

IntPtr a = new IntPtr(10);
IntPtr b = a + 10;

And it says:

Operator '+' cannot be applied to operands of type 'System.IntPtr' and 'int'.

MSDN says that this operation should work.

like image 780
Sp3ct3R Avatar asked Oct 01 '11 15:10

Sp3ct3R


1 Answers

If you are targetting .net 4 then your code will work.

For earlier versions you need to use IntPtr.ToInt64.

IntPtr a = new IntPtr(10);
IntPtr b = new IntPtr(a.ToInt64()+10);

Use ToInt64 rather than ToInt32 so that your code works for both 32 and 64 bit.

like image 102
David Heffernan Avatar answered Oct 11 '22 01:10

David Heffernan