.NET 中的运算符重载

发布于 2024-08-03 19:57:52 字数 31 浏览 7 评论 0原文

在什么情况下您会考虑重载 .NET 中的运算符?

In what situations would you consider overloading an operator in .NET?

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

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

发布评论

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

评论(5

我要还你自由 2024-08-10 19:57:52
  • 我会强烈考虑在覆盖 Equals 的任何地方重载 == 和 !=
  • 我会在实现 IComparable 的任何地方考虑(不太强烈)重载比较运算符
  • 我会考虑重载基本数字类型的算术运算符
  • 我会考虑为“包装”类型(例如 Nullable)提供显式转换
  • 非常很少考虑提供隐式转换

黄金法则是如果含义不完全明显,重载运算符。例如,我认为在 Stream 上有一个 + 运算符会很奇怪 - 它可能意味着“在这里创建一个可写的 T,以便写入结果写入两者”或者它可能意味着“一篇接着一篇地读”或者可能是其他的东西。

根据我的经验,除了 == 和 != 之外,很少会重载任何内容。

  • I would strongly consider overloading == and != anywhere I override Equals
  • I would consider (much less strongly) overloading comparison operators anywhere I implement IComparable<T>
  • I would consider overloading arithmetic operators for fundamentally numeric types
  • I would consider providing explicit conversions for "wrapper" types (like Nullable<T>)
  • I would very rarely consider providing implicit conversions

The golden rule is not to overload operators if the meaning isn't entirely obvious. For example, I think it would be pretty odd to have a + operator on Stream - it could mean "make a writable T here, so that writes to the result write to both" or it could mean "read one after the other" or probably other things.

In my experience it's pretty rare to overload anything other than == and !=.

完美的未来在梦里 2024-08-10 19:57:52

我认为框架设计指南提供了一些不错的建议:

  • 避免定义运算符重载,除了应该感觉像的类型
    原始(内置)类型。
  • 考虑在应该感觉像原始类型的类型中定义运算符重载。
  • 请在表示数字的结构中定义运算符重载(例如
    系统.十进制)。
  • 定义运算符重载时不要表现得可爱。
  • 不要提供运算符重载,除非至少有一个操作数属于定义重载的类型。
  • DO 以对称方式重载运算符。
  • 考虑提供具有与以下内容对应的友好名称的方法
    每个重载的运算符。

I think the Framework design guidelines provide some decent advice:

  • AVOID defining operator overloads, except in types that should feel like
    primitive (built-in) types.
  • CONSIDER defining operator overloads in a type that should feel like a primitive type.
  • DO define operator overloads in structs that represent numbers (such as
    System.Decimal).
  • DO NOT be cute when defining operator overloads.
  • DO NOT provide operator overloads unless at least one of the operands is of the type defining the overload.
  • DO overload operators in a symmetric fashion.
  • CONSIDER providing methods with friendly names that correspond to
    each overloaded operator.
染墨丶若流云 2024-08-10 19:57:52

我考虑在这种情况下重写运算符:

  • == 和 != 当它是简单值类型时,并实现值比较。在其他类型中,我希望 == 和 != 能够进行参考比较并且也不昂贵。
  • 如果它只是一个数值,则与上面的情况相同的比较运算符(>、>= 等)。
  • 实际上,我从不重载算术运算符,但如果可以增强可用性,我会对相同类型的数值执行此操作。
  • 如果类型可以无损地转换为适当的其他类型,则转换运算符。

I consider overriding operators i this cases:

  • == and != when it is a simple value type, and implement value comparison. In other types, I expect that == and != so reference comparison and are also not expensive.
  • comparison operators (>, >= etc) in the same case as above if it is just a numeric value.
  • I actually never overload aritmetic operators, but would do this for the same kind of numeric values if it would enhance the usability.
  • casting operators if the type can be lossless turned to an appropriate other type.
饭团 2024-08-10 19:57:52

我确实请求关闭它,但也许它可以保持打开状态(我试图改变主意,但显然你不能撤销关闭请求:P)

我个人会回答:从不

我从来不认为实现运算符重载是一个好主意。只有一个例外:如果我正在编写某种集合(这种情况并不经常发生)并且我想实现索引器[]

在我个人看来,我认为重写 == 和其他类似的东西是不合适的。但话又说回来,我并不在复杂的数学和金融领域工作,这些东西可能在这些领域有用。但我不能根据这件事的经验来说话(实际上,我可以在金融界,但我们在那里没有这样做)。

I did request to close this, but perhaps it can remain open (I tried to change my mind but apparently you can't revoke a close request :P)

I will answer personally: never.

I never think it's a good idea to implement operator overloading. There is only one exception: If I'm writing some sort of collection (it doesn't happen very often) and I want to implement the indexers [].

In my personal opinion, I do not think it is ever appropriate to be overriding ==, and other such things. But then again, I do not work in complex fields of maths and finance, where such things may be useful. But I can't speak from experience in that matter (actually, I can in the finance world, and we didn't do it there).

郁金香雨 2024-08-10 19:57:52

我会考虑使用运算符重载对表示逻辑值的类型进行算术运算。例如,有时我希望 Point 具有 +- 运算符的重载。

I would consider using operator overloading for arithmetic operations on types that represent a logical value. For instance, there have been occasions when I wished that Point had an overload for the + and - operators.

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