Home  >  Article  >  Backend Development  >  C# difficulties are broken down one by one (4): main function

C# difficulties are broken down one by one (4): main function

黄舟
黄舟Original
2016-12-22 14:09:201712browse

I still remember when I first learned about computer programming in C language, Main(){}. At that time, I didn’t understand what the entry function meant. I just copied the examples in the book and ran printf line by line to see.

In C#, Main() is the main entry function. We know that C and C# are compiled languages. It can be imagined as the beginning part of a program. The Main() function enters the statement-by-statement compilation and execution. If the HTML page is also called a programming language, then it is executed sentence by sentence (downloaded) from top to bottom; js is also executed from top to bottom, but js is quite strange, and variable scopes must be treated specially; in asp.net Generally speaking, Page_Load(object sender,EventArgs e) can be considered as the main entrance. The string[] args parameter in

Main()

Main() function must be modified with static, which means it must be static and cannot be instantiated - if it can be instantiated, the program will be over (multi-threaded!? ). The default Main() function has formal parameters, such as static void Main(string[] args). A very crucial issue here is: many people think that the parameters are only used when the program requires the user to enter parameters. You must bring it if it is valuable. This is completely wrong. I also understood it this way before. I only found out today when I was looking up information when writing this article. Please refer to the code below

using System; 

/****************************** 
* Chapter:C#难点逐个击破(四) 
* Author:王洪剑 
* Date:2010-1-15 
* Blog:http://www.51obj.cn/ 
* Email:walkingp@126.com 
* Description:容易出错的Main参数理解 
* ***************************/ 
namespace TestMain 
{ 
class Program 
{ 
static void Main()//此处没有加string[] args 
{ 
Console.WriteLine("请输入您的姓:"); 
String firstName = Console.ReadLine(); 
Console.WriteLine("请输入您的名:"); 
String lastName = Console.ReadLine(); 
Console.WriteLine("您的姓名是:{0}{1}", firstName, lastName); 
} 
} 
}

C# difficulties are broken down one by one (4): main function

Secondly, the parameter type can only be string[], otherwise a compilation error will occur

C# difficulties are broken down one by one (4): main function

So what does this parameter do? For example, you will know, notepad c:boot.ini, or IEXPLORER.exe http://www.g.cn, yes, the parameters inside are the parameters to be added to compile the exe. For example, we can add a Parameter min or hide, and then add the corresponding code to minimize or hide the program when it is running. The return value of the

Main() function

Main() is similar to the default return value. In addition, it can also return int, and only int can be returned. There are not many uses for returning int, as described in msdn Use the batch bat to call the return result of the program execution, and judge whether the program is executed smoothly based on the results. The return type does not display "

" in the console. The above is the content of the main function that has been broken down one by one in C# (4). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn