30
1
using System;
2
using System.Text;
3
4
public class Program
5
{
6
public static void Main()
7
{
8
// Concatenation uses the + operator to combine strings and variables
9
var life = "life";
10
var universe = "universe";
11
var everything = "everything";
12
var answer = 42;
13
Console.WriteLine("The answer to " + life + ", the " + universe + ", and " + everything + " is " + answer);
14
15
// Interpolation uses $ at the beginning and curly brackets {} to insert variables
16
Console.WriteLine($"The answer to {life}, the {universe}, and {everything} is {answer}");
17
18
// String builder saves the final string only on the 'ToString()' method
19
var builder = new StringBuilder();
20
builder.Append("The answer to ");
21
builder.Append(life);
22
builder.Append(", the ");
23
builder.Append(universe);
24
builder.Append(", and ");
25
builder.Append(everything);
26
builder.Append(" is ");
27
builder.Append(answer);
28
Console.WriteLine(builder.ToString());
29
}
30
}
Cached Result
19/04/2019