O'Reilly logo

C# Primer Plus by Klaus Michelsen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required