C # command line parameters

Source: Internet
Author: User

 

MainParameters can be used in the method. In this case, they take one of the following forms:

Static IntMain (String[] ARGs) 

static void main ( string [] ARGs)

To use Windows FormsProgramInMainTo enable the command line parameters in the method, you must manually modify the parameters in program. CS.Main. TheCodeCreateMain. You can also useEnvironment..::.CommandLineOrEnvironment..::.GetcommandlineargsAccess command line parameters from any location in the console or Windows application.

 

 MainThe method parameters represent the command line parameters.StringArray. Generally, you can test the Length attribute to determine whether a parameter exists. For example

 

If (ARGs. Length = 0 )
{
System. Console. writeline ( " Please enter a numeric argument. " );
Return 1 ;
}

 

You can also useConvertClass orParseMethod to convert string parameters to numeric values. For example, the following statement usesParseMethod to convert a string to a long number:

 Long num = int64.parse (ARGs [0]);

You can also use the C # type long with the alias int64:

 Long num = long. parse (ARGs [0]);

You can also use the convert method toint64 to do the same job:

Long num = convert. toint64 (s ); 

 

Example

Public Class Functions
{
Public Static Long Factorial ( Int N)
{
// Test for invalid input
If (N <= 0 ) | (N> 256 ))
{
Return - 1 ;
}

// Calculate the factorial iteratively rather than recursively:
Long Tempresult = 1 ;
For ( Int I = 1 ; I <= N; I ++)
{
Tempresult * = I;
}
Return Tempresult;
}
}

Class Mainclass
{
Static Int Main ( String [] ARGs)
{
// Test if input arguments were supplied:
If (ARGs. Length = 0 )
{
System. Console. writeline ( " Please enter a numeric argument. " );
System. Console. writeline ( " Usage: factorial <num> " );
Return 1 ;
}

// Try to convert the input arguments to numbers. This will throw
// An exception if the argument is not a number.
// Num = int. parse (ARGs [0]);
Int Num;
Bool Test = Int . Tryparse (ARGs [ 0 ], Out Num );
If (Test = False )
{
System. Console. writeline ( " Please enter a numeric argument. " );
System. Console. writeline ( " Usage: factorial <num> " );
Return 1 ;
}

//Calculate factorial.
LongResult = functions. factorial (Num );

// Print result.
If (Result =- 1 )
System. Console. writeline ( " Input must be> 0 and <256. " );
Else
System. Console. writeline ( " The factorial of {0} is {1 }. " , Num, result );

Return 0;
}
}
//If 3 is entered on command line,

// Output reads: The factorial of 3 is 6.


If you need to use the CSC command, see Add a path to the CSC command.

Debug the command line as follows:

C: \ Users \ erichu> "D: \ backup \ My Documents \ Visual Studio 2010 \ projects \ mulmethod01 \ mulmethod01 \ bin \ debug \ mulmethod01.exe" 23

 

 

 

 

 

 

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.