Home >Backend Development >C#.Net Tutorial >How to pass command line parameters in the main method of C#?

How to pass command line parameters in the main method of C#?

WBOY
WBOYforward
2023-09-25 23:41:061232browse

C# 的 main 方法中如何传递命令行参数?

Main() method is the entry point-

static void Main(string[] args)

The parameter array args is used to set parameters-

string[] args)

If you add two parameters , it will set the following -

var args = new string[] {"arg1","arg2”}

Here is the demo code-

Example

using System;

namespace Demo {
   class HelloWorld {

      // args for command line
      static void Main(string[] args) {
         Console.WriteLine("Welcome here!");
         Console.ReadKey();
      }
   }
}

Compiling a C# program using the command line instead of the Visual Studio IDE-

  • Open a text editor and add the above code.

  • Save the file as helloworld.cs

  • Open the command prompt tool and go to the directory where the file is saved.

  • Enter csc helloworld.cs and press Enter to compile your code.

  • If there are no error codes in your code, the command prompt will take you to the next line and generate the helloworld.exe executable file.

  • Type helloworld to execute your program.

  • You can see the output Hello World printed on the screen.

The above is the detailed content of How to pass command line parameters in the main method of C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete