Skip to content
Open
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,39 @@
// <Snippet14>
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;

public class Example14
{
public static async Task Main()
{
var tasks = new List<Task>();
Console.WriteLine($"The current culture is {Thread.CurrentThread.CurrentCulture.Name}");
Thread.CurrentThread.CurrentCulture = new CultureInfo("pt-BR");
// Change the current culture to Portuguese (Brazil).
Console.WriteLine($"Current culture changed to {Thread.CurrentThread.CurrentCulture.Name}");
Console.WriteLine($"Application thread is thread {Thread.CurrentThread.ManagedThreadId}");
// Launch six tasks and display their current culture.
for (int ctr = 0; ctr <= 5; ctr++)
tasks.Add(Task.Run(() =>
{
Console.WriteLine($"Culture of task {Task.CurrentId} on thread {Thread.CurrentThread.ManagedThreadId} is {Thread.CurrentThread.CurrentCulture.Name}");
}));

await Task.WhenAll(tasks.ToArray());
}
}

// The example displays output like the following:
// The current culture is en-US
// Current culture changed to pt-BR
// Application thread is thread 9
// Culture of task 2 on thread 11 is pt-BR
// Culture of task 1 on thread 10 is pt-BR
// Culture of task 3 on thread 11 is pt-BR
// Culture of task 5 on thread 11 is pt-BR
// Culture of task 6 on thread 11 is pt-BR
// Culture of task 4 on thread 10 is pt-BR
// </Snippet14>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// <Snippet5>
using System;
using System.Globalization;

public class Example5
{
public static void Main()
{
CultureInfo culture = CultureInfo.CurrentCulture;
Console.WriteLine($"The current culture is {culture.NativeName} [{culture.Name}]");
}
}

// The example displays output like the following:
// The current culture is English (United States) [en-US]
// </Snippet5>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SetCultureExample.Run();

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// <Snippet11>
using System;
using System.Globalization;
using System.Threading;

public class Info11 : MarshalByRefObject
{
public void ShowCurrentCulture()
{
Console.WriteLine($"Culture of thread {Thread.CurrentThread.Name}: {CultureInfo.CurrentCulture.Name}");
}
}

public class SetCultureExample
{
public static void Run()
{
Info11 inf = new Info11();
// Set the current culture to Dutch (Netherlands).
Thread.CurrentThread.Name = "MainThread";
CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("nl-NL");
inf.ShowCurrentCulture();
}
}

// The example displays the following output:
//
// Culture of thread MainThread: nl-NL
// </Snippet11>
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// <snippet11>
using System;
using System.Globalization;
using System.Threading;

public class Example0
{
Expand All @@ -22,6 +21,7 @@ public static void Main()
Console.WriteLine("CurrentUICulture is now {0}.", CultureInfo.CurrentUICulture.Name);
}
}

// The example displays the following output:
// CurrentCulture is en-US.
// CurrentCulture is now th-TH.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net4.8</TargetFramework>
<StartupObject>Example5</StartupObject>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static void Main()
Console.WriteLine("{0:C2}", value);
}
}

// The example displays the following output:
// Current Culture: fr-CA
// 1 634,92 $
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// <Snippet12>
using System;
using System.Globalization;
using System.Threading;

public class Example12
{
public static void Main()
{
double value = 1634.92;
CultureInfo.CurrentCulture = new CultureInfo("fr-CA");
Console.WriteLine($"Current Culture: {CultureInfo.CurrentCulture.Name}");
Console.WriteLine($"{value:C2}\n");

Thread.CurrentThread.CurrentCulture = new CultureInfo("fr");
Console.WriteLine($"Current Culture: {CultureInfo.CurrentCulture.Name}");
Console.WriteLine($"{value:C2}");
}
}

// The example displays the following output:
// Current Culture: fr-CA
// 1 634,92 $
//
// Current Culture: fr
// 1 634,92 €
// </Snippet12>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// <Snippet14>
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.Versioning;
using System.Threading;
using System.Threading.Tasks;

public class Example
{
public static async Task Main()
{
var tasks = new List<Task>();
Console.WriteLine($"The current UI culture is {Thread.CurrentThread.CurrentUICulture.Name}");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("pt-BR");
// Change the current UI culture to Portuguese (Brazil).
Console.WriteLine($"Current UI culture changed to {Thread.CurrentThread.CurrentUICulture.Name}");
Console.WriteLine($"Application thread is thread {Thread.CurrentThread.ManagedThreadId}");
// Launch six tasks and display their current culture.
for (int ctr = 0; ctr <= 5; ctr++)
tasks.Add(Task.Run(() =>
{
Console.WriteLine($"UI Culture of task {Task.CurrentId} on thread {Thread.CurrentThread.ManagedThreadId} is {Thread.CurrentThread.CurrentUICulture.Name}");
}));

await Task.WhenAll(tasks.ToArray());
}
}

// The example displays output like the following:
// The current UI culture is en-US
// Current UI culture changed to pt-BR
// Application thread is thread 9
// UI Culture of task 2 on thread 11 is pt-BR
// UI Culture of task 1 on thread 10 is pt-BR
// UI Culture of task 3 on thread 11 is pt-BR
// UI Culture of task 5 on thread 11 is pt-BR
// UI Culture of task 6 on thread 11 is pt-BR
// UI Culture of task 4 on thread 10 is pt-BR
// </Snippet14>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// <Snippet5>
using System;
using System.Globalization;

public class Example2
{
public static void Main()
{
CultureInfo culture = CultureInfo.CurrentUICulture;
Console.WriteLine($"The current UI culture is {culture.NativeName} [{culture.Name}]");
}
}
// The example displays output like the following:
// The current UI culture is English (United States) [en-US]
// </Snippet5>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// <Snippet1>
using System;
using System.Globalization;

public class Example1
{
public static void Main()
{
Console.WriteLine($"The current UI culture: {CultureInfo.CurrentUICulture.Name}");

CultureInfo.CurrentUICulture = CultureInfo.CreateSpecificCulture("fr-FR");
Console.WriteLine($"The current UI culture: {CultureInfo.CurrentUICulture.Name}");
}
}
// The example displays output like the following:
// The current UI culture: en-US
// The current UI culture: fr-FR
// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// <Snippet1>
using System;
using System.IO;
using System.Globalization;

public class Example
{
public static void Main()
{
// Persist the date and time data.
StreamWriter sw = new StreamWriter(@".\DateData.dat");

// Create a DateTime value.
DateTime dtIn = DateTime.Now;
// Retrieve a CultureInfo object.
CultureInfo invC = CultureInfo.InvariantCulture;

// Convert the date to a string and write it to a file.
sw.WriteLine(dtIn.ToString("r", invC));
sw.Close();

// Restore the date and time data.
StreamReader sr = new StreamReader(@".\DateData.dat");
String input;
while ((input = sr.ReadLine()) != null)
{
Console.WriteLine($"Stored data: {input}\n");

// Parse the stored string.
DateTime dtOut = DateTime.Parse(input, invC, DateTimeStyles.RoundtripKind);

// Create a French (France) CultureInfo object.
CultureInfo frFr = new CultureInfo("fr-FR");
// Displays the date formatted for the "fr-FR" culture.
Console.WriteLine($"Date formatted for the {frFr.Name} culture: {dtOut.ToString("f", frFr)}");

// Creates a German (Germany) CultureInfo object.
CultureInfo deDe= new CultureInfo("de-De");
// Displays the date formatted for the "de-DE" culture.
Console.WriteLine($"Date formatted for {deDe.Name} culture: {dtOut.ToString("f", deDe)}");
}
sr.Close();
}
}
// The example displays the following output:
// Stored data: Tue, 15 May 2012 16:34:16 GMT
//
// Date formatted for the fr-FR culture: mardi 15 mai 2012 16:34
// Date formatted for de-DE culture: Dienstag, 15. Mai 2012 16:34
// </Snippet1>
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 {DateTime.Now:D}");
}

public void DisplayCultures()
{
Console.WriteLine($"Application domain is {AppDomain.CurrentDomain.Id}");
Console.WriteLine($"Default Culture: {CultureInfo.DefaultThreadCurrentCulture}");
Console.WriteLine($"Default UI Culture: {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>
Loading
Loading