C# 十进制和十进制的区别

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

Difference between Decimal and decimal

c#class

提问by Matthew Jones

If someone could explain to me the difference between Decimal and decimal in C# that would be great.

如果有人能向我解释 C# 中十进制和​​十进制之间的区别,那就太好了。

In a more general fashion, what is the difference between the lower-case structs like decimal, int, string and the upper case classes Decimal, Int32, String.

以更一般的方式,小写结构(如十进制、整数、字符串)与大写类 Decimal、Int32、String 之间有什么区别。

Is the only difference that the upper case classes also wrap functions (like Decimal.Divide())?

唯一的区别是大写类也包装函数(如 Decimal.Divide())?

采纳答案by Martin

They are the same. The type decimal is an alias for System.Decimal.

他们是一样的。类型decimal 是System.Decimal 的别名。

So basically decimal is the same thing as Decimal. It's down to user's preference which one to use but most prefer to use int and string as they are easier to type and more familiar among C++ programmers.

所以基本上十进制与十进制是一样的。使用哪一种取决于用户的偏好,但大多数人更喜欢使用 int 和 string,因为它们更易于键入并且在 C++ 程序员中更熟悉。

回答by Bob

decimal, int, string are all just short hand notation to make things easier/prettier for you. The framework doesn't really know what a "decimal" is, but it does know System.Decimal, so when you compile your code, decimal just turns into System.Decimal. Try looking at some code where all the types are fully qualified, then try looking at some code where the aliases are used, I think most programmers will prefer the more compact aliases and perceive it as being easier to read. I also think it might be a throw back to C/C++ to make transitioning easier.

十进制、整数、字符串都只是简写符号,让您更轻松/更漂亮。该框架并不真正知道“十进制”是什么,但它确实知道 System.Decimal,因此当您编译代码时,十进制会变成 System.Decimal。尝试查看一些所有类型都是完全限定的代码,然后尝试查看一些使用别名的代码,我认为大多数程序员会更喜欢更紧凑的别名并认为它更易于阅读。我也认为这可能是回归 C/C++ 以使转换更容易。

回答by Anthony Mastrean

The built-in C# typesaren't all structs*. They are aliases for the predefined types in the System namespace. They are literally the same in all ways except formatting. The alias types are lowercase and formatted like keywords (dark blue). The System types are PascalCased and formatted like types (light blue).

内置C#类型并非所有的结构*。它们是 System 命名空间中预定义类型的别名。除了格式之外,它们在所有方面都完全相同。别名类型是小写的,格式类似于关键字(深蓝色)。系统类型是 PascalCased 和格式类似的类型(浅蓝色)。



*objectand stringare classes

*objectstring是类

回答by OmniSECT

As C# is a .NET language, all types must map to a .NET Framework Type.

由于 C# 是一种 .NET 语言,所有类型都必须映射到 .NET Framework 类型。

To answer your first question, decimal is an Alias of the System.Decimal .NET Framework type. They may be used interchangeably.

要回答您的第一个问题,decimal 是 System.Decimal .NET Framework 类型的别名。它们可以互换使用。

To answer your second question, both Decimal and decimal should extend the same functions, including both from the created variable and from the "Structure" of the value type itself.

要回答你的第二个问题,小数和小数都应该扩展相同的函数,包括来自创建的变量和值类型本身的“结构”。

decimal FirstDec = 12;
Decimal SecondDec = 13;
decimal ThirdDec = decimal.Ceiling(FirstDec, SecondDec);
Decimal FourthDec = Decimal.Floor(ThirdDec);
bool isEqual = FirstDec.Equals(SecondDec) && FourthDec.Equals(ThirdDec);

The following MSDN Page for Built-In Typeswill show you which System.ValueTypeeach alias maps to. And for Decimaland decimalspecifically, you can reference this MSDN Page for Decimal.

以下内置类型的 MSDN 页面将显示System.ValueType每个别名映射到哪个。而对于Decimaldecimal具体情况,你可以参考这个MSDN页面小数

回答by user3416507

Hadn't C# been case sensitive, some people would have written applications in all-caps as they did in Cobol and VB. This would have made it impossible for developers to read the code and maintain it. Case-sensitivity is a designer's effort to help making the language more usable.

如果 C# 不区分大小写,有些人会像在 Cobol 和 VB 中那样用大写字母编写应用程序。这将使开发人员无法阅读和维护代码。区分大小写是设计者帮助使语言更有用的努力。