C# 9.0 introduced a new feature called Top Level Statement. Top-Level Statement allows developers to write programs without explicitly defining the class or main method. Until C# 9.0, It was all about the Main() method where program control start and ends. With C# 9.0, you don’t need to mention the Main() method or Class definition explicitly using Top-Level Statement. Let us take a look at how to use the Top-Level statement in C# and how it works internally.
Top-Level Statement in C# 9.0
A typical Hello World Console Application for C# looks as below
using System;
namespace TopLevelStatementCsharp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
With the above code executed, you will find typical “Hello World!” output in the console! and nothing new here.
With Top-Level Statement in C# 9.0, you can achieve the same using just following line of code.
System.Console.WriteLine("Hello World!");
Which will produce the same output as previous code block.
Like the basic statement of execution, you can pass the command line argument for the top-level statement. You can pass as many as an argument you want.
System.Console.WriteLine($"Hello {args?[0]}");
Following image shows an quick example of passing command line argument for Top-Level Statement
You can provide the Command line argument for any console application from Project Properties > Debug > Application Arguments
Popular Tips from Daily .NET Tips : How to Pass Command Line Arguments using Visual Studio ?
If you have any complication error for this, you have to ensure you have the selected “Targeted Framework” to .NET 5.0. This setting available under Project Properties -> Application
This is simple and reduce developer time using typing repetitive statement.
Behind the Scene
While as a developer we don’t write all these additional codes, behind the scenes C# Complier does the job for us. We can easily explore this using the ILDASM Tool and inspecting the DLLs generated by the C# Code
The following image shows the IL Code generated for the started C# Main Method Code, which clearly defined the Main() method, which takes an argument and is the executions’ entry point. Idstr defined the constant string for “Hello World!” which is getting printed in the following statement.
Similarly for Top-Level Statement as well, the IL Generated code show the same set of IL Block that would be executed.
This clearly shows the complier generates the similar code in the behind and get that executed which abstracted some line of code execution.
Planning to start learning Azure – Check this out : 10 Azure Cloud services that every Developers, Consultant, and Architects should Know and Learn it well
Pingback: The Morning Brew - Chris Alcock » The Morning Brew #3212
Pingback: The Morning Brew #3212 - Software Mile.com
Pingback: Top Level Statement in C# 9.0 - Software Mile.com
Pingback: Dew Drop – April 16, 2021 (#3424) – Morning Dew by Alvin Ashcraft
Pingback: Getting Started with Azure Web PubSub Service - Daily .NET Tips - News WWC
Pingback: Getting Started with Azure Web PubSub Service - Daily .NET Tips
Pingback: Command Line Arguments and C# 9.0 Top Level Statement - Visual Studio - Daily .NET Tips