Skip to content

Commit 7f945f9

Browse files
authored
Extract remarks for System.Globalization* (#38912)
1 parent fdddc6e commit 7f945f9

Some content is hidden

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

57 files changed

+2900
-6
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
> [!NOTE]
2+
> **.NET Core running on Linux and macOS systems only:** The collation behavior for the C and Posix cultures is always case-sensitive because these cultures do not use the expected Unicode collation order. We recommend that you use a culture other than C or Posix for performing culture-sensitive, case-insensitive sorting operations.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This article provides supplementary remarks to the reference documentation for this API.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// <Snippet1>
2+
using System;
3+
using System.Globalization;
4+
5+
public class Example
6+
{
7+
public static void Main()
8+
{
9+
// Set the default culture and display the current date in the current application domain.
10+
Info info1 = new Info();
11+
SetAppDomainCultures("fr-FR");
12+
13+
// Create a second application domain.
14+
AppDomainSetup setup = new AppDomainSetup();
15+
setup.AppDomainInitializer = SetAppDomainCultures;
16+
setup.AppDomainInitializerArguments = new string[] { "ru-RU" };
17+
AppDomain domain = AppDomain.CreateDomain("Domain2", null, setup);
18+
// Create an Info object in the new application domain.
19+
Info info2 = (Info)domain.CreateInstanceAndUnwrap(typeof(Example).Assembly.FullName,
20+
"Info");
21+
22+
// Execute methods in the two application domains.
23+
info2.DisplayDate();
24+
info2.DisplayCultures();
25+
26+
info1.DisplayDate();
27+
info1.DisplayCultures();
28+
}
29+
30+
public static void SetAppDomainCultures(string[] names)
31+
{
32+
SetAppDomainCultures(names[0]);
33+
}
34+
35+
public static void SetAppDomainCultures(string name)
36+
{
37+
try
38+
{
39+
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.CreateSpecificCulture(name);
40+
CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.CreateSpecificCulture(name);
41+
}
42+
// If an exception occurs, we'll just fall back to the system default.
43+
catch (CultureNotFoundException)
44+
{
45+
return;
46+
}
47+
catch (ArgumentException)
48+
{
49+
return;
50+
}
51+
}
52+
}
53+
54+
public class Info : MarshalByRefObject
55+
{
56+
public void DisplayDate()
57+
{
58+
Console.WriteLine("Today is {0:D}", DateTime.Now);
59+
}
60+
61+
public void DisplayCultures()
62+
{
63+
Console.WriteLine("Application domain is {0}", AppDomain.CurrentDomain.Id);
64+
Console.WriteLine("Default Culture: {0}", CultureInfo.DefaultThreadCurrentCulture);
65+
Console.WriteLine("Default UI Culture: {0}", CultureInfo.DefaultThreadCurrentUICulture);
66+
}
67+
}
68+
// The example displays the following output:
69+
// Today is 14 октября 2011 г.
70+
// Application domain is 2
71+
// Default Culture: ru-RU
72+
// Default UI Culture: ru-RU
73+
// Today is vendredi 14 octobre 2011
74+
// Application domain is 1
75+
// Default Culture: fr-FR
76+
// Default UI Culture: fr-FR
77+
// </Snippet1>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// <Snippet1>
2+
using System;
3+
using System.Globalization;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
7+
public class AsyncCultureEx1
8+
{
9+
public static void Main()
10+
{
11+
decimal[] values = { 163025412.32m, 18905365.59m };
12+
string formatString = "C2";
13+
14+
string FormatDelegate()
15+
{
16+
string output = $"Formatting using the {CultureInfo.CurrentCulture.Name} " +
17+
"culture on thread {Thread.CurrentThread.ManagedThreadId}.\n";
18+
foreach (decimal value in values)
19+
output += $"{value.ToString(formatString)} ";
20+
21+
output += Environment.NewLine;
22+
return output;
23+
}
24+
25+
Console.WriteLine($"The example is running on thread {Thread.CurrentThread.ManagedThreadId}");
26+
// Make the current culture different from the system culture.
27+
Console.WriteLine($"The current culture is {CultureInfo.CurrentCulture.Name}");
28+
if (CultureInfo.CurrentCulture.Name == "fr-FR")
29+
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
30+
else
31+
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
32+
33+
Console.WriteLine($"Changed the current culture to {CultureInfo.CurrentCulture.Name}.\n");
34+
35+
// Execute the delegate synchronously.
36+
Console.WriteLine("Executing the delegate synchronously:");
37+
Console.WriteLine(FormatDelegate());
38+
39+
// Call an async delegate to format the values using one format string.
40+
Console.WriteLine("Executing a task asynchronously:");
41+
var t1 = Task.Run(FormatDelegate);
42+
Console.WriteLine(t1.Result);
43+
44+
Console.WriteLine("Executing a task synchronously:");
45+
var t2 = new Task<string>(FormatDelegate);
46+
t2.RunSynchronously();
47+
Console.WriteLine(t2.Result);
48+
}
49+
}
50+
// The example displays the following output:
51+
// The example is running on thread 1
52+
// The current culture is en-US
53+
// Changed the current culture to fr-FR.
54+
//
55+
// Executing the delegate synchronously:
56+
// Formatting using the fr-FR culture on thread 1.
57+
// 163 025 412,32 € 18 905 365,59 €
58+
//
59+
// Executing a task asynchronously:
60+
// Formatting using the fr-FR culture on thread 3.
61+
// 163 025 412,32 € 18 905 365,59 €
62+
//
63+
// Executing a task synchronously:
64+
// Formatting using the fr-FR culture on thread 1.
65+
// 163 025 412,32 € 18 905 365,59 €
66+
// </Snippet1>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// <Snippet3>
2+
using System;
3+
using System.Globalization;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
7+
public class AsyncCultureEx3
8+
{
9+
public static void Main()
10+
{
11+
decimal[] values = { 163025412.32m, 18905365.59m };
12+
string formatString = "C2";
13+
Func<String> formatDelegate = () =>
14+
{
15+
string output = String.Format("Formatting using the {0} culture on thread {1}.\n",
16+
CultureInfo.CurrentCulture.Name,
17+
Thread.CurrentThread.ManagedThreadId);
18+
foreach (var value in values)
19+
output += String.Format("{0} ", value.ToString(formatString));
20+
21+
output += Environment.NewLine;
22+
return output;
23+
};
24+
25+
Console.WriteLine("The example is running on thread {0}",
26+
Thread.CurrentThread.ManagedThreadId);
27+
// Make the current culture different from the system culture.
28+
Console.WriteLine("The current culture is {0}",
29+
CultureInfo.CurrentCulture.Name);
30+
if (CultureInfo.CurrentCulture.Name == "fr-FR")
31+
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
32+
else
33+
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
34+
35+
Console.WriteLine("Changed the current culture to {0}.\n",
36+
CultureInfo.CurrentCulture.Name);
37+
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.CurrentCulture;
38+
39+
// Execute the delegate synchronously.
40+
Console.WriteLine("Executing the delegate synchronously:");
41+
Console.WriteLine(formatDelegate());
42+
43+
// Call an async delegate to format the values using one format string.
44+
Console.WriteLine("Executing a task asynchronously:");
45+
var t1 = Task.Run(formatDelegate);
46+
Console.WriteLine(t1.Result);
47+
48+
Console.WriteLine("Executing a task synchronously:");
49+
var t2 = new Task<String>(formatDelegate);
50+
t2.RunSynchronously();
51+
Console.WriteLine(t2.Result);
52+
}
53+
}
54+
// The example displays the following output:
55+
// The example is running on thread 1
56+
// The current culture is en-US
57+
// Changed the current culture to fr-FR.
58+
//
59+
// Executing the delegate synchronously:
60+
// Formatting using the fr-FR culture on thread 1.
61+
// 163 025 412,32 € 18 905 365,59 €
62+
//
63+
// Executing a task asynchronously:
64+
// Formatting using the fr-FR culture on thread 3.
65+
// 163 025 412,32 € 18 905 365,59 €
66+
//
67+
// Executing a task synchronously:
68+
// Formatting using the fr-FR culture on thread 1.
69+
// 163 025 412,32 € 18 905 365,59 €
70+
// </Snippet3>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// <Snippet3>
2+
using System;
3+
using System.Globalization;
4+
5+
public class ChangeEx1
6+
{
7+
public static void Main()
8+
{
9+
CultureInfo current = CultureInfo.CurrentCulture;
10+
Console.WriteLine("The current culture is {0}", current.Name);
11+
CultureInfo newCulture;
12+
if (current.Name.Equals("fr-FR"))
13+
newCulture = new CultureInfo("fr-LU");
14+
else
15+
newCulture = new CultureInfo("fr-FR");
16+
17+
CultureInfo.CurrentCulture = newCulture;
18+
Console.WriteLine("The current culture is now {0}",
19+
CultureInfo.CurrentCulture.Name);
20+
}
21+
}
22+
// The example displays output like the following:
23+
// The current culture is en-US
24+
// The current culture is now fr-FR
25+
// </Snippet3>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// <Snippet4>
2+
using System;
3+
using System.Globalization;
4+
5+
public class ChangeUICultureEx
6+
{
7+
public static void Main()
8+
{
9+
CultureInfo current = CultureInfo.CurrentUICulture;
10+
Console.WriteLine("The current UI culture is {0}", current.Name);
11+
CultureInfo newUICulture;
12+
if (current.Name.Equals("sl-SI"))
13+
newUICulture = new CultureInfo("hr-HR");
14+
else
15+
newUICulture = new CultureInfo("sl-SI");
16+
17+
CultureInfo.CurrentUICulture = newUICulture;
18+
Console.WriteLine("The current UI culture is now {0}",
19+
CultureInfo.CurrentUICulture.Name);
20+
}
21+
}
22+
// The example displays output like the following:
23+
// The current UI culture is en-US
24+
// The current UI culture is now sl-SI
25+
// </Snippet4>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// <Snippet1>
2+
using System;
3+
using System.Globalization;
4+
using System.Threading;
5+
6+
public class CurrentCultureEx
7+
{
8+
public static void Main()
9+
{
10+
CultureInfo culture1 = CultureInfo.CurrentCulture;
11+
CultureInfo culture2 = Thread.CurrentThread.CurrentCulture;
12+
Console.WriteLine("The current culture is {0}", culture1.Name);
13+
Console.WriteLine("The two CultureInfo objects are equal: {0}",
14+
culture1 == culture2);
15+
}
16+
}
17+
// The example displays output like the following:
18+
// The current culture is en-US
19+
// The two CultureInfo objects are equal: True
20+
// </Snippet1>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// <Snippet2>
2+
using System;
3+
using System.Globalization;
4+
using System.Threading;
5+
6+
public class CurrentUIEx
7+
{
8+
public static void Main()
9+
{
10+
CultureInfo uiCulture1 = CultureInfo.CurrentUICulture;
11+
CultureInfo uiCulture2 = Thread.CurrentThread.CurrentUICulture;
12+
Console.WriteLine("The current UI culture is {0}", uiCulture1.Name);
13+
Console.WriteLine("The two CultureInfo objects are equal: {0}",
14+
uiCulture1 == uiCulture2);
15+
}
16+
}
17+
// The example displays output like the following:
18+
// The current UI culture is en-US
19+
// The two CultureInfo objects are equal: True
20+
// </Snippet2>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// <Snippet1>
2+
using System;
3+
using System.Globalization;
4+
using System.Threading;
5+
6+
public class DefaultThreadEx
7+
{
8+
static Random rnd = new Random();
9+
10+
public static void Main()
11+
{
12+
if (Thread.CurrentThread.CurrentCulture.Name != "fr-FR")
13+
{
14+
// If current culture is not fr-FR, set culture to fr-FR.
15+
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("fr-FR");
16+
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("fr-FR");
17+
}
18+
else
19+
{
20+
// Set culture to en-US.
21+
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
22+
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("en-US");
23+
}
24+
ThreadProc();
25+
26+
Thread worker = new Thread(ThreadProc);
27+
worker.Name = "WorkerThread";
28+
worker.Start();
29+
}
30+
31+
private static void DisplayThreadInfo()
32+
{
33+
Console.WriteLine("\nCurrent Thread Name: '{0}'",
34+
Thread.CurrentThread.Name);
35+
Console.WriteLine("Current Thread Culture/UI Culture: {0}/{1}",
36+
Thread.CurrentThread.CurrentCulture.Name,
37+
Thread.CurrentThread.CurrentUICulture.Name);
38+
}
39+
40+
private static void DisplayValues()
41+
{
42+
// Create new thread and display three random numbers.
43+
Console.WriteLine("Some currency values:");
44+
for (int ctr = 0; ctr <= 3; ctr++)
45+
Console.WriteLine(" {0:C2}", rnd.NextDouble() * 10);
46+
}
47+
48+
private static void ThreadProc()
49+
{
50+
DisplayThreadInfo();
51+
DisplayValues();
52+
}
53+
}
54+
// The example displays output similar to the following:
55+
// Current Thread Name: ''
56+
// Current Thread Culture/UI Culture: fr-FR/fr-FR
57+
// Some currency values:
58+
// 8,11 €
59+
// 1,48 €
60+
// 8,99 €
61+
// 9,04 €
62+
//
63+
// Current Thread Name: 'WorkerThread'
64+
// Current Thread Culture/UI Culture: en-US/en-US
65+
// Some currency values:
66+
// $6.72
67+
// $6.35
68+
// $2.90
69+
// $7.72
70+
// </Snippet1>

0 commit comments

Comments
 (0)