In many scenarios, operator overloads allow you to express yourself better. Why are they hardly used in C#?
In many scenarios, operator overloads allow you to express yourself better. Why are they
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
They are not used very frequently because often it doesn’t make sense to have mathematical operations (which the majority of operators are) over most classes of objects. The semantics of the operations you want are usually different in some way. You wouldn’t use addition against a bunch of OrderLine objects, for example, when you actually want aggregation or grouping.
To use the example that you have now provided in the comments for your question, “+” may seem like a decent way to put child elements into a parent element but the operations have entirely different semantics. “+” suggests that you would be adding them together mathematically, when in fact you are aiming for a hierarchial relationship between the two. It may make sense to you, but I imagine that on first read it is probably not obvious to a lot of programmers.
They’re rarely used because they rarely fit.