C# 数字常量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# numeric constants
提问by Rob Walker
I have the following C# code:
我有以下 C# 代码:
byte rule = 0;
...
rule = rule | 0x80;
which produces the error:
产生错误:
Cannot implicitly convert type 'int' to 'byte'. An explicit conversion exists (are you missing a cast?)
无法将类型“int”隐式转换为“byte”。存在显式转换(您是否缺少演员表?)
[Update: first version of the question was wrong ... I misread the compiler output]
[更新:问题的第一个版本是错误的......我误读了编译器输出]
Adding the cast doesn'tfix the problem:
添加演员并不能解决问题:
rule = rule | (byte) 0x80;
I need to write it as:
我需要把它写成:
rule |= 0x80;
Which just seems weird. Why is the |=
operator any different to the |
operator?
这看起来很奇怪。为什么|=
运营商与运营商有什么不同|
?
Is there any other way of telling the compiler to treat the constant as a byte?
有没有其他方法可以告诉编译器将常量视为一个字节?
@ Giovanni Galbo: yes and no. The code is dealing with the programming of the flash memory in an external device, and logically represents a single byte of memory. I could cast it later, but this seemed more obvious. I guess my C heritage is showing through too much!
@乔瓦尼加尔博:是也不是。该代码处理外部设备中闪存的编程,逻辑上表示存储器的单个字节。我可以稍后施放它,但这似乎更明显。我想我的 C 语言遗产暴露得太多了!
@ Jonathon Holland: the 'as' syntax looks neater but unfortunately doesn't appear to work ... it produces:
@ Jonathon Holland:'as' 语法看起来更简洁,但不幸的是似乎不起作用......它产生:
The as operator must be used with a reference type or nullable type ('byte' is a non-nullable value type)
as 运算符必须与引用类型或可空类型一起使用('byte' 是不可为空的值类型)
采纳答案by David J. Sokol
int rule = 0;
rule |= 0x80;
http://msdn.microsoft.com/en-us/library/kxszd0kx.aspxThe | operator is defined for all value types. I think this will produced the intended result. The "|=" operator is an or then assign operator, which is simply shorthand for rule = rule | 0x80.
http://msdn.microsoft.com/en-us/library/kxszd0kx.aspx的| 运算符是为所有值类型定义的。我认为这将产生预期的结果。"|=" 运算符是一个 or then 赋值运算符,它只是 rule = rule | 的简写。0x80。
One of the niftier things about C# is that it lets you do crazy things like abuse value types simply based on their size. An 'int' is exactly the same as a byte, except the compiler will throw warnings if you try and use them as both at the same time. Simply sticking with one (in this case, int) works well. If you're concerned about 64bit readiness, you can specify int32, but all ints are int32s, even running in x64 mode.
C# 更妙的地方之一是,它可以让你做一些疯狂的事情,比如仅仅根据值类型的大小来滥用值类型。'int' 与字节完全相同,但如果您尝试同时使用它们,编译器会抛出警告。简单地坚持一个(在这种情况下,int)效果很好。如果您担心 64 位就绪,您可以指定 int32,但所有 int 都是 int32,即使在 x64 模式下运行也是如此。
回答by sectrean
Looks like you may just have to do it the ugly way: http://msdn.microsoft.com/en-us/library/5bdb6693.aspx.
看起来你可能只需要以丑陋的方式去做:http: //msdn.microsoft.com/en-us/library/5bdb6693.aspx。
回答by Eric Z Beard
C# does not have a literal suffix for byte. u = uint, l = long, ul = ulong, f = float, m = decimal, but no byte. You have to cast it.
C# 没有字节的文字后缀。u = uint,l = long,ul = ulong,f = float,m = 十进制,但没有字节。你必须投它。
回答by FlySwat
The term you are looking for is "Literal" and unfortunately C# does not have a byte literal.
您正在寻找的术语是“文字”,不幸的是 C# 没有字节文字。
Here's a list of all C# literals.
这是所有 C# 文字的列表。
回答by grom
According to the ECMA Specification, pg 72there is no byte literal. Only integer literals for the types: int, uint, long, and ulong.
根据ECMA 规范,第 72 页没有字节文字。类型只有整数文字:int、uint、long 和 ulong。
回答by Peter Meyer
Unfortunately, your only recourse is to do it just the way you have. There is no suffix to mark the literal as a byte. The | operator does not provide for implicit conversion as an assignment (i.e. initialization) would.
不幸的是,你唯一的办法就是按照你的方式去做。没有后缀将文字标记为字节。的| 运算符不像赋值(即初始化)那样提供隐式转换。
回答by jmatthias
This works:
这有效:
rule = (byte)(rule | 0x80);
Apparently the expression 'rule | 0x80' returns an int even if you define 0x80 as 'const byte 0x80'.
显然表达式'规则| 即使您将 0x80 定义为 'const byte 0x80',0x80' 也会返回一个 int。
回答by jfs
Apparently the expression 'rule | 0x80' returns an int even if you define 0x80 as 'const byte 0x80'.
显然表达式'规则| 即使您将 0x80 定义为 'const byte 0x80',0x80' 也会返回一个 int。
I think the rule is numbers like 0x80 defaults to int unless you include a literal suffix. So for the expression rule | 0x80
, the result will be an int since 0x80 is an int and rule (which is a byte) can safely be converted to int.
我认为规则是像 0x80 这样的数字默认为 int ,除非您包含文字后缀。所以对于表达式rule | 0x80
,结果将是一个 int,因为 0x80 是一个 int 并且规则(这是一个字节)可以安全地转换为 int。
回答by Bruce Bowman
According to the C standard, bytes ALWAYS promote to int in expressions, even constants. However, as long as both values are UNSIGNED, the high-order bits will be discarded so the operation should return the correct value.
根据 C 标准,字节总是在表达式中提升为 int,甚至是常量。但是,只要两个值都是 UNSIGNED,高位将被丢弃,因此操作应该返回正确的值。
Similarly, floats promote to double, etc.
同样,浮点数提升为双倍等。
Pull out of copy of K&R. It's all in there.
拉出 K&R 的副本。都在里面
回答by arx
Almost five years on and nobody has actually answered the question.
将近五年过去了,没有人真正回答过这个问题。
A couple of answers claim that the problem is the lack of a byte literal, but this is irrelevant. If you calculate (byte1 | byte2)
the result is of type int
. Even if "b" were a literal suffix for byte the type of (23b | 32b)
would still be int
.
几个答案声称问题在于缺少字节文字,但这无关紧要。如果计算(byte1 | byte2)
结果是类型int
. 即使“b”是字节的字面后缀,类型(23b | 32b)
仍然是int
.
The accepted answer links to an MSDN article claiming that operator|
is defined for all integral types, but this isn't true either.
接受的答案链接到 MSDN 文章,声称operator|
为所有整数类型定义,但这也不是真的。
operator|
is not defined on byte
so the compiler uses its usual overload resolution rules to pick the version that's defined on int
. Hence, if you want to assign the result to a byte
you need to cast it:
operator|
未定义,byte
因此编译器使用其通常的重载解析规则来选择在 上定义的版本int
。因此,如果要将结果分配给 a byte
,则需要对其进行转换:
rule = (byte)(rule | 0x80);
The question remains, why does rule |= 0x80;
work?
问题仍然存在,为什么rule |= 0x80;
有效?
Because the C# specification has a special rule for compound assignment that allows you to omit the explicit conversion. In the compound assignment x op= y
the rule is:
因为 C# 规范有一个特殊的复合赋值规则,允许您省略显式转换。在复合赋值中x op= y
,规则是:
if the selected operator is a predefined operator, if the return type of the selected operator is explicitly convertible to the type of x, and if y is implicitly convertible to the type of x or the operator is a shift operator, then the operation is evaluated as
x = (T)(x op y)
, where T is the type of x, except that x is evaluated only once.
如果所选运算符是预定义的运算符,如果所选运算符的返回类型可显式转换为 x 的类型,并且 y 可隐式转换为 x 的类型或运算符是移位运算符,则计算该操作as
x = (T)(x op y)
,其中 T 是 x 的类型,只是 x 只计算一次。