5

Possible Duplicate:
Why must C# operator overloads be static?

Why Static keyword before the function signature of all the overloaded operators in C# like:

public static void operator = (Object a, Object b)

When we are doing a = b; then a value will be implicitly passed right. So there is no need of static keyword. It must be like:

public void operator = (Object b)

Is it?

Community
  • 1
  • 1
Sunil
  • 1,941
  • 3
  • 19
  • 25
  • This post might shed more light on the matter: http://stackoverflow.com/questions/2018108/why-must-c-operator-overloads-be-static/ – Igor Zevaka Feb 23 '10 at 11:06

2 Answers2

9

The fact that operators are static allows them to be used in situations where there are null values. It also emphasizes the fact that operators are not applied polymorphically. (They potentially could be applied polymorphically if they weren't static, admittedly... but overriding would generally be a bad idea anyway IMO.)

(Note that you can't overload the assignment operator in C# anyway.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Note that you can't overload the assignment operator in C# anyway Why? – Sunil Feb 23 '10 at 11:05
  • @Sunil: Because it's nasty in terms of readability and has evil corner cases, IIRC. You can provide an implicit conversion though, which will cope with most of the cases where you'd want to overload the assignment operator. – Jon Skeet Feb 23 '10 at 13:19
0

Otherwise you would Always need an instance of the Object to perform that. Which you might not have on case a or b is null.

Filip Ekberg
  • 36,033
  • 20
  • 126
  • 183