Overloading operators in C #

2

I have some doubts about overloading operators in C #.

  • What is it for?
  • Is your concept the same as overload in Java methods?
  • Is there a practical example of doing this on a daily basis?
  • asked by anonymous 07.11.2018 / 19:56

    1 answer

    4
      

    What's the use?

    To "create" operators on certain types. Not that you can create a new operator, but as the name says it can overload an existing operator appropriately for this particular type.

    It almost always only makes sense in types that express simple and clear mathematical quantities, it is commonly a type by value, but matrix is also common.

    Without this functionality we would have to use methods to do the same. In fact the operator is no longer a method, but at the time of consuming it the syntax is much more pleasant, if created with good sense and good taste.

    Most of the types, not to mention all, makes a lot of sense to have the == operator, without it you would have to use the Equals() or ReferenceEquals() method.

    Do not be crazy about these operators. Some people criticize the language to allow this because a person can make the + operator subtract something. But why would you do this? Only a madman does. And everything in the language can be abused. So it's a good thing. Just do not try to use a symbol for something that has no relation to what it indicates to be, it is not to use at random just to get shorter to type something at the time of use. There are already criticisms if + should be used to concatenate string . I do not see problems, even being a bit different from usual in math makes some sense.

    The operator's precedence or associativity rules does not change anything.

    You have to take some care and know all the consequences well before using this mechanism. Its adoption is not so trivial. The rules for using 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 almost no one creates).

      

    Is your concept the same as overload in Java methods?

    Yes, and it equals the overload of C # as well. The operator is still a method, only has a name and a special rule.

    Because Java does not have this has some weird and long expressions.

      

    Is there any practical example of doing this on a daily basis?

    The most obvious example that everyone uses is Complex (see sources there). Another well used is Decimal . Already Matrix3D is a somewhat more complex one, but that has not been implemented very adequately. Not to mention String as already said (although it is provided by language). Did you notice that these cases there is abuse and it makes sense to have operators?

    See more at How to create operators in C #? .

        
    07.11.2018 / 20:06