Skip to content

Commit 8ccdcc7

Browse files
authored
Current thread culture -> current culture (dotnet#7410)
1 parent 683f007 commit 8ccdcc7

File tree

29 files changed

+247
-234
lines changed

29 files changed

+247
-234
lines changed
Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
11
// <Snippet1>
22
using System;
33
using System.Globalization;
4-
using System.Threading;
54

65
public class DateToStringExample
76
{
8-
public static void Main()
9-
{
10-
CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
11-
DateTime exampleDate = new DateTime(2008, 5, 1, 18, 32, 6);
7+
public static void Main()
8+
{
9+
CultureInfo currentCulture = CultureInfo.CurrentCulture;
10+
DateTime exampleDate = new DateTime(2021, 5, 1, 18, 32, 6);
1211

13-
// Display the date using the current (en-US) culture.
14-
Console.WriteLine(exampleDate.ToString());
12+
// Change the current culture to en-US and display the date.
13+
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
14+
Console.WriteLine(exampleDate.ToString());
1515

16-
// Change the current culture to fr-FR and display the date.
17-
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("fr-FR");
18-
Console.WriteLine(exampleDate.ToString());
16+
// Change the current culture to fr-FR and display the date.
17+
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("fr-FR");
18+
Console.WriteLine(exampleDate.ToString());
1919

20-
// Change the current culture to ja-JP and display the date.
21-
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("ja-JP");
22-
Console.WriteLine(exampleDate.ToString());
20+
// Change the current culture to ja-JP and display the date.
21+
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("ja-JP");
22+
Console.WriteLine(exampleDate.ToString());
2323

24-
// Restore the original culture
25-
Thread.CurrentThread.CurrentCulture = currentCulture;
26-
}
24+
// Restore the original culture
25+
CultureInfo.CurrentCulture = currentCulture;
26+
}
2727
}
28+
2829
// The example displays the following output to the console:
29-
// 5/1/2008 6:32:06 PM
30-
// 01/05/2008 18:32:06
31-
// 2008/05/01 18:32:06
30+
// 5/1/2021 6:32:06 PM
31+
// 01/05/2021 18:32:06
32+
// 2021/05/01 18:32:06
3233
// </Snippet1>

samples/snippets/csharp/VS_Snippets_CLR_System/system.DateTime.ToString/cs/ToString2.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// <Snippet2>
22
using System;
33

4-
public class DateToStringExample
4+
public class DateToStringExample2
55
{
6-
public static void Main()
6+
public static void Main0()
77
{
88
DateTime dateValue = new DateTime(2008, 6, 15, 21, 15, 07);
99
// Create an array of standard format strings.

samples/snippets/csharp/VS_Snippets_CLR_System/system.DateTime.ToString/cs/ToString4.cs

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

5-
public class Example
5+
public class Example2
66
{
7-
public static void Main()
7+
public static void Main2()
88
{
99
CultureInfo[] cultures = new CultureInfo[] {CultureInfo.InvariantCulture,
1010
new CultureInfo("en-us"),
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

samples/snippets/csharp/VS_Snippets_CLR_System/system.DateTime.ToString/cs/tostring3.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
// <Snippet4>
1+
// <Snippet4>
22
using System;
33
using System.Globalization;
44

55
public class Example
66
{
7-
public static void Main()
7+
public static void Main1()
88
{
99
// Create an array of all supported standard date and time format specifiers.
1010
string[] formats = {"d", "D", "f", "F", "g", "G", "m", "o", "r",
1111
"s", "t", "T", "u", "U", "Y"};
1212
// Create an array of four cultures.
13-
CultureInfo[] cultures = {CultureInfo.CreateSpecificCulture("de-DE"),
14-
CultureInfo.CreateSpecificCulture("en-US"),
15-
CultureInfo.CreateSpecificCulture("es-ES"),
16-
CultureInfo.CreateSpecificCulture("fr-FR")};
13+
CultureInfo[] cultures = {CultureInfo.GetCultureInfo("de-DE"),
14+
CultureInfo.GetCultureInfo("en-US"),
15+
CultureInfo.GetCultureInfo("es-ES"),
16+
CultureInfo.GetCultureInfo("fr-FR")};
1717
// Define date to be displayed.
1818
DateTime dateToDisplay = new DateTime(2008, 10, 31, 17, 4, 32);
1919

@@ -28,6 +28,7 @@ public static void Main()
2828
}
2929
}
3030
}
31+
3132
// The example displays the following output:
3233
// d Format Specifier de-DE Culture 31.10.2008
3334
// d Format Specifier en-US Culture 10/31/2008

samples/snippets/csharp/VS_Snippets_CLR_System/system.DateTime.ToString/cs/tostring5.cs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
// <Snippet5>
22
using System;
33
using System.Globalization;
4-
using System.Threading;
54

6-
public class Example
5+
public class Example3
76
{
8-
public static void Main()
9-
{
10-
String[] formats = { "G", "MM/yyyy", @"MM\/dd\/yyyy HH:mm",
11-
"yyyyMMdd" };
12-
String[] cultureNames = { "en-US", "fr-FR" };
13-
DateTime date = new DateTime(2015, 8, 18, 13, 31, 17);
14-
foreach (var cultureName in cultureNames) {
15-
var culture = new CultureInfo(cultureName);
16-
CultureInfo.CurrentCulture = culture;
17-
Console.WriteLine(culture.NativeName);
18-
foreach (var format in formats)
19-
Console.WriteLine(" {0}: {1}", format,
20-
date.ToString(format));
21-
Console.WriteLine();
22-
}
23-
}
7+
public static void Main3()
8+
{
9+
string[] formats = { "G", "MM/yyyy", @"MM\/dd\/yyyy HH:mm", "yyyyMMdd" };
10+
string[] cultureNames = { "en-US", "fr-FR" };
11+
DateTime date = new DateTime(2015, 8, 18, 13, 31, 17);
12+
foreach (var cultureName in cultureNames)
13+
{
14+
var culture = new CultureInfo(cultureName);
15+
CultureInfo.CurrentCulture = culture;
16+
Console.WriteLine(culture.NativeName);
17+
foreach (var format in formats)
18+
Console.WriteLine($" {format}: {date.ToString(format)}");
19+
Console.WriteLine();
20+
}
21+
}
2422
}
2523
// The example displays the following output:
2624
// English (United States)

samples/snippets/csharp/VS_Snippets_CLR_System/system.Globalization.CultureInfo.CurrentCulture2/CS/currentculture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class Example
77
{
88
public static void Main()
99
{
10-
// Display the name of the current thread culture.
10+
// Display the name of the current culture.
1111
Console.WriteLine("CurrentCulture is {0}.", CultureInfo.CurrentCulture.Name);
1212

1313
// Change the current culture to th-TH.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

samples/snippets/visualbasic/VS_Snippets_CLR_System/system.DateTime.ToString/vb/ToString1.vb

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,31 @@ Option Strict On
33

44
' <Snippet1>
55
Imports System.Globalization
6-
Imports System.Threading
76

87
Module DateToStringExample
9-
Public Sub Main()
10-
Dim currentCulture As CultureInfo = Thread.CurrentThread.CurrentCulture
11-
Dim exampleDate As Date = #05/01/2008 6:32:06PM#
12-
13-
' Display the date using the current (en-US) culture.
14-
Console.WriteLine(exampleDate.ToString())
8+
Public Sub Main()
9+
Dim currentCulture As CultureInfo = CultureInfo.CurrentCulture
10+
Dim exampleDate As Date = #05/01/2021 6:32:06PM#
1511

16-
' Change the current culture to fr-FR and display the date.
17-
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("fr-FR")
18-
Console.WriteLine(exampleDate.ToString())
12+
' Change the current culture to en-US and display the date.
13+
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-US")
14+
Console.WriteLine(exampleDate.ToString())
1915

20-
' Change the current culture to ja-JP and display the date.
21-
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("ja-JP")
22-
Console.WriteLine(exampleDate.ToString())
23-
24-
' Restore the original culture
25-
Thread.CurrentThread.CurrentCulture = currentCulture
26-
End Sub
16+
' Change the current culture to fr-FR and display the date.
17+
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("fr-FR")
18+
Console.WriteLine(exampleDate.ToString())
19+
20+
' Change the current culture to ja-JP and display the date.
21+
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("ja-JP")
22+
Console.WriteLine(exampleDate.ToString())
23+
24+
' Restore the original culture
25+
CultureInfo.CurrentCulture = currentCulture
26+
End Sub
2727
End Module
28+
2829
' The example displays the following output to the console:
29-
' 5/1/2008 6:32:06 PM
30-
' 01/05/2008 18:32:06
31-
' 2008/05/01 18:32:06
30+
' 5/1/2021 6:32:06 PM
31+
' 01/05/2021 18:32:06
32+
' 2021/05/01 18:32:06
3233
' </Snippet1>

samples/snippets/visualbasic/VS_Snippets_CLR_System/system.DateTime.ToString/vb/ToString2.vb

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,29 @@
22
Option Strict On
33

44
' <Snippet2>
5-
Module DateToStringExample
6-
Public Sub Main()
7-
Dim dateValue As Date = #6/15/2008 9:15:07PM#
8-
' Create an array of standard format strings.
9-
Dim standardFmts() As String = {"d", "D", "f", "F", "g", "G", _
10-
"m", "o", "R", "s", "t", "T", _
5+
Module DateToStringExample2
6+
Public Sub Main2()
7+
Dim dateValue As Date = #6/15/2008 9:15:07PM#
8+
' Create an array of standard format strings.
9+
Dim standardFmts() As String = {"d", "D", "f", "F", "g", "G",
10+
"m", "o", "R", "s", "t", "T",
1111
"u", "U", "y"}
12-
' Output date and time using each standard format string.
13-
For Each standardFmt As String In standardFmts
14-
Console.WriteLine("{0}: {1}", standardFmt, _
12+
' Output date and time using each standard format string.
13+
For Each standardFmt As String In standardFmts
14+
Console.WriteLine("{0}: {1}", standardFmt,
1515
dateValue.ToString(standardFmt))
16-
Next
17-
Console.WriteLine()
18-
19-
' Create an array of some custom format strings.
20-
Dim customFmts() As String = {"h:mm:ss.ff t", "d MMM yyyy", "HH:mm:ss.f", _
21-
"dd MMM HH:mm:ss", "\Mon\t\h\: M", "HH:mm:ss.ffffzzz" }
22-
' Output date and time using each custom format string.
23-
For Each customFmt As String In customFmts
24-
Console.WriteLine("'{0}': {1}", customFmt, _
16+
Next
17+
Console.WriteLine()
18+
19+
' Create an array of some custom format strings.
20+
Dim customFmts() As String = {"h:mm:ss.ff t", "d MMM yyyy", "HH:mm:ss.f",
21+
"dd MMM HH:mm:ss", "\Mon\t\h\: M", "HH:mm:ss.ffffzzz"}
22+
' Output date and time using each custom format string.
23+
For Each customFmt As String In customFmts
24+
Console.WriteLine("'{0}': {1}", customFmt,
2525
dateValue.ToString(customFmt))
26-
Next
27-
End Sub
26+
Next
27+
End Sub
2828
End Module
2929
' This example displays the following output to the console:
3030
' d: 6/15/2008

0 commit comments

Comments
 (0)