Command Line Arguments and C# 9.0 Top Level Statement – Visual Studio

Command Line Arguments and C# 9.0 Top Level Statement – Visual Studio

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.

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.

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

In Visual Studio you can set the parameter from the debug tab as “Application Argument” .

Command Line Arguments in Visual Studio

Once this is set, you can run and test it out.

Command Line Arguments in Visual Studio

Read : How to Pass Command Line Arguments using Visual Studio ?

Looking for mentorship: Connect 1:1 with Abhijit

would be happy to connect and mentor you for your personal and professional growth. I can provide you career guidance, share my thoughts for any interview preparation, or ask me anything related to choosing a technology platform, guide you in writing your first book, help you in public speaking, or starting your own blog.

Abhijit Jana

Abhijit runs the Daily .NET Tips. He started this site with a vision to have a single knowledge base of .NET tips and tricks and share post that can quickly help any developers . He is a Former Microsoft ASP.NET MVP, CodeProject MVP, Mentor, Speaker, Author, Technology Evangelist and presently working as a .NET Consultant. He blogs at http://abhijitjana.net , you can follow him @AbhijitJana . He is the author of book Kinect for Windows SDK Programming Guide.

2 Comments to “Command Line Arguments and C# 9.0 Top Level Statement – Visual Studio”

Comments are closed.