C# 编译器数字文字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/166752/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:26:25  来源:igfitidea点击:

C# compiler number literals

c#compiler-construction

提问by Phil Wright

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

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

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...

默认情况下,声明 '0' 使其成为 Int32,而声明 '0.0' 使其成为 'Double'。我可以在末尾使用文字修饰符 'f' 来确保某些东西被视为'Single'。比如像这样...

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.

我可以使用哪些其他修饰符?是否有强制为 Double、Decimal、UInt32 的方法?我尝试谷歌搜索,但找不到任何东西。也许我的术语是错误的,所以这解释了为什么我会出现空白。非常感谢任何帮助。

采纳答案by user7116

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# specification2.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# 规范2.4.4.2 Integer literals 和 2.4.4.3 Real literals。请注意,为了清楚起见,L 和 UL 是首选,而不是它们的小写变体,正如Jon Skeet推荐的那样。

回答by Nic Wise

You might want to start by looking at the C# language spec. Most of the types are listed in there, and have a suffix:

您可能希望首先查看 C# 语言规范。大多数类型都列在那里,并有一个后缀:

  • L = long
  • F = float
  • U = uint
  • ulong's are a little different
  • m = decimal (money)
  • D = double
  • L = 长
  • F = 浮动
  • U = 单位
  • ulong 的有点不同
  • m = 十进制(钱)
  • D = 双倍

Of course, if you stop using varthen you get around the whole problem, and your code becomes more readable (ok, thats subjective, but for something like this, it's more readable by other people:

当然,如果你停止使用var那么你就解决了整个问题,你的代码变得更具可读性(好吧,这是主观的,但对于这样的事情,其他人更容易阅读:

var x = 0; //whats x?
float x = 0; //oh, it's a float
byte x = 0; // or not!

回答by Marc Gravell

If you don't want to have to remember them, then the compiler also accepts a cast for the same purpose (you can check the IL that the effect is the same - i.e. the compiler, not the runtime, does the cast). To borrow the earlier example:

如果您不想记住它们,那么编译器也接受出于相同目的的强制转换(您可以检查 IL 的效果是否相同 - 即编译器而不是运行时执行强制转换)。借用前面的例子:

    var y = (float)0; // y is single
    var z = (double)0; // z is double
    var r = (decimal)0; // r is decimal
    var i = (uint)0; // i is unsigned int
    var j = (long)0; // j is long
    var k = (ulong)0; // k is unsigned long

And for the record, I agree that "var" is a bad choice here; I'll happily use var for a SortedDictionary<SomeLongType, SomeOtherLongType>, but for "int" it is just lazy...

为了记录,我同意“var”在这里是一个糟糕的选择;我很乐意将 var 用于 SortedDictionary<SomeLongType, SomeOtherLongType>,但对于“int”,它只是懒惰...