Is it possible define an extension operator method?

前端 未结 10 734
迷失自我
迷失自我 2021-01-04 02:09

is it possible to define an extension method that at the same time is an operator? I want for a fixed class add the possibility to use a known operator that actually can\'t

10条回答
  •  迷失自我
    2021-01-04 02:41

    This is all true, but it would be nice for M$ to add this functionality in the future. Sometimes the framework is just missing things and an extension can help plug the gap (or fix the issue) this can sometimes be operators.

    An example. To compare IP Addresses, you must use the Equals method to directly compare (of course parts of the struct could also be compared as could the address bytes individually - but that's another story). However, using the == operator always returns false at the object level (i.e. without converting them to strings etc). How hard is it to put the Equals method call inside the == operator call (that's rhetorical), but we can't do it. This is inconsistant and a place for bugs to creep in (note it does not fail, just always equates to false - whereas Equals does not).

提交回复
热议问题