Skip to content

Commit fc6656d

Browse files
authored
Reinstate supplemental remarks (System.Text, System.Threading) (#12706)
1 parent 587b29e commit fc6656d

65 files changed

Lines changed: 3594 additions & 25 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

snippets/csharp/System.Text.RegularExpressions/Regex/Match/Project.csproj renamed to snippets/csharp/System.Text/Encoding/Overview/Project.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Library</OutputType>
5-
<TargetFramework>net6.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
66
</PropertyGroup>
7-
8-
</Project>
7+
8+
</Project>

snippets/csharp/System.Text/Encoding/Overview/convert.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
using System;
33
using System.Text;
44

5-
class Example
5+
class ConvertExample
66
{
7-
static void Main()
7+
static void Run()
88
{
99
string unicodeString = "This string contains the unicode character Pi (\u03a0)";
1010

@@ -28,6 +28,7 @@ static void Main()
2828
Console.WriteLine("Ascii converted string: {0}", asciiString);
2929
}
3030
}
31+
3132
// The example displays the following output:
3233
// Original string: This string contains the unicode character Pi (Π)
3334
// Ascii converted string: This string contains the unicode character Pi (?)
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// <Snippet1>
2+
using System;
3+
using System.Text;
4+
5+
public class GetEncodingExample
6+
{
7+
public static void Run()
8+
{
9+
Encoding enc = Encoding.GetEncoding(1253);
10+
Encoding altEnc = Encoding.GetEncoding("windows-1253");
11+
Console.WriteLine($"{enc.EncodingName} = Code Page {altEnc.CodePage}: {enc.Equals(altEnc)}");
12+
string greekAlphabet = "Α α Β β Γ γ Δ δ Ε ε Ζ ζ Η η " +
13+
"Θ θ Ι ι Κ κ Λ λ Μ μ Ν ν Ξ ξ " +
14+
"Ο ο Π π Ρ ρ Σ σ ς Τ τ Υ υ " +
15+
"Φ φ Χ χ Ψ ψ Ω ω";
16+
Console.OutputEncoding = Encoding.UTF8;
17+
byte[] bytes = enc.GetBytes(greekAlphabet);
18+
Console.WriteLine("{0,-12} {1,20} {2,20:X2}", "Character",
19+
"Unicode Code Point", "Code Page 1253");
20+
for (int ctr = 0; ctr < bytes.Length; ctr++) {
21+
if (greekAlphabet[ctr].Equals(' '))
22+
continue;
23+
24+
Console.WriteLine("{0,-12} {1,20} {2,20:X2}", greekAlphabet[ctr],
25+
GetCodePoint(greekAlphabet[ctr]), bytes[ctr]);
26+
}
27+
}
28+
29+
private static string GetCodePoint(char ch)
30+
{
31+
string retVal = "u+";
32+
byte[] bytes = Encoding.Unicode.GetBytes(ch.ToString());
33+
for (int ctr = bytes.Length - 1; ctr >= 0; ctr--)
34+
retVal += bytes[ctr].ToString("X2");
35+
36+
return retVal;
37+
}
38+
}
39+
40+
// The example displays the following output:
41+
// Character Unicode Code Point Code Page 1253
42+
// Α u+0391 C1
43+
// α u+03B1 E1
44+
// Β u+0392 C2
45+
// β u+03B2 E2
46+
// Γ u+0393 C3
47+
// γ u+03B3 E3
48+
// Δ u+0394 C4
49+
// δ u+03B4 E4
50+
// Ε u+0395 C5
51+
// ε u+03B5 E5
52+
// Ζ u+0396 C6
53+
// ζ u+03B6 E6
54+
// Η u+0397 C7
55+
// η u+03B7 E7
56+
// Θ u+0398 C8
57+
// θ u+03B8 E8
58+
// Ι u+0399 C9
59+
// ι u+03B9 E9
60+
// Κ u+039A CA
61+
// κ u+03BA EA
62+
// Λ u+039B CB
63+
// λ u+03BB EB
64+
// Μ u+039C CC
65+
// μ u+03BC EC
66+
// Ν u+039D CD
67+
// ν u+03BD ED
68+
// Ξ u+039E CE
69+
// ξ u+03BE EE
70+
// Ο u+039F CF
71+
// ο u+03BF EF
72+
// Π u+03A0 D0
73+
// π u+03C0 F0
74+
// Ρ u+03A1 D1
75+
// ρ u+03C1 F1
76+
// Σ u+03A3 D3
77+
// σ u+03C3 F3
78+
// ς u+03C2 F2
79+
// Τ u+03A4 D4
80+
// τ u+03C4 F4
81+
// Υ u+03A5 D5
82+
// υ u+03C5 F5
83+
// Φ u+03A6 D6
84+
// φ u+03C6 F6
85+
// Χ u+03A7 D7
86+
// χ u+03C7 F7
87+
// Ψ u+03A8 D8
88+
// ψ u+03C8 F8
89+
// Ω u+03A9 D9
90+
// ω u+03C9 F9
91+
// </Snippet1>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Library</OutputType>
5+
<TargetFramework>net10.0</TargetFramework>
6+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
7+
</PropertyGroup>
8+
9+
</Project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// <Snippet4>
2+
using System;
3+
using System.Text;
4+
5+
public class Example
6+
{
7+
public static void Main()
8+
{
9+
StringBuilder sb = new StringBuilder();
10+
sb.Append("This is the beginning of a sentence, ");
11+
sb.Replace("the beginning of ", "");
12+
sb.Insert(sb.ToString().IndexOf("a ") + 2, "complete ");
13+
sb.Replace(",", ".");
14+
Console.WriteLine(sb.ToString());
15+
}
16+
}
17+
// The example displays the following output:
18+
// This is a complete sentence.
19+
// </Snippet4>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// <Snippet5>
2+
using System;
3+
using System.Text;
4+
5+
public class Example2
6+
{
7+
public static void Main()
8+
{
9+
StringBuilder sb = new StringBuilder("This is the beginning of a sentence, ");
10+
sb.Replace("the beginning of ", "").Insert(sb.ToString().IndexOf("a ") + 2,
11+
"complete ").Replace(",", ".");
12+
Console.WriteLine(sb.ToString());
13+
}
14+
}
15+
// The example displays the following output:
16+
// This is a complete sentence.
17+
// </Snippet5>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// <Snippet7>
2+
using System;
3+
using System.Globalization;
4+
using System.Text;
5+
6+
public class Example3
7+
{
8+
public static void Main()
9+
{
10+
Random rnd = new Random();
11+
StringBuilder sb = new StringBuilder();
12+
13+
// Generate 10 random numbers and store them in a StringBuilder.
14+
for (int ctr = 0; ctr <= 9; ctr++)
15+
sb.Append(rnd.Next().ToString("N5"));
16+
17+
Console.WriteLine("The original string:");
18+
Console.WriteLine(sb.ToString());
19+
20+
// Decrease each number by one.
21+
for (int ctr = 0; ctr < sb.Length; ctr++)
22+
{
23+
if (Char.GetUnicodeCategory(sb[ctr]) == UnicodeCategory.DecimalDigitNumber)
24+
{
25+
int number = (int)Char.GetNumericValue(sb[ctr]);
26+
number--;
27+
if (number < 0) number = 9;
28+
29+
sb[ctr] = number.ToString()[0];
30+
}
31+
}
32+
Console.WriteLine("\nThe new string:");
33+
Console.WriteLine(sb.ToString());
34+
}
35+
}
36+
// The example displays the following output:
37+
// The original string:
38+
// 1,457,531,530.00000940,522,609.000001,668,113,564.000001,998,992,883.000001,792,660,834.00
39+
// 000101,203,251.000002,051,183,075.000002,066,000,067.000001,643,701,043.000001,702,382,508
40+
// .00000
41+
//
42+
// The new string:
43+
// 0,346,420,429.99999839,411,598.999990,557,002,453.999990,887,881,772.999990,681,559,723.99
44+
// 999090,192,140.999991,940,072,964.999991,955,999,956.999990,532,690,932.999990,691,271,497
45+
// .99999
46+
// </Snippet7>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// <Snippet3>
2+
using System;
3+
using System.Reflection;
4+
using System.Text;
5+
6+
public class Example4
7+
{
8+
public static void Main()
9+
{
10+
StringBuilder sb = new StringBuilder();
11+
ShowSBInfo(sb);
12+
sb.Append("This is a sentence.");
13+
ShowSBInfo(sb);
14+
for (int ctr = 0; ctr <= 10; ctr++)
15+
{
16+
sb.Append("This is an additional sentence.");
17+
ShowSBInfo(sb);
18+
}
19+
}
20+
21+
private static void ShowSBInfo(StringBuilder sb)
22+
{
23+
foreach (var prop in sb.GetType().GetProperties())
24+
{
25+
if (prop.GetIndexParameters().Length == 0)
26+
Console.Write("{0}: {1:N0} ", prop.Name, prop.GetValue(sb));
27+
}
28+
Console.WriteLine();
29+
}
30+
}
31+
// The example displays the following output:
32+
// Capacity: 16 MaxCapacity: 2,147,483,647 Length: 0
33+
// Capacity: 32 MaxCapacity: 2,147,483,647 Length: 19
34+
// Capacity: 64 MaxCapacity: 2,147,483,647 Length: 50
35+
// Capacity: 128 MaxCapacity: 2,147,483,647 Length: 81
36+
// Capacity: 128 MaxCapacity: 2,147,483,647 Length: 112
37+
// Capacity: 256 MaxCapacity: 2,147,483,647 Length: 143
38+
// Capacity: 256 MaxCapacity: 2,147,483,647 Length: 174
39+
// Capacity: 256 MaxCapacity: 2,147,483,647 Length: 205
40+
// Capacity: 256 MaxCapacity: 2,147,483,647 Length: 236
41+
// Capacity: 512 MaxCapacity: 2,147,483,647 Length: 267
42+
// Capacity: 512 MaxCapacity: 2,147,483,647 Length: 298
43+
// Capacity: 512 MaxCapacity: 2,147,483,647 Length: 329
44+
// Capacity: 512 MaxCapacity: 2,147,483,647 Length: 360
45+
// </Snippet3>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// <Snippet10>
2+
using System;
3+
using System.Text;
4+
5+
public class Example5
6+
{
7+
public static void Main()
8+
{
9+
StringBuilder sb = new StringBuilder("A StringBuilder object");
10+
ShowSBInfo(sb);
11+
// Remove "object" from the text.
12+
string textToRemove = "object";
13+
int pos = sb.ToString().IndexOf(textToRemove);
14+
if (pos >= 0)
15+
{
16+
sb.Remove(pos, textToRemove.Length);
17+
ShowSBInfo(sb);
18+
}
19+
// Clear the StringBuilder contents.
20+
sb.Clear();
21+
ShowSBInfo(sb);
22+
}
23+
24+
public static void ShowSBInfo(StringBuilder sb)
25+
{
26+
Console.WriteLine($"\nValue: {sb.ToString()}");
27+
foreach (var prop in sb.GetType().GetProperties())
28+
{
29+
if (prop.GetIndexParameters().Length == 0)
30+
Console.Write("{0}: {1:N0} ", prop.Name, prop.GetValue(sb));
31+
}
32+
Console.WriteLine();
33+
}
34+
}
35+
// The example displays the following output:
36+
// Value: A StringBuilder object
37+
// Capacity: 22 MaxCapacity: 2,147,483,647 Length: 22
38+
//
39+
// Value: A StringBuilder
40+
// Capacity: 22 MaxCapacity: 2,147,483,647 Length: 16
41+
//
42+
// Value:
43+
// Capacity: 22 MaxCapacity: 2,147,483,647 Length: 0
44+
// </Snippet10>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// <Snippet9>
2+
using System;
3+
using System.Text;
4+
5+
public class Example6
6+
{
7+
public static void Main()
8+
{
9+
// Create a StringBuilder object with no text.
10+
StringBuilder sb = new StringBuilder();
11+
// Append some text.
12+
sb.Append('*', 10).Append(" Adding Text to a StringBuilder Object ").Append('*', 10);
13+
sb.AppendLine("\n");
14+
sb.AppendLine("Some code points and their corresponding characters:");
15+
// Append some formatted text.
16+
for (int ctr = 50; ctr <= 60; ctr++)
17+
{
18+
sb.AppendFormat("{0,12:X4} {1,12}", ctr, Convert.ToChar(ctr));
19+
sb.AppendLine();
20+
}
21+
// Find the end of the introduction to the column.
22+
int pos = sb.ToString().IndexOf("characters:") + 11 +
23+
Environment.NewLine.Length;
24+
// Insert a column header.
25+
sb.Insert(pos, $"{Environment.NewLine}{"Code Unit",12} {"Character",12}{Environment.NewLine}");
26+
27+
// Convert the StringBuilder to a string and display it.
28+
Console.WriteLine(sb.ToString());
29+
}
30+
}
31+
// The example displays the following output:
32+
// ********** Adding Text to a StringBuilder Object **********
33+
//
34+
// Some code points and their corresponding characters:
35+
//
36+
// Code Unit Character
37+
// 0032 2
38+
// 0033 3
39+
// 0034 4
40+
// 0035 5
41+
// 0036 6
42+
// 0037 7
43+
// 0038 8
44+
// 0039 9
45+
// 003A :
46+
// 003B ;
47+
// 003C <
48+
// </Snippet9>

0 commit comments

Comments
 (0)