2

Somebody mentioned that it would make sense to overload + in e.g. Color struct. But how can I do it? I thought it needs to be inside of the type.

2
  • 2
    Just wondering, what happens when you + red and red? Double red? Commented Feb 28, 2011 at 18:44
  • @gaearon: I would assume it would be something along the lines of Color.FromARGB(Math.Min(255, c1.A + c2.A), ...) Commented Feb 28, 2011 at 18:47

3 Answers 3

5

That's correct; there's no way to add operator overloading to an existing type (there are no extension operators).

Sign up to request clarification or add additional context in comments.

Comments

0

I wouldn't pursue this - it would probably be cleaner to create an extension method Add(Color c1, Color c2)

Comments

0

Operators involve 2 operands, and you have the chance of placing the operator overload into eather of the 2 types involved.

In other words, you can write your own custom type, and add operators that makes it interact with Color, but if you want Color and int to interact with new operators, then sorry, no, you can´t do that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.