Skip to content

The sample does not work #11820

@conradakunga

Description

@conradakunga

Type of issue

Code doesn't work

Description

The code sample does not run.

It throws the following error:

Program.cs(1,1): error CS0246: The type or namespace name 'CultureInfo' could not be found (are you missing a using directive or an assembly reference?)
Program.cs(2,5): error CS0103: The name 'CultureInfo' does not exist in the current context
Program.cs(3,9): error CS0246: The type or namespace name 'CultureInfo' could not be found (are you missing a using directive or an assembly reference?)
Program.cs(4,9): error CS0246: The type or namespace name 'CultureInfo' could not be found (are you missing a using directive or an assembly reference?)
Program.cs(5,9): error CS0246: The type or namespace name 'CultureInfo' could not be found (are you missing a using directive or an assembly reference?)
Program.cs(6,9): error CS0246: The type or namespace name 'CultureInfo' could not be found (are you missing a using directive or an assembly reference?)
Program.cs(7,9): error CS0246: The type or namespace name 'CultureInfo' could not be found (are you missing a using directive or an assembly reference?)
Program.cs(12,10): error CS0246: The type or namespace name 'CultureInfo' could not be found (are you missing a using directive or an assembly reference?)

You have to

  1. Import any required namespaces yourself
  2. Create a class to wrap the sample
  3. Manually provide an entry point for the code to run

BEFORE

CultureInfo[] cultures = [
    CultureInfo.InvariantCulture,
    new CultureInfo("en-us"),
    new CultureInfo("fr-fr"),
    new CultureInfo("de-DE"),
    new CultureInfo("es-ES"),
    new CultureInfo("ja-JP")
];

DateTime thisDate = new(2025, 5, 1, 9, 0, 0);

foreach (CultureInfo culture in cultures)
{
    string cultureName;
    if (string.IsNullOrEmpty(culture.Name))
        cultureName = culture.NativeName;
    else
        cultureName = culture.Name;

    Console.WriteLine($"In {cultureName}, {thisDate.ToString(culture)}");
}

// The example produces the following output:
//    In Invariant Language (Invariant Country), 05/01/2009 09:00:00
//    In en-US, 5/1/2009 9:00:00 AM
//    In fr-FR, 01/05/2009 09:00:00
//    In de-DE, 01.05.2009 09:00:00
//    In es-ES, 01/05/2009 9:00:00
//    In ja-JP, 2009/05/01 9:00:00

AFTER

using System.Globalization;

public class Program
{
    public static async Task Main(string[] args)
    {
        CultureInfo[] cultures = [
            CultureInfo.InvariantCulture,
            new CultureInfo("en-us"),
            new CultureInfo("fr-fr"),
            new CultureInfo("de-DE"),
            new CultureInfo("es-ES"),
           new CultureInfo("ja-JP")
        ];

        DateTime thisDate = new(2025, 5, 1, 9, 0, 0);

        foreach (CultureInfo culture in cultures)
        {
            string cultureName;
            if (string.IsNullOrEmpty(culture.Name))
                cultureName = culture.NativeName;
            else
                cultureName = culture. Name;

            Console.WriteLine($"In {cultureName}, {thisDate.ToString(culture)}");
        }
    }
}

// The example produces the following output:
//    In Invariant Language (Invariant Country), 05/01/2009 09:00:00
//    In en-US, 5/1/2009 9:00:00 AM
//    In fr-FR, 01/05/2009 09:00:00
//    In de-DE, 01.05.2009 09:00:00
//    In es-ES, 01/05/2009 9:00:00
//    In ja-JP, 2009/05/01 9:00:00

Page URL

https://learn.microsoft.com/en-us/dotnet/api/system.datetime.tostring?view=net-9.0

Content source URL

https://github.com/dotnet/dotnet-api-docs/blob/main/xml/System/DateTime.xml

Document Version Independent Id

8338c51d-b558-b442-322c-3d056b62194f

Platform Id

7246d402-ae7d-724f-4e8e-d4cb610a46b4

Article author

@dotnet-bot

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions