C#编译器字面量 [英] C# compiler number literals

查看:161
本文介绍了C#编译器字面量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道C#编译器数字文字修饰符的完整列表?

Does anyone know the full list of C# compiler number literal modifiers?

默认声明'0'使它成为一个Int32,'0.0' '。我可以在结尾使用文字修饰符'f',以确保某些东西被视为单一。例如像这样...

By default declaring '0' makes it an Int32 and '0.0' makes it a 'Double'. I can use the literal modifier 'f' at the end to ensure something is treated as a 'Single' instead. For example like this...

var x = 0;    // x is Int32
var y = 0f;   // y is Single

我可以使用的其他修饰符是什么?有一个强制为双精度,十进制,UInt32?我试图谷歌搜索这个,但找不到任何东西。也许我的术语是错误的,所以这就解释了为什么我来了空白。任何帮助非常感激。

What are the other modifiers I can use? Is there one for forcing to Double, Decimal, UInt32? I tried googling for this but could not find anything. Maybe my terminology is wrong and so that explains why I am coming up blank. Any help much appreciated.

推荐答案

var y = 0f; // y is single
var z = 0d; // z is double
var r = 0m; // r is decimal
var i = 0U; // i is unsigned int
var j = 0L; // j is long (note capital L for clarity)
var k = 0UL; // k is unsigned long (note capital L for clarity)

C#规范 2.4.4.2整数文字和2.4.4.3实数文字。请注意,根据 Jon Skeet 的建议,为了清晰起见,L和UL是首选,而不是小写形式。

From the C# specification 2.4.4.2 Integer literals and 2.4.4.3 Real literals. Take note that L and UL are preferred as opposed to their lowercase variants for clarity as recommended by Jon Skeet.

这篇关于C#编译器字面量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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