加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 无法添加IntPtr和Int

发布时间:2020-12-15 03:42:43 所属栏目:百科 来源:网络整理
导读:我在C#Visual Studio 2010中有这行 IntPtr a = new IntPtr(10);IntPtr b = a + 10; 它说: Operator ‘+’ cannot be applied to operands of type ‘System.IntPtr’ and ‘int’. MSDN says that this operation should work. 解决方法 如果你正在使用.net
我在C#Visual Studio 2010中有这行
IntPtr a = new IntPtr(10);
IntPtr b = a + 10;

它说:

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

MSDN says that this operation should work.

解决方法

如果你正在使用.net 4,那么你的代码将会工作.

对于早期版本,您需要使用IntPtr.ToInt64.

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

使用ToInt64而不是ToInt32,以便您的代码适用于32位和64位.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读