Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extend an existing struct in C# to add operators

I want to extend the .NET's built-in Color struct to add new operators like + or -.
I will use them like:

Color c1 = Color.FromName("Red");
Color c2 = Color.FromName("Blue");
Color result = c2 - c1;

Is it possible? if yes, how?

like image 888
Can Poyrazoğlu Avatar asked Aug 17 '11 15:08

Can Poyrazoğlu


2 Answers

There is no way to do it with the built in operators.

You could write an extension method to kind of fake it though:

public static class Extensions
{
    public static Color Substract(this Color color, Color theOtherColor)
    {
        //perform magic here! 
        //be sure to return something or this won't compile
    }
}

Color c1 = Color.FromName("Red");
Color c2 = Color.FromName("Blue");
Color result = c2.Subtract(c1);
like image 191
asawyer Avatar answered Oct 14 '22 16:10

asawyer


As others suggested, you can go either the extension methods way or the Decorator pattern way.

However, consider that Color has a fair number of properties and methods, so redirecting them all from the decorator class to your wrapped Color struct will mean writing a lot of boilerplate. If you go that route, however, you can indeed define operators and even implicit conversions from your class to Color and the other way around (so that you can use them more interchangeably), like this:

public class MyColor {
    public System.Drawing.Color val;

    public MyColor(System.Drawing.Color color)
    {
        this.val = color;
    }

    public static MyColor AliceBlue 
    {
        get {
            return new MyColor(System.Drawing.Color.AliceBlue);
        }
    }

    public override string ToString()
    {
        return val.ToString();
    }
    // .... and so on....

    // User-defined conversion from MyColor to Color
    public static implicit operator System.Drawing.Color(MyColor c)
    {
        return c.val;
    }
    //  User-defined conversion from Color to MyColor
    public static implicit operator MyColor(System.Drawing.Color c)
    {
        return new MyColor(c);
    }
}

to test:

MyColor c = System.Drawing.Color.AliceBlue; // assigning a Color to a MyColor
                                            // thanks to the implicit conversion
Console.WriteLine(c.ToString()); // writes "Color [AliceBlue]"
like image 39
Paolo Falabella Avatar answered Oct 14 '22 18:10

Paolo Falabella