2
I have some questions about overloading operators in C#.
- What good is?
- His concept is the same as Overload in Java methods?
- Is there any practical example of doing such a procedure on a day-to-day basis?
2
I have some questions about overloading operators in C#.
4
What good is?
To "create" operators in certain types. Not that it can create a new operator, but as the name says it can overload an existing operator appropriately for that particular type.
It almost always only makes sense in types that express simple and clear mathematical magnitudes, it is usually one type per value, but in matrix it is also common.
Without this functionality we would have to use methods to do the same. In fact the operator is not a method, but when consuming it the syntax is much more pleasant, if created with common sense and good taste.
Most of the guys, not to mention all of them, it makes a lot of sense to have the operator ==
, without it would have to use the method Equals()
or ReferenceEquals()
.
One should not do crazy things with these operators. Some people criticize the language allow this because one person can do the operator +
subtract something. But why would you do that? Only a madman does. But everything in language can be abused, so it’s a good thing.
Just don’t try to use a symbol for something that has no relation to what it indicates to be, it’s not to use it just to be shorter to type something at the time of use. There are already criticisms if the +
should be used to concatenate string
. I don’t see any problems, even being a little different from the usual math makes some sense.
The rules of precedence or association of the operator do not change anything.
You have to take some care and know all the consequences before using this mechanism. Its adoption is not so trivial. The rules of use of C# are different from other languages, such as C++, for example. For example they can only be static methods, so the compiler can more easily change the order of use.
In normal classes it is rare to have its use except the indexing operator, and eventually cast. Even in structs
s most cases do not need, except maybe, cast (and hardly anyone creates).
Its concept is the same as Overload in Java methods?
Yeah, and it’s just like Overload of C# as well. The operator is still a method, only has a name and special rule.
Because Java doesn’t have it has some weird and long expressions.
Is there any practical example of doing such a procedure on a day-to-day basis?
The most obvious example everyone uses is Complex
(see the sources there). Another well used is the Decimal
. Already Matrix3D
is a bit more complex, but it has not been implemented very properly. Not to mention String
as already said (although it is provided by language). Noted that these cases there is no abuse and it makes sense to have operators?
See more in How to create C operators#?.
Browser other questions tagged c# characteristic-language operators overload
You are not signed in. Login or sign up in order to post.
Possible duplicate of What is operator overload?
– Netinho Santos