String Concatenation

Syntactically closely related to arithmetic operations is string concatenation, reusing the + operator notation. The following is an example:

Console.Write("Enter your name: ");string name = Console.ReadLine();string message = "Hello " + name;Console.WriteLine(message);

The way concatenation works internally is by calling into any of the String.Concat static methods, as shown in Figure 5.17.

Image

FIGURE 5.17 String concatenation under the hood.

String concatenation is available not only between two string types but also between a string and any object (System.Object). In that case, ToString is invoked on the object before carrying ...

Get C# 5.0 Unleashed 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.