About the main (string [] ARGs) parameter input of C #

Source: Internet
Author: User
 

Specify related test code

 

First, writeAbout Main (string [] ARGs) parameter inputAs follows:

 

Using system;

 

Public class hello
{
Public static void main (string [] ARGs)
{
Switch (ARGs [0])
{
Case "boss ":
Console. writeline ("good morning! We are ready to help you at any time! ");
Break;
Case "employee ":
Console. writeline ("good morning! You can get started! ");
Break;
Default:
Console. writeline ("good morning! Good luck! ");

 

Break;
}
Console. writeline ("ARGs [1] value:" + ARGs [1]);
}
}

 

 

 

Test Run

 

Now, if you run it directly, the result is an error "the index has exceeded the array limit. Why? At this time, string [] ARGs does not have any content. That is to say, it is an empty array. If you reference ARGs [0], it will certainly lead to an out-of-bounds error.

 

 

 

 

 

 

Figure: errors caused by direct running

 

How to pass in the ARGs [0] Parameter

 

Now, how can we pass in parameters for our purpose? You can use either of the following methods:

 

1. Set our parameters before running so that they can be passed in. In the "solution" window, right-click your project name and select "properties ", in the new window that appears, select "debug"-> "startup options"-> "command line parameters" and enter the string parameter value you want to input. (Multiple parameters are separated by spaces)

 

2. Save the written test code, for example, program. CS. Then compile (CSC program. CS) in the SDK to pay attention to the path. Get a program.exe file (in the same directory), and then we can input The args parameter in the SDK by entering the compiled file in the SDK and passing in the parameter together: Good morning, the boss of program.exe. The information is as follows:

 

Good morning! We are ready to help you at any time!
The value of argS [1] is good morning.

 

Note::

 

1. if multiple parameters are input, they must be separated by spaces. Format: program.exe parameter 1 parameter 2 ....
2. The number of parameters must be consistent with the number of parameters in your code. Otherwise, an exception "the index exceeds the limit of the array" may occur.

 

 

 

 

 

 

Figure: Compile and pass the Parameter

 

This is now successful !!!

 

Q: How do I set the ARGs [] value in the program? ARGs [] cannot be accessed directly in the form of argS [0] = "hello", nor can it be constructed in the form of argS [0] = new string, the transfer is only available!

 

The following program can run successfully directly. However, if you remove the try block to capture exceptions, the exception may still occur. Think carefully about the role of the try block to give you a more in-depth understanding of the main parameter transfer.

 

Using system;

 

Public class hello
{
Static int I = 0;
Static void print ()
{
I ++;
If (I = 1)
{
String [] S = {"boss "};
Main (s );
}
Else
{
Return;
}
}
Static void main (string [] ARGs)
{
Print ();
Try
{
Switch (ARGs [0])
{
Case "boss ":
Console. writeline ("good morning! We are ready to help you at any time! ");
Break;

 

Case "employee ":
Console. writeline ("good morning! You can get started! ");
Break;
Default:
Console. writeline ("good morning! Good luck! ");
Break;
}
}
Catch
{
Console. writeline ("finished, finish ");
Console. readkey ();
}
}
}

 

Analyze this program

 

First, we assume that the catch exception try block is removed. without a try block, the execution sequence starts from the main () entry, calls print (), and declares S (s) as a local variable in print, only valid in print), then call main (s), input s to execute switch, for ARGs [0], print "good morning! We are ready to help you at any time! ", Print () call completed (s lifecycle ends ). return to main () for further execution. When the switch is executed, because string is a transfer reference, and S is extinct, Arg [0] is null, an error occurs when the index exceeds the array limit.

 


As can be seen from the above, the switch block actually needs to be executed twice. The previous execution in a function call passed in the value of argS [0], and the second execution was due to the death of S, as a result, argS [0] is empty. an error occurred while "the index has exceeded the array limit. the reason for capturing the try block exception is obvious, that is, to prevent the second running of the switch block from directly accessing the catch, prevent ARGs [0] from being used without a value.

 

This program can be extended. Pay attention to the role of the keyword static. It is a great benefit to practice this program repeatedly and constantly destroy and reinforce it.

 

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.