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

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