using System.Collections.Generic;
static public class Concatenate
public static void Examples()
UsingAddWithConstantStrings();
UsingInterpolationWithVariables();
private static void UsingAddWithConstantStrings()
string text = "Historically, the world of data and the world of objects " +
"have not been well integrated. Programmers work in C# or Visual Basic " +
"and also in SQL or XQuery. On the one side are concepts such as classes, " +
"objects, fields, inheritance, and .NET Framework APIs. On the other side " +
"are tables, columns, rows, nodes, and separate languages for dealing with " +
"them. Data types often require translation between the two worlds; there are " +
"different standard functions. Because the object world has no notion of query, a " +
"query can only be represented as a string without compile-time type checking or " +
"IntelliSense support in the IDE. Transferring data from SQL tables or XML trees to " +
"objects in memory is often tedious and error-prone.";
System.Console.WriteLine(text);
private static void UsingAddWithVariables()
string userName = "<Type your name here>";
string dateString = DateTime.Today.ToShortDateString();
string str = "Hello " + userName + ". Today is " + dateString + ".";
System.Console.WriteLine(str);
str += " How are you today?";
System.Console.WriteLine(str);
private static void UsingInterpolationWithVariables()
string userName = "<Type your name here>";
string date = DateTime.Today.ToShortDateString();
string str = $"Hello {userName}. Today is {date}.";
System.Console.WriteLine(str);
str = $"{str} How are you today?";
System.Console.WriteLine(str);
private static void UsingStringBuilder()
var sb = new System.Text.StringBuilder();
for (int i = 0; i < 20; i++)
sb.AppendLine(i.ToString());
System.Console.WriteLine(sb.ToString());
private static void UsingConcatAndJoin()
string[] words = { "The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog." };
var unreadablePhrase = string.Concat(words);
System.Console.WriteLine(unreadablePhrase);
var readablePhrase = string.Join(" ", words);
System.Console.WriteLine(readablePhrase);
private static void UsingAggregate()
string[] words = { "The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog." };
var phrase = words.Aggregate((partialPhrase, word) =>$"{partialPhrase} {word}");
System.Console.WriteLine(phrase);