Operator Overloading

You have already seen how the + operator can have different meanings in different contexts. For example, when we position the + operator between two values of type int, as in the following:

int distance1, distance2;
...
totalDistance = distance1 + distance2;

the compiler will recognize the int types and perform the familiar addition operation we know from arithmetic. However, if we apply the + operator with two values of type string:

string myText, yourText, fullText;
myString = "The boy was ";
yourString = "walking in the park";
fullText = myString + yourString;

myString and yourString are concatenated and assigned to fullText, which contains the following text:

"The boy was walking in the park"

So in the latter case ...

Get C# Primer Plus now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.