forked from dotnet/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring-interpolation.cs
62 lines (56 loc) · 2.04 KB
/
string-interpolation.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
using System;
using System.Globalization;
namespace StringInterpolationTutorial
{
public class Examples
{
public void StringFormatExample()
{
// <StringFormatExample>
var firstName = "Matt";
var lastName = "Groves";
var str = String.Format("My name is {0} {1}", firstName, lastName);
Console.WriteLine(str);
// </StringFormatExample>
}
public void InterpolationExample()
{
// <InterpolationExample>
var firstName = "Matt";
var lastName = "Groves";
var str = $"My name is {firstName} {lastName}";
Console.WriteLine(str);
// </InterpolationExample>
}
public void InterpolationExpressionExample()
{
// <InterpolationExpressionExample>
for(var i = 0; i < 5; i++) {
Console.WriteLine($"This is line number {i + 1}");
}
// </InterpolationExpressionExample>
}
public void InterpolationFormattingExample()
{
// <InterpolationFormattingExample>
var rand = new Random();
for(var i = 998; i < 1005; i++)
{
var randomDecimal = rand.NextDouble() * 10000;
Console.WriteLine($"{i, -10} {randomDecimal, 6:N2}");
}
// </InterpolationFormattingExample>
}
public void InterpolationInternationalizationExample()
{
// <InterpolationInternationalizationExample>
var birthday = new DateTime(1980, 1, 29);
Console.WriteLine($"My birthday is {birthday}");
// This outputs "My birthday is 1/29/1980 12:00:00 AM"
var birthdayFormattable = (IFormattable)$"My birthday is {birthday}";
Console.WriteLine(birthdayFormattable.ToString(null, new CultureInfo("fr-FR")));
// This outputs "My birthday is 29/01/1980 00:00:00"
// </InterpolationInternationalizationExample>
}
}
}