Skip to content
The Chat Archive
llama-4-maverick-17b-128e-instruct· Meta LMArena Human Preference 140K

do it again but in .net9

  1. User

    do it again but in .net9

  2. Assistant

    Here's the same example, but this time using the latest features available in .NET 9 (preview) and C# 13 (preview). Keep in mind that .NET 9 is still in preview and features may change.

    // Person.cs
    public class Person(string name, int age)
    {
        public string Name { get; } = name;
        public int Age { get; } = age;
    
        public void SayHello() => Console.WriteLine($"Hello, my name is {Name} and I'm {Age} years old.");
    }
    
    // Program.cs
    new Person("John", 30).SayHello();
    

    What's new here?

    1. Primary Constructors: The Person class now uses a primary constructor (Person(string name, int age)) to simplify initialization. This is a feature introduced in C# 11, but still relevant and useful in .NET 9.
    2. Top-Level Statements: The Program.cs file still uses top-level statements (introduced in C# 9.0), which allow us to write code directly in the file without the need for a Main method or a Program class.

    To create a .NET 9 project:

    1. Install the .NET 9 SDK (preview) from the .NET download page.
    2. Open a terminal or command prompt.
    3. Run the following command to create a new .NET 9 console application:

    dotnet new console --framework net9.0

    4. Replace the contents of `Program.cs` and add a new file `Person.cs` with the code above.
    
    **To run the application:**
    
    1. Navigate to the project directory in the terminal.
    2. Run the application using:
       ```bash
    dotnet run
    

    You should see the same output as before:

    Hello, my name is John and I'm 30 years old.
    

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.