-
Notifications
You must be signed in to change notification settings - Fork 0
/
InterpolatedStringsAdvanced.cs
77 lines (63 loc) · 3.18 KB
/
InterpolatedStringsAdvanced.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Common scenarios of string interpolation usage.
double a = 3;
double b = 4;
Console.WriteLine($"Area of the right triangle with legs of {a} and {b} is {0.5 * a * b}");
Console.WriteLine($"Length of the hypotenuse of the right triangle with legs of {a} and {b} is {CalculateHypotenuse(a, b)}");
double CalculateHypotenuse(double leg1, double leg2) => Math.Sqrt(leg1 * leg1 + leg2 * leg2);
// Expected output:
// Area of the right triangle with legs of 3 and 4 is 6
// Length of the hypotenuse of the right triangle with legs of 3 and 4 is 5
// Specify a format string that is supported by the type of the expression result
// By following the interpolation expression with a colon (":") and the format string
// {<interpolationExpression>:<formatString>}
var date = new DateTime(1731, 11, 25);
Console.WriteLine($"On {date:dddd, MMMM dd, yyyy} Han Solo introduced the letter e to denote {Math.E:F5} in a letter to Princess Leia.");
// Expected output:
// On Sunday, November 25, 1731 Han Solo introduced the letter e to denote 2.71828 in a letter to Princess Leia.
// Escape sequences
var xs = new int[] { 1, 2, 7, 9 };
var ys = new int[] { 7, 9, 12 };
Console.WriteLine($"Find the intersection of the {{{string.Join(", ",xs)}}} and {{{string.Join(", ",ys)}}} sets.");
var userName = "Jane";
var stringWithEscapes = $"C:\\Users\\{userName}\\Documents";
var verbatimInterpolated = $@"C:\Users\{userName}\Documents";
Console.WriteLine(stringWithEscapes);
Console.WriteLine(verbatimInterpolated);
// Expected output:
// Find the intersection of the {1, 2, 7, 9} and {7, 9, 12} sets.
// C:\Users\Jane\Documents
// C:\Users\Jane\Documents
// How to use a ternary conditional operator ?: in an interpolation expression.
// As colon (":") has special meaning in an item with an interpolation expression,
// In order to use a conditional operator in an expression, enclose it in parentheses.
var rand = new Random();
for (int i = 0; i < 7; i++)
{
Console.WriteLine($"Coin flip: {(rand.NextDouble() < 0.5 ? "heads" : "tails")}");
}
// How to create a culture-specific result string with string interpolation.
var cultures = new System.Globalization.CultureInfo[]
{
System.Globalization.CultureInfo.GetCultureInfo("en-US"),
System.Globalization.CultureInfo.GetCultureInfo("en-GB"),
System.Globalization.CultureInfo.GetCultureInfo("nl-NL"),
System.Globalization.CultureInfo.InvariantCulture
};
var date = DateTime.Now;
var number = 31_415_926.536;
FormattableString message = $"{date,20}{number,20:N3}";
foreach (var culture in cultures)
{
var cultureSpecificMessage = message.ToString(culture);
Console.WriteLine($"{culture.Name,-10}{cultureSpecificMessage}");
}
// Expected output is like:
// en-US 5/17/18 3:44:55 PM 31,415,926.536
// en-GB 17/05/2018 15:44:55 31,415,926.536
// nl-NL 17-05-18 15:44:55 31.415.926,536
// 05/17/2018 15:44:55 31,415,926.536
// How to create a result string using the invariant culture.
string messageInInvariantCulture = FormattableString.Invariant($"Date and time in invariant culture: {DateTime.Now}");
Console.WriteLine(messageInInvariantCulture);
// Expected output is like:
// Date and time in invariant culture: 05/17/2018 15:46:24