在 C# 中使用结构体获取简单的域值

发布于 2024-08-06 03:48:48 字数 196 浏览 2 评论 0原文

我正在编写一个财务应用程序,其中经常使用“价格”的概念。它当前由 C# decimal 类型表示。我想让它更明确,并且能够在将来将其更改为 double ,因此我正在考虑创建一个“Price”结构,其基本上与十进制类型完全相同(也许添加一些验证,例如必须大于 0)。

您认为这样做的优点和缺点是什么?

I am writing a financial application where the concept of 'Price' is used a lot. It's currently represented by the C# decimal type. I would like to make it more explicit and be able to change it to maybe double in the future, so I was thinking of creating a 'Price' struct that would basically act exactly the same as the decimal type (maybe add a bit of validation like must be greater than 0).

What do you think are the pros and cons of doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

神仙妹妹 2024-08-13 03:48:48

不要使用双倍来赚钱。您必须记住对其进行四舍五入以便在您使用它的任何地方进行显示,并且如果您除以或乘以大数,则可能会出现准确性问题。 Decimal 会产生溢出错误,double 只会失去准确性。我不确定你的看法,但对于金钱来说,我宁愿选择错误和中止的操作,也不愿默默地继续进行,但会失去准确性。

如果有的话,根据我参与过的项目,您可能需要考虑使用具有小数点和一些货币指示的结构。

Please don't use double for money. You'll have to remember to round it for display everywhere you use it at, and you have potential accuracy issues if you divide or multiply by large numbers. Decimal will give overflow errors, double will just lose accuracy. I'm not sure about you, but with money, I'd prefer an error and aborted operation to silently proceeding with a loss of accuracy.

If anything, based on projects I've been on, you may want to consider using a struct that has a decimal and some indication of what currency it is.

夏日浅笑〃 2024-08-13 03:48:48

结构应该用于(在我看来)不可变的小型类型,即值类型。我不确定“经常使用”是什么意思,但是如果这些结构将在性能关键操作中大量传递,您将必须考虑复制它们的价格与堆分配的价格。我怀疑你是否需要考虑到这一点,但这是值得考虑的事情。我很少发现在日常活动中需要使用结构。

此外,正如 Jonathan 指出的那样,使用 double 类型来获取金钱并不是一个好主意。小数类型更适合财务计算。

还有一个旁白;您可能会听到很多讨论堆栈与堆分配的回复,因此您可能会对本文感兴趣:

http://blogs.msdn.com/ericlippert/archive/2009/04/27/the-stack-is-an-implementation-detail .aspx

Structs should be used for small types that will (in my opinion) be immutable, i.e., value types. I am not sure what you mean by "used a lot", but if these structs will be passed around a lot in performance critical operations you will have to take into account the price of copying them versus the price of heap allocation. I doubt you will need to take that into account, but it is something to think about. I rarely find the need to use structs in my daily activities.

Also, as Jonathan points out, using the double type for money is a bad idea. The decimal type is much better suited to financial calculations.

Yet another aside; you will probably hear a lot of responses which talk about stack v heap allocation, so this article may interest you:

http://blogs.msdn.com/ericlippert/archive/2009/04/27/the-stack-is-an-implementation-detail.aspx

撕心裂肺的伤痛 2024-08-13 03:48:48

不应该有理由更改这样的数量的数据类型;但是,您可能决定添加其他信息(例如货币或小数位数)以在计算中跟踪,因此此时使用结构体将为您节省大量时间。

There shouldn't be a reason to change the data type for a quantity like this; however, you may decide to add other information such as the currency or the number of decimal places to keep track of in calculations, so using a struct at this point will save you a LOT of time down the road.

一紙繁鸢 2024-08-13 03:48:48

结构体可能无法通过 C# 以外的 .NET 语言进行访问。舍入误差也可能是一个问题。为什么不直接创建一个 Money 类并将值存储为 Decimal 和所使用的货币。

Structs may not be so accessible from .NET languages other than C#. Rounding errors could be a problem too. Why not just create a Money class and store the value as a Decimal and the currency used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文