0

Is it possible in C# to override operators on types I don't have control of? E.g. I tried to override add operator for arrays:

public static double[] operator +(double[] a, double[] b)
{
...
}

but it complains that At least one parameter should be <enclosing type>. In C++ one would just create a function...

Grzenio
  • 35,875
  • 47
  • 158
  • 240

1 Answers1

1

Since double[] is a built in type and you can't add operators to it, you could create a wrapper class to do this:

E.g (from wudzik's link):

public class Foo
{
    double[] x;

    // Legal
    public static double[] operator+(double[] x, Foo y);
Jeb
  • 3,689
  • 5
  • 28
  • 45