I am writing a C# class that is exposed to VB.Net. I would like to overload the vb.net ^ operator so that I can write:
Dim c as MyClass
Set c = New ...
Dim d as MyClass
Set d = c^2
In C#, the ^ operator is the xor operator and the power operator doesn’t exist. Is there a way I can do this anyway?
EDIT
It turns out there’s a
SpecialNameAttributethat lets you declare “special” functions in C# that will allow you (among other things) to overload the VB power operator:The
op_Exponentfunction in the class above gets translated by VB into the^power operator.Interestingly, the documentation states the Attribute in not currently used by the .NET framework…
–ORIGINAL ANSWER —
No. The power (
^) operator gets compiled asMath.Pow()so there’s no way to “overload” it in C#.From LinqPad:
IL: