我们为什么要使用C#文字? [英] Why should we use literals in C#?

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

问题描述

在一些C#代码,我已经看到了staments是这样的:

In some C# code I have seen staments like this:

float someFloat = 57f;



我想知道为什么我们应该使用像文字˚F在上述情况?

推荐答案

在F上面是一个类型的后缀。这告诉编译器提供的文字的确切类型。这是用来使编译器可以分配的存储(精度)的字面适量。缺省情况下,浮点文字给出存储为一个双重。如果添加F作为后缀,文字只会得到一个浮动,这将有精度较差的存储空间。

The "f" above is a type suffix. This tells the compiler the exact type of the literal provided. This is used so the compiler can allocate the appropriate amount of storage (precision) for the literal. By default, floating point literals are given storage for a "double." If you add "f" as a suffix, the literal will only get the storage for a float, which will have less accuracy.

double d = 50.1234; // given double storage
double d = 50.1234f; // given float storage, which might lose precision compared to double

这篇关于我们为什么要使用C#文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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