Operator Overloading

Operator overloading creates operator behavior for user-defined types. The best-known example of operator overloading is in C++. The >> and << operators, which are normally the bitwise shift operators, are overloaded to support input and output streams. The >>operator becomes the extraction operator, while the << operator becomes the insertion operator. You can overload operators for any user-defined types in C# to make those types functionally similar to standard or primitive types. However, when overloading operators you should adhere to the general meaning of the operator. For example, don’t overload an operator + for a user-defined type to perform some type of subtraction or deletion. That would definitely confuse users.

Operator overloading has been referred to as syntactic sugar because it is never required. Member functions are an adequate substitute for any operator overloading. Member functions are explicit, which is helpful and adds clarity to code. For example, instead of overloading an operator +, you can provide an Add method in the user-defined type. Operator overloading is implicit and might lead to inadvertent problems or make the application more difficult to maintain. Despite this, operator overloading is common and is an important concept in C#. Operator overloading is the topic of the next chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.223.160