-
Notifications
You must be signed in to change notification settings - Fork 2
/
Program.cs
108 lines (87 loc) · 1.83 KB
/
Program.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System.Diagnostics.CodeAnalysis;
using System.Text;
// UTF8 string literal
ReadOnlySpan<byte> mystring = "Bücher"u8;
byte[] array = "my string"u8.ToArray();
Console.WriteLine(Encoding.UTF8.GetString(mystring));
// raw string literals
string s1 = """
<books>
<book isbn="3443874">
<title>Professional C#</title>
</book>
</books>
""";
Console.WriteLine(s1);
// keep indentation
string s2 = """
<books>
<book isbn="3443874">
<title>Professional C#</title>
</book>
</books>
""";
Console.WriteLine(s2);
Console.WriteLine();
// string interpolation
string isbn = "344343";
string title = "Professional C# and .NET";
string s3 = $"""
<books>
<book isbn="{isbn}">
<title>{title}</title>
</book>
</books>
""";
Console.WriteLine(s3);
Console.WriteLine();
// replace { with {{ using $$
string val = "waboom";
string code1 = $$"""
public void Main()
{
string hello = "{{val}}";
Console.WriteLine($"This is so cool {hello}");
}
""";
ProcessCSharp("""
void Foo()
{
Console.WriteLine(""Hello World"");
}
""");
Console.WriteLine(code1);
Console.WriteLine();
// generate JSON
string json1 = $$"""
{
"book": {
"title": "{{title}}",
"publisher": "Wiley"
}
};
""";
ProcessJson(json1);
ProcessJson($$"""
{
"book": {
"title": "{{title}}",
"publisher": "Wiley"
}
};
""");
void ProcessXml([StringSyntax(StringSyntaxAttribute.Xml)] string xml)
{
Console.WriteLine(xml);
Console.WriteLine();
}
void ProcessJson([StringSyntax(StringSyntaxAttribute.Json)] string json)
{
Console.WriteLine(json);
Console.WriteLine();
}
void ProcessCSharp([StringSyntax("csharp")] string csharp)
{
Console.WriteLine(csharp);
Console.WriteLine();
}