...

/

Implementing Functions with Operators and Local Functions

Implementing Functions with Operators and Local Functions

Learn about implementing functions with operators and local functions.

Let’s explore C# operators, starting with string concatenation using the Concat method and transitioning to intuitive operators like + and * . We then leverage operators to enhance code simplicity and expressiveness through operator overloading and local functions.

Implementing functionality using operators

The System.String class has a static method named Concat that concatenates two string values and returns the result, as shown in the following code:

Press + to interact
string s1 = "Hello ";
string s2 = "World!";
string s3 = string.Concat(s1, s2);
WriteLine(s3); // Hello World!

Calling a method like Concat works, but it might be more natural for a programmer to use the + symbol operator to “add” two string values together, as shown in the following code:

Press + to interact
string s3 = s1 + s2;

A well-known biblical phrase is ‘Go forth and multiply’, meaning to procreate. Let’s write code so that the * (multiply) symbol will allow two Person objects to procreate. We will use the + operator to marry two ...