You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Lucas Vos edited this page Jul 16, 2014
·
4 revisions
Operator overloading
in C# operators can be overloaded, which is not supported in Typescript.
To mimic this behaviour, we use a convention based approach, along with some helper methods.
Resulting in this kind of coding
C# sample
var P1 = new Point(1,1);
var P2 = new Point(3,3);
var P = P1 + P2;
In Typescript
var P1 = new Point(1,1);
var P2 = new Point(3,3);
var P = Ops.Add(P1 , P2);
Example
An operator overload in C#
public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
}
An implementation in Typescript
public static op_Addition(c1 : Complex, c2: Complex) : Complex
{
return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
}