Table of contents
1.
Introduction
2.
Example of command-line argument
2.1.
Input
2.2.
Output
3.
Overview
4.
Valid Main signatures
5.
Frequently asked questions
5.1.
What are the command line arguments?
5.2.
What do args mean in C#?
5.3.
What is the difference between boxing and unpacking in C#?
5.4.
What is the purpose of Devenv EXE?
5.5.
What does void mean in C#?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

C# Command Line Arguments

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

This article will explain the C# Command Line Argument. We all know that a parameter can be passed as an argument to a function, but what about the Main(string[] args) method? In C#, may parameters be provided to the Main() method? Yes, in C#, parameter(s) can be provided as command-line arguments to the Main() method.

The Main() method is where the program begins to run. Any method's parameter is not accepted by the main method. The command line is used to get parameters. It's an array-type parameter that can take an arbitrary number of parameters at runtime.

Args is a string-type array that can carry several parameters in Main(string[] args).

Recommended Topic, Palindrome in C#, singleton design pattern in c#, and Ienumerable vs Iqueryable.

Example of command-line argument

Consider the following command line example in C# to understand command-line arguments better.

Using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace command
{
  class Program
  {
      static void Main(string[] args)  //PROGRAM STARTS FROM HERE
                                                       //ARGS COMMAND-LINE ARGUMENT
      {
          Console.WriteLine("First Name is " + args[0]);
          Console.WriteLine("Last Name is " + args[1]);
          Console.ReadLine();
      }
  }
}

Input

Coding 
Ninja

Output

First Name is Coding
Last Name is Ninja.

 

We gave two parameters to the main procedure as command-line arguments in the earlier example. The Main() method accepts both parameters at runtime and stores them in the args variable, a string type array. The array's index position is then used to access both values.

Overview

  • The Main method is the starting and ending point for program control in an executable program.
     
  • The main function is declared within a class or struct. Main doesn't need to be public. (In the previous case, it had private access by default.) The enclosing class or struct doesn't need to be static.
     
  • A void, int, or, starting with C# 7.1, Task or Task return type can be used in Main.
     
  • Main's declaration may include the async modifier if and only if it returns a Task or Task<int>. An async void Main method is expressly excluded.
     
  • A string[] parameter that holds command-line parameters can be specified with or without the Main method. You can add the option directly or use the GetCommandLineArgs() method to get the command-line arguments when using Visual Studio to construct Windows apps. Parameters are read as command-line arguments with a zero index. The program name is not considered the first command-line parameter in the args array, as it is in C and C++, but it is the first element in the GetCommandLineArgs() method.

Valid Main signatures

public static int Main() { }
public static void Main() { }
public static async Task Main() { }
public static async Task<int> Main() { }
public static int Main(string[] args) { }
public static void Main(string[] args) { }
public static async Task Main(string[] args) { }
public static async Task<int> Main(string[] args) { }

Frequently asked questions

What are the command line arguments?

Command-line arguments are used when the main has parameters. An integer and an array of pointers to char(strings) represent user-defined values given to the main. The first parameter, args, defines the number of elements identified in the second argument.

What do args mean in C#?

The args parameter keeps all command-line arguments passed by the user when running the program. There are four arguments if you launch your application from the console using the command program.exe. The four strings "there," "our," "4", and "parameters" will be in your args parameter.

What is the difference between boxing and unpacking in C#?

Boxing converts a value type to the type object or any interface type implemented by this value type. When the common language runtime (CLR) boxes a value type, it wraps the value inside a System. Object instance and stores it on the managed heap. Unboxing extracts the value type from the object.

What is the purpose of Devenv EXE?

Devenv allows you to configure the IDE, build projects, debug projects, and deploy the command line. Use these switches to start the IDE in a specific configuration or execute it from a script or.bat file (such as a nightly build script).

What does void mean in C#?

You use the return type void to indicate that a method (or a local function) does not return a value.

Conclusion

This article has gone through an essential C# Command Line Argument. Arguments are supplied to the Main() procedure by the user or programmer. The Main() method is where a program begins to run. The array of strings is passed to the Main() function. Click here to know more.

Check out the Introduction in C#. It will clear the concept.

Check out more related articles: 

Are you planning to ace the interviews of reputed product-based companies like Amazon, Google, Microsoft, and more?

Please go check out our C# practice course. 

Attempt our Online Mock Test Series on Coding Ninjas Studio now!

We hope that this blog has helped you enhance your knowledge regarding C# and if you would like to learn more, check out our course. . Do upvote our blog to help other ninjas grow. Happy Coding!

Ninja, have a great time learning.

Live masterclass