How Hello World! changed – top level statements and functions (C# 9)

C# 9 comes with many new features to enhance productivity and fix bugs. One productivity enhancement comes for small programs and learning C#: top level statements. This also allows for a new way to create a Hello World program with C#. It’s not the first time that a new C# language feature made a change for Hello World. This also happened with C# 6. Let’s come on a tour how Hello World changed during the C# years, and what’s going on with top level statements and functions.

Hello World

C# 1

The first version of C# was influenced by Java, Delphi, and C++. To create a simple Hello World application, the Main method had to be defined as the entry point into the application. With this object-oriented programming language every method had to be put into a type, e.g. the Main method into the Program class. To write a string to the console, the WriteLine method of the Console class can be used. The Console class is defined within the System namespace, so the using declation openend this namespace:

using System;

public class Program
{
  public static void Main()
  {
    Console.WriteLine("Hello World!");
  }
}

C# 6 – Using Static Directive

Following the different versions of C#, every new version offers many new features – just a few mentioned here are generics, LINQ, and async/await. The Hello World application didn’t change – up to C# 6. With C# 6 using static was added which allows to open all members of a static type. It’s no longer needed to use the class name when invoking a static method. The first time with C# the Hello World application was changed. The WriteLine method can be invoked without the class name:

using static System.Console;

public class Program
{
  public static void Main()
  {
    WriteLine("Hello World!");
  }
}

C# 9 – Top-Level Statements

It took some years for the next simplification of Hello World. With C# 9 Top-level statements, it’s no longer necessary to delcare the Main method at all. Just add method invocations top-level.

using System;

Console.WriteLine("Hello World!");

Behind the scenes, the compiler creates a $Program class and a $Main method:

public class $Program
{
  public static void $Main()
  {
    System.Console.WriteLine("Hello World!");
  }
}

Accessing Command Line Arguments

What about command-line arguments? Yes, you can access these as well. If the variable args is used, the compiler creates a Main with string[] args, and you can use it as you’re used to:

if (args.Length > 0)
{
    foreach (var arg in args)
    {
        Console.WriteLine(arg);
    }
}

Using Local Functions

What about declaring methods in the top-level scope without a class? This is not possible. Declaring methods requires a type where they are declared into. However, you can specify local functions in the top-level scope. Local functions can now be declared static if instance variables outside of the function don’t need to be accessed (see the static Foo local function). The AddToX local function accesses the variable x outside of its scope:

Foo();

int x = 3;

int result = AddToX(4);
Console.WriteLine(result);

static void Foo()
{
    Console.WriteLine("Foo");
}

int AddToX(int y)
{
    return x + y;
}

> Local functions are available since C# 7. Static local functions are available since C# 8.

Take away

For many applications, C# 9’s top level statements will not offer any advantage. However, creating small tools, and learning C#, top level statements is a nice enhancements. It’s no longer necessary to define a Main method, and with this a class isn’t needed either.

> This feature is marked in the Roslyn repo with the label New Language Feature – Simple Programs.

If you like this article, consider buying me a coffee which helps me staying up longer and writing more articles.

Buy Me A Coffee

You can get the complete sample code. See the TopLevelStatements sample solution in the CSharp folder.

Enjoy learning and programming!

Christian

More Information

C# 9 – Positional or Nominal Creation

Using, using, using with C# 8

C# 7 – Local Functions – What’s the Value?

C# Proposal Top Level Statements

Top level statements and functions

New Language Feature – Simple Programs

More information on C# and programming .NET Core applications is in my book Professional C# 7 and .NET Core 2.0, and in my workshops.

Hello world ID 49800341 © Makaule | Dreamstime.com

7 thoughts on “How Hello World! changed – top level statements and functions (C# 9)

  1. Great article, thanks for explaining this.

    But as a long time C# user, I can’t help but feel that many of the recent language additions are syntactic sugar gone overboard. The language shortcuts up until now have generally been well thought-out and handy, but things classless top-level programming seem far more confusing than helpful. Was it really that hard to use the scaffolded Main class?

    Concise is good. Clear intent is better.

    Liked by 1 person

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.