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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.