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. Then how do you pass command-line arguments for your program? especially when you are running it from Visual Studio. In this post let us have a look at how we can pass command line arguments in Visual Studio for C## 9.0 Top Level Statement.

Typical C## Console Vs 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 Top-Level Statement in C## 9.0, you can achieve the same using just following line of code.

?

1

System.Console.WriteLine("Hello World!");

Which will produce the same output as previous code block.

Command Line Arguments with Visual Studio

You can pass the parameter for the top-level statement along with multiple arguments.

?

1

System.Console.WriteLine($"Hello {Args?[0]}");

#visual studio #c# 9.0 #visual studio

Command Line Arguments and C# 9.0 Top Level Statement - Visual Studio
1.15 GEEK