[C #] Have you ever understood the Main () function of the entry function? You can use batch processing to convert the Main function,

Source: Internet
Author: User

[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,MainThe method is the first method to be called. Only one entry point is allowed in the program. If many classes containMainYou must use the/main compiler option to compile the program and selectMainMethod.

[Note] databases and services are not requiredMainMethod 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:voidAndint.

  • 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 parametersMainMethod.

1   static void Main(string[] args)2 3   static int Main(string[] args)

[Note] In a Windows form applicationMainTo enable the CMD parameter in the method, you must manually modify the Program. cs class.MainMethod 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 determineLengthAttribute to determine whether a parameter exists. See:

1         if (args.Length == 0)2         {3             WriteLine("Hello World.");4             return 1;5         }

 

You can also useParseMethod 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 wantMainYou can usevoidSimplified 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.MainThe Return Value of the function. When running your program in Windows,MainThe function stores the returned value toERRORLEVEL. Check is calledERRORLEVELThe 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:MainThe 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

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.