C# 编译器数字文字 [英] C# compiler number literals

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

问题描述

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

默认情况下,声明 '0' 使其成为 Int32 和 '0.0' 使其成为 'Double'.我可以在末尾使用文字修饰符f"来确保将某些内容视为单一".比如像这样……

var x = 0;//x 是 Int32变量 y = 0f;//y 是单一的

我可以使用哪些其他修饰符?是否有一个强制加倍,​​十进制,UInt32?我试着用谷歌搜索,但找不到任何东西.也许我的术语是错误的,所以这解释了为什么我会出现空白.非常感谢任何帮助.

解决方案

var y = 0f;//y 是单一的变量 z = 0d;//z 是双倍的变量 r = 0m;//r 是十进制变量 i = 0U;//i 是无符号整数变量 j = 0L;//j 很长(为清楚起见,请注意大写 L)变量 k = 0UL;//k 是无符号长整数(为清楚起见,请注意大写 L)

来自 C# 规范 2.4.4.2 整数文字和 2.4.4.3 实数.请注意,Jon Skeet 推荐的 L 和 UL 是首选,而不是小写变体.p>

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

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

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)

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天全站免登陆