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

WBOY
Release: 2023-09-25 23:41:06
forward
1200 people have browsed it

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

Main() method is the entry point-

static void Main(string[] args)
Copy after login

The parameter array args is used to set parameters-

string[] args)
Copy after login

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

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

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();
      }
   }
}
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template