设置在C#中使用十六进制整数文字负值 [英] Setting ints to negative values using hexadecimal literals in C#

查看:141
本文介绍了设置在C#中使用十六进制整数文字负值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法到设置 INT 来使用C#文字十六进制负值?我查了整数文字规范的,但它没有提到任何东西。

Is there any way to set an int to a negative value using a hexadecimal literal in C#? I checked the specification on Integer literals but it didn't mention anything.

例如:

int a = -1;         // Allowed
int b = 0xFFFFFFFF; // Not allowed?



十六进制表示法是我的应用程序更清晰,我宁愿不使用 UINT 因为我需要做一些额外的铸造。

Hexadecimal notation is clearer for my application, and I'd prefer not to use uints because I would need to do some extra casting.

推荐答案

使用未经检查的关键字。

Use the unchecked keyword.

unchecked
{
   int b = (int)0xFFFFFFFF;    
}



甚至更短。

or even shorter

int b = unchecked((int)0xFFFFFFFF);

这篇关于设置在C#中使用十六进制整数文字负值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆