“Byte”的后缀(类型字符)是什么? VB.NET 中的数字常量?

发布于 2024-07-08 00:51:07 字数 282 浏览 6 评论 0原文

只是出于好奇:

我知道我可以告诉编译器我是否希望将一个值解释为某种数字类型,例如整数(32 位有符号),这样可以将“I”(类型字符)附加到常量值:

Private Function GetTheAnswerAsInteger() As Integer

   Return 42I

End Function

还有表示 Short 的“S”、表示 Decimal 的“D”等。

但是 Byte 的后缀是什么? 提示:这不是明显的“B”......

Just out of curiosity:

I know I can tell the compiler if I want a value to be interpreted as a certain numeric type, e.g. as Integer (32 bit signed), this way appending an "I" (type character) to the constant value:

Private Function GetTheAnswerAsInteger() As Integer

   Return 42I

End Function

There's also "S" for Short, "D" for Decimal, etc.

But what is the suffix for Byte? Hint: it's not the obvious one "B"...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜无邪 2024-07-15 00:51:07

没有一个。 如果您需要区分常量的整数和字节(例如,调用适当的重载),则需要进行强制转换。

(顺便说一下,C# 也是如此。)

MSDN 提供了确认:

Byte 没有文字类型字符或
标识符类型字符。

还有一个类型字符和文字后缀列表

There isn't one. If you need to distinguish between an integer and a byte (e.g. to call an appropriate overload) for a constant, you need to cast.

(The same is true in C#, by the way.)

MSDN provides confirmation:

Byte has no literal type character or
identifier type character.

There's also a list of type characters and literal suffixes.

゛时过境迁 2024-07-15 00:51:07

所以,我们去年秋天在 VB 中添加了二进制文字并得到了类似的反馈
来自早期测试者。 我们确实决定为 VB 添加字节后缀。 我们
选择 SB(有符号字节)和 UB(无符号字节)。 原因
不只是 B 和 SB 是双重的。

第一,如果您以十六进制书写,则 B 后缀是不明确的(什么
0xFFB 是什么意思?)即使我们有一个解决方案,或者另一个
比“B”字符(“Y”被考虑,F#使用这个)没有人可以
记住默认值是有符号的还是无符号的 - .NET 字节是
默认情况下未签名,因此选择 B 和 SB 是有意义的,但所有
其他后缀默认签名,因此保持一致
与其他类型后缀选择 B 和 UB。 最后我们去了
明确的 SB 和 UB。
——安东尼·D·格林,

https://roslyn.codeplex.com/discussions/542111

已集成到即将发布的 VB.NET 版本中,这就是它的工作方式:

Public Const MyByte As Byte = 4UB;
Public Const MyByte2 As SByte = 4SB;

So, we added binary literals in VB last fall and got similar feedback
from early testers. We did decide to add a suffix for byte for VB. We
settled on SB (for signed byte) and UB (for unsigned byte). The reason
it's not just B and SB is two-fold.

One, the B suffix is ambiguous if you're writing in hexadecimal (what
does 0xFFB mean?) and even if we had a solution for that, or another
character than 'B' ('Y' was considered, F# uses this) no one could
remember whether the default was signed or unsigned - .NET bytes are
unsigned by default so it would make sense to pick B and SB but all
the other suffixes are signed by default so it would be consistent
with other type suffixes to pick B and UB. In the end we went for
unambiguous SB and UB.
-- Anthony D. Green,

https://roslyn.codeplex.com/discussions/542111

It has been integrated to the upcoming VB.NET release, and this is the way it will work:

Public Const MyByte As Byte = 4UB;
Public Const MyByte2 As SByte = 4SB;
月下伊人醉 2024-07-15 00:51:07

这个答案并没有真正提供后缀,但它已经尽可能接近了。

如果您将扩展方法定义为

Imports System.Runtime.CompilerServices

Module IntegerExtensions

    <Extension()> _
    Public Function B(ByVal iNumber As Integer) As Byte
        Return Convert.ToByte(iNumber)
    End Function

End Module

您可以像这样使用它:

Private Function GetTheAnswerAsByte() As Byte

   Return 42.B

End Function

This answer does not really provide a suffix, but it's as close as it gets.

If you define an extension method as

Imports System.Runtime.CompilerServices

Module IntegerExtensions

    <Extension()> _
    Public Function B(ByVal iNumber As Integer) As Byte
        Return Convert.ToByte(iNumber)
    End Function

End Module

You can use it like this:

Private Function GetTheAnswerAsByte() As Byte

   Return 42.B

End Function
请帮我爱他 2024-07-15 00:51:07

.NET 中没有字节文字。

There's no byte literal in .NET.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文