12.2. Understanding Operator Overloading
C#, like any programming language, has a canned set of tokens that are used to perform basic operations on intrinsic types. For example, you know that the + operator can be applied to two integers in order to yield a larger integer:
// The + operator with ints. int a = 100;
int b = 240; int c = a + b; // c is now 340
Once again, this is no major newsflash, but have you ever stopped and noticed how the same + operator can be applied to most intrinsic C# data types? For example, consider this code:
// + operator with strings. string s1 = "Hello"; string s2 = " world!"; string s3 = s1 + s2; // s3 is now "Hello world!"
In essence, the + operator functions in specific ways based on the supplied data types ...
Get Pro C# 2010 and the .NET 4 Platform, Fifth Edition 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.