C# exponentiation operator & operator overloading

  • Thread starter Thread starter David Laub
  • Start date Start date
D

David Laub

I know there is no C# exponentiation operator. But since
the double class is sealed, there seems no way to add the
operator override without creating a new class which uses
containment (of a double value) This seems a major pain,
and would probably wind up being more syntactically messy
than just calling Math.Pow(x,y)

Surely greater minds than I have already wrestled with
this problem...

David Laub
 
David,
Just out of curiosity, what operator would you propose to use that would
signify exponentiation?

As all the operators that you are allowed to override already have
predefined meanings. If you attempted to hi-jack one of them, would you not
be introducing semantically messy code? (Am I doing exponentiation here or
am I doing an Xor?)

Remember the "Design Guidelines for Class Library Developers" state "Use
operator overloading in cases where it is immediately obvious what the
result of the operation will be. ... However, it is not appropriate to use
the 'or' operator to create the union of two database queries, or to use
'shift' to write to a stream" Which IMHO avoids semantically messy code.

http://msdn.microsoft.com/library/d...l/cpconOperatorOverloadingUsageGuidelines.asp

Seeing as semantically messy code tends to be harder for the initial
developer and subsequent developers to follow I don't see a big problem with
using Math.Pow. Of course if you are use to VB.NET or other languages that
have a defined exponentiation operator, I can understand your desire on
mimicking the other language, however C# is NOT the other language!

Hope this helps
Jay
 
Back
Top