Example code for the static void Main (string[] args) parameter in C #

Source: Internet
Author: User
This article mainly introduces C # in the static void Main (string[] args) parameter in detail, this article through the specific examples to introduce you very detailed, the need for friends can refer to the following

The most common example program for Learning C # programming is to output Hello world! in a console application


Using System;namespace demomainargs{class Program {static void Main (string[] args) {  Console.WriteLine ("Hello World ! ");  Console.ReadLine (); } }}
    1. Static: The main method is declared static, which is the entry for the application.

    2. void: Indicates that the main method does not return any content.

    3. String[]args: This is used to receive command-line incoming arguments. String[] is the data type that declares args and can store string arrays.

The command window pops up when you start the program with the Cmd.exe program, where you can enter parameters, string[] args refers to the arguments entered in the command window, that is, command-line arguments.

Args is used to receive command-line arguments. It is optional, not required.

The following is illustrated by a specific example program.

1. New Console program Demomainargs.

2. Write the code.


Using System;namespace demomainargs{class Program {//<summary>///Application portal//</summary>//<param Nam E= "args" > Receive command Line arguments passed in (separated by spaces between multiple parameters) </param> static void Main (string[] args) {  if (args! = null)  {  int argslength = args. Length;  Console.WriteLine ("main function parameter args length:" + argslength);  for (int i = 0;i < argslength;i++)  {   Console.Write ("+ (i + 1) +" parameters are: ");   Console.WriteLine (Args[i]);  }  }  Console.ReadLine (); } }}

3. Press F6 to compile and generate the DemoMainArgs.exe file. Remember not F5 compile and run!!!

Here are 3 ways to show specific effects

Mode one: Start the program in the command line and enter the parameters

Open the command line, enter the path +demomainargs, empty one, and then enter a B c d (you can enter any number of parameters, separated by a space)

Enter ENTER to view the results:

Mode two: input parameters before compiling

Right-click on "Project"-"Properties" to popup the configuration interface. Select "Debug"-"Startup Options"-"command line arguments" text box to enter any parameters, separated by a space

Press F5 to compile and run and see the output directly

Method three: By adding parameters to the shortcut in the program

After the program compiles, create a shortcut program. Right-click on "DemoMainArgs.exe"-"Create Shortcut"

A shortcut file was generated, as follows:

Right click on "DemoMainArgs.exe Shortcut"-"Properties"-"shortcut"-"target", enter any parameters at the end of the text box,

Separated by a space between multiple

Double-click the DemoMainArgs.exe shortcut to run the results as follows

Summary: The 3rd Way is more flexible, can be the client (the user) freely input parameters within the range of features, and then the program according to the input parameters to determine the execution of different business logic.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.