为什么一些后缀有必要吗

为什么一些后缀有必要吗

本文介绍了为什么一些后缀有必要吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#语言(和其他语言我敢肯定),要求在数字文字的结尾后缀。这些后缀表明文字的类型。例如, 5M 是一个十进制数, 1006米是一个浮点数。

The C# language (and other languages I'm sure) require suffixes at the end of numeric literals. These suffixes indicate the type of the literal. For example, 5m is a decimal, 5f is a floating point number.

我的问题是:这些后缀确有必要,或者是有可能从它的上下文推断出一个文字的类型

My question is: are these suffixes really necessary, or is it possible to infer the type of a literal from its context?

例如,代码十进制D = 5.0 应推断 5.0 是不是双,但小数。 ?请问那种语法原因问题

For example, the code decimal d = 5.0 should infer that 5.0 is not a double, but a decimal. Does that kind of grammar cause problems?

推荐答案

这是对简单的场合,如:

That's fine for simple cases like:

float f = 7;



但它远不如做个明确的,这样你就不用担心类似的语句:

but it's far better to be explicit so that you don't have to worry about statements like:

float f = (double)(int)(1 / 3 + 6e22 / (double)7);



是的,我知道这是一个人为的例子,但我们的的认识编码器从刚刚类型左边的意图的尤其的,如果它没有被分配给所有的变量(如作为参数传递给一个重载函数,可以带参数之一传递,浮动,双,小数等)。

Yes, I know that's a contrived example but we don't know the intent of the coder from just the type on the left, especially if it's not being assigned to a variable at all (such as being passed as an argument to an overloaded function that can take one of int, float, double, decimal et al).

这篇关于为什么一些后缀有必要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 14:26