[C #] Have you ever understood the Main () function of the entry function? You can use batch processing to convert the Main function,
Have you ever understood the Main () function of the entry function? You can use batch processing to convert the Main function directory.
- Introduction
- Features
- Method Parameters
- Method Return Value
- Example of interaction with Batch Processing
Introduction
We know that when you create a console application, IDE will create a default class Program. cs, which will contain a Main method. When we directly press F5, we will immediately execute the statements in it. But do you know it carefully.
Main
The method is the entry point (also called the entry point) of the C # console application and the Windows form application ). When the application is started,Main
The method is the first method to be called. Only one entry point is allowed in the program. If many classes containMain
You must use the/main compiler option to compile the program and selectMain
Method.
[Note] databases and services are not requiredMain
Method as the entry point.
This may be an example of getting started with C.
Class MyClass {static void Main (string [] args) {// WriteLine (args. Length );}}
[Note] the access modifier is not specified here. The default access level is private.
Features
It is the entry point of the. exe program, where the program control flow starts and ends.
It is declared in a class or structure. It must be static and should not be public.
There are two types of responses:void
Andint
.
It can contain real arguments of the command line.string[]
The parameter. When using VS to create a Windows form application, you can manually add the form parameter. Of course, you can also choose to use a class such as Environment to read the arguments of CMD.
[Note] Here it refers to the Main method.
Method Parameters
You can send parametersMain
Method.
1 static void Main(string[] args)2 3 static int Main(string[] args)
[Note] In a Windows form applicationMain
To enable the CMD parameter in the method, you must manually modify the Program. cs class.Main
Method signature. Because, in the code automatically generated by the Windows Form Designer,The Main method does not contain parameters by default.
. Of course, you can also access the CMD parameter from any location in the CMD or Windows application using methods like Environment. GetCommandLineArgs.
The string [] args parameter is a String array that represents the CMD parameter. We generally determineLength
Attribute to determine whether a parameter exists. See:
1 if (args.Length == 0)2 {3 WriteLine("Hello World.");4 return 1;5 }
You can also useParse
Method to convert a parameter to a value type. See:
1 // The result is the same 2 long num = Int64.Parse (args [0]); 3 4 long num = long. parse (args [0]); // alias of Int64 5 6 long num = Convert. toInt64 (args [0]); // Convert class
Method Return Value
It can return void or int.
1 static void Main () 2 {3 //... 4} 5 6 // return type: int 7 static int Main () 8 {9 //... 10 return 0; 11}
If you do not wantMain
You can usevoid
Simplified code. However, if an integer is returned, the program can interact with other programs or scripts that call the. exe file.
Example of interaction with Batch Processing
I plan to use a batch file to execute the program and test it.Main
The Return Value of the function. When running your program in Windows,Main
The function stores the returned value toERRORLEVEL
. Check is calledERRORLEVEL
The execution results can be known for the batch processing file. Generally, the execution is successful when the return value is 0.
The following is a simple example:Main
The function returns 0 (0 indicates that the program runs successfully ). I named it MyMainTest. cs (changed the name of Program. cs ).
1 class MyMainTest2 {3 static int Main(string[] args)4 {5 //TODO: Other6 return 0;7 }8 }
After writing it, locate the folder containing MyMainTest. cs.
Rem yourTest. bat @ echo off MyMainTest @ if "% ERRORLEVEL %" = "0" goto OK: fail echo Execution Failed echo return value = % ERRORLEVEL % goto end: OK echo Execution succeeded echo Return value = % ERRORLEVEL % goto end: end
Note: Do not double-click it, because it is not effective to directly flash back. Open CMD and find your directory:
C # basic Review Series
C # knowledge Review-serialization
C # knowledge Review-Expression Tree Expression Trees
C # knowledge Review-feature Attribute and AssemblyInfo. cs-understanding common feature attributes
C # knowledge Review-delegated delegate and C # knowledge Review-delegated delegate (continued)
C # knowledge Review-Introduction to events and C # knowledge Review-Event Events
There is no unspeakable secret between the string, the String, the big S, and the small S
C # knowledge Review-do you really understand exceptions?
[Blogger] Anti-Bot
[Source] http://www.cnblogs.com/liqingwen/p/6213624.html
[Reference] Microsoft official documentation