简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM