9

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.

svick
  • 236,525
  • 50
  • 385
  • 514
Sp3ct3R
  • 715
  • 1
  • 12
  • 24

1 Answers1

19

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490