Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract remarks for System.Globalization* #38912

Merged
merged 8 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
> [!NOTE]
> **.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.
1 change: 1 addition & 0 deletions docs/fundamentals/runtime-libraries/includes/context.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This article provides supplementary remarks to the reference documentation for this API.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// <Snippet1>
using System;
using System.Globalization;

public class Example
{
public static void Main()
{
// Set the default culture and display the current date in the current application domain.
Info info1 = new Info();
SetAppDomainCultures("fr-FR");

// Create a second application domain.
AppDomainSetup setup = new AppDomainSetup();
setup.AppDomainInitializer = SetAppDomainCultures;
setup.AppDomainInitializerArguments = new string[] { "ru-RU" };
AppDomain domain = AppDomain.CreateDomain("Domain2", null, setup);
// Create an Info object in the new application domain.
Info info2 = (Info)domain.CreateInstanceAndUnwrap(typeof(Example).Assembly.FullName,
"Info");

// Execute methods in the two application domains.
info2.DisplayDate();
info2.DisplayCultures();

info1.DisplayDate();
info1.DisplayCultures();
}

public static void SetAppDomainCultures(string[] names)
{
SetAppDomainCultures(names[0]);
}

public static void SetAppDomainCultures(string name)
{
try
{
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.CreateSpecificCulture(name);
CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.CreateSpecificCulture(name);
}
// If an exception occurs, we'll just fall back to the system default.
catch (CultureNotFoundException)
{
return;
}
catch (ArgumentException)
{
return;
}
}
}

public class Info : MarshalByRefObject
{
public void DisplayDate()
{
Console.WriteLine("Today is {0:D}", DateTime.Now);
}

public void DisplayCultures()
{
Console.WriteLine("Application domain is {0}", AppDomain.CurrentDomain.Id);
Console.WriteLine("Default Culture: {0}", CultureInfo.DefaultThreadCurrentCulture);
Console.WriteLine("Default UI Culture: {0}", CultureInfo.DefaultThreadCurrentUICulture);
}
}
// The example displays the following output:
// Today is 14 октября 2011 г.
// Application domain is 2
// Default Culture: ru-RU
// Default UI Culture: ru-RU
// Today is vendredi 14 octobre 2011
// Application domain is 1
// Default Culture: fr-FR
// Default UI Culture: fr-FR
// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// <Snippet1>
using System;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;

public class AsyncCultureEx1
{
public static void Main()
{
decimal[] values = { 163025412.32m, 18905365.59m };
string formatString = "C2";

string FormatDelegate()
{
string output = $"Formatting using the {CultureInfo.CurrentCulture.Name} " +
"culture on thread {Thread.CurrentThread.ManagedThreadId}.\n";
foreach (decimal value in values)
output += $"{value.ToString(formatString)} ";

output += Environment.NewLine;
return output;
}

Console.WriteLine($"The example is running on thread {Thread.CurrentThread.ManagedThreadId}");
// Make the current culture different from the system culture.
Console.WriteLine($"The current culture is {CultureInfo.CurrentCulture.Name}");
if (CultureInfo.CurrentCulture.Name == "fr-FR")
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
else
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");

Console.WriteLine($"Changed the current culture to {CultureInfo.CurrentCulture.Name}.\n");

// Execute the delegate synchronously.
Console.WriteLine("Executing the delegate synchronously:");
Console.WriteLine(FormatDelegate());

// Call an async delegate to format the values using one format string.
Console.WriteLine("Executing a task asynchronously:");
var t1 = Task.Run(FormatDelegate);
Console.WriteLine(t1.Result);

Console.WriteLine("Executing a task synchronously:");
var t2 = new Task<string>(FormatDelegate);
t2.RunSynchronously();
Console.WriteLine(t2.Result);
}
}
// The example displays the following output:
// The example is running on thread 1
// The current culture is en-US
// Changed the current culture to fr-FR.
//
// Executing the delegate synchronously:
// Formatting using the fr-FR culture on thread 1.
// 163 025 412,32 € 18 905 365,59 €
//
// Executing a task asynchronously:
// Formatting using the fr-FR culture on thread 3.
// 163 025 412,32 € 18 905 365,59 €
//
// Executing a task synchronously:
// Formatting using the fr-FR culture on thread 1.
// 163 025 412,32 € 18 905 365,59 €
// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// <Snippet3>
using System;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;

public class AsyncCultureEx3
{
public static void Main()
{
decimal[] values = { 163025412.32m, 18905365.59m };
string formatString = "C2";
Func<String> formatDelegate = () =>
{
string output = String.Format("Formatting using the {0} culture on thread {1}.\n",
CultureInfo.CurrentCulture.Name,
Thread.CurrentThread.ManagedThreadId);
foreach (var value in values)
output += String.Format("{0} ", value.ToString(formatString));

output += Environment.NewLine;
return output;
};

Console.WriteLine("The example is running on thread {0}",
Thread.CurrentThread.ManagedThreadId);
// Make the current culture different from the system culture.
Console.WriteLine("The current culture is {0}",
CultureInfo.CurrentCulture.Name);
if (CultureInfo.CurrentCulture.Name == "fr-FR")
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
else
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");

Console.WriteLine("Changed the current culture to {0}.\n",
CultureInfo.CurrentCulture.Name);
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.CurrentCulture;

// Execute the delegate synchronously.
Console.WriteLine("Executing the delegate synchronously:");
Console.WriteLine(formatDelegate());

// Call an async delegate to format the values using one format string.
Console.WriteLine("Executing a task asynchronously:");
var t1 = Task.Run(formatDelegate);
Console.WriteLine(t1.Result);

Console.WriteLine("Executing a task synchronously:");
var t2 = new Task<String>(formatDelegate);
t2.RunSynchronously();
Console.WriteLine(t2.Result);
}
}
// The example displays the following output:
// The example is running on thread 1
// The current culture is en-US
// Changed the current culture to fr-FR.
//
// Executing the delegate synchronously:
// Formatting using the fr-FR culture on thread 1.
// 163 025 412,32 € 18 905 365,59 €
//
// Executing a task asynchronously:
// Formatting using the fr-FR culture on thread 3.
// 163 025 412,32 € 18 905 365,59 €
//
// Executing a task synchronously:
// Formatting using the fr-FR culture on thread 1.
// 163 025 412,32 € 18 905 365,59 €
// </Snippet3>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// <Snippet3>
using System;
using System.Globalization;

public class ChangeEx1
{
public static void Main()
{
CultureInfo current = CultureInfo.CurrentCulture;
Console.WriteLine("The current culture is {0}", current.Name);
CultureInfo newCulture;
if (current.Name.Equals("fr-FR"))
newCulture = new CultureInfo("fr-LU");
else
newCulture = new CultureInfo("fr-FR");

CultureInfo.CurrentCulture = newCulture;
Console.WriteLine("The current culture is now {0}",
CultureInfo.CurrentCulture.Name);
}
}
// The example displays output like the following:
// The current culture is en-US
// The current culture is now fr-FR
// </Snippet3>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// <Snippet4>
using System;
using System.Globalization;

public class ChangeUICultureEx
{
public static void Main()
{
CultureInfo current = CultureInfo.CurrentUICulture;
Console.WriteLine("The current UI culture is {0}", current.Name);
CultureInfo newUICulture;
if (current.Name.Equals("sl-SI"))
newUICulture = new CultureInfo("hr-HR");
else
newUICulture = new CultureInfo("sl-SI");

CultureInfo.CurrentUICulture = newUICulture;
Console.WriteLine("The current UI culture is now {0}",
CultureInfo.CurrentUICulture.Name);
}
}
// The example displays output like the following:
// The current UI culture is en-US
// The current UI culture is now sl-SI
// </Snippet4>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// <Snippet1>
using System;
using System.Globalization;
using System.Threading;

public class CurrentCultureEx
{
public static void Main()
{
CultureInfo culture1 = CultureInfo.CurrentCulture;
CultureInfo culture2 = Thread.CurrentThread.CurrentCulture;
Console.WriteLine("The current culture is {0}", culture1.Name);
Console.WriteLine("The two CultureInfo objects are equal: {0}",
culture1 == culture2);
}
}
// The example displays output like the following:
// The current culture is en-US
// The two CultureInfo objects are equal: True
// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// <Snippet2>
using System;
using System.Globalization;
using System.Threading;

public class CurrentUIEx
{
public static void Main()
{
CultureInfo uiCulture1 = CultureInfo.CurrentUICulture;
CultureInfo uiCulture2 = Thread.CurrentThread.CurrentUICulture;
Console.WriteLine("The current UI culture is {0}", uiCulture1.Name);
Console.WriteLine("The two CultureInfo objects are equal: {0}",
uiCulture1 == uiCulture2);
}
}
// The example displays output like the following:
// The current UI culture is en-US
// The two CultureInfo objects are equal: True
// </Snippet2>
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// <Snippet1>
using System;
using System.Globalization;
using System.Threading;

public class DefaultThreadEx
{
static Random rnd = new Random();

public static void Main()
{
if (Thread.CurrentThread.CurrentCulture.Name != "fr-FR")
{
// If current culture is not fr-FR, set culture to fr-FR.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("fr-FR");
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("fr-FR");
}
else
{
// Set culture to en-US.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("en-US");
}
ThreadProc();

Thread worker = new Thread(ThreadProc);
worker.Name = "WorkerThread";
worker.Start();
}

private static void DisplayThreadInfo()
{
Console.WriteLine("\nCurrent Thread Name: '{0}'",
Thread.CurrentThread.Name);
Console.WriteLine("Current Thread Culture/UI Culture: {0}/{1}",
Thread.CurrentThread.CurrentCulture.Name,
Thread.CurrentThread.CurrentUICulture.Name);
}

private static void DisplayValues()
{
// Create new thread and display three random numbers.
Console.WriteLine("Some currency values:");
for (int ctr = 0; ctr <= 3; ctr++)
Console.WriteLine(" {0:C2}", rnd.NextDouble() * 10);
}

private static void ThreadProc()
{
DisplayThreadInfo();
DisplayValues();
}
}
// The example displays output similar to the following:
// Current Thread Name: ''
// Current Thread Culture/UI Culture: fr-FR/fr-FR
// Some currency values:
// 8,11 €
// 1,48 €
// 8,99 €
// 9,04 €
//
// Current Thread Name: 'WorkerThread'
// Current Thread Culture/UI Culture: en-US/en-US
// Some currency values:
// $6.72
// $6.35
// $2.90
// $7.72
// </Snippet1>
Loading
Loading