C# Main函数的概念是什么呢?C# Main()是C#应用程序的入口点,执行这个函数就是执行应用程序。也就是说,在执行过程开始时,会执行Main()函数,在Main()函数执行完毕时,执行过程就结束了。
C# Main函数的四种情况:
1.static void Main()
2.{
3. }
4.static int Main()
5.{
6. }
7.static void Main(string[] args)
8.{
9. }
10.static int Main(string[] args)
11. {
12. }
1.主程序Main函数一共有以上四种版
2.一个程序中不能有两个以上的Main函数,有且只有一个
3.Main函数只能返回int类型,如果返回1,则从命令行调用不成功。否则成功
4.在命令行传输参数时,存放在string数组args中。使用Length属性来测试输入参数的个数。
5.使用foreach语句来检索所有的参数
6.程序入口主要供其他程序来执行本程序功能
C# Main函数实例:
13.//Main() 和命令行参数
14.
15./*以检举数组中所有元素访问信息
16. for each (string str int args(
17. Console.WriteLine(str);*/
https://www.wendangku.net/doc/937826110.html,ing System;
https://www.wendangku.net/doc/937826110.html,ing System.Collections.Generic;
https://www.wendangku.net/doc/937826110.html,ing System.Text;
21.
https://www.wendangku.net/doc/937826110.html,space HelloWorld
23.{
24.class Program
25.{
26.public static long getx(int x)
27.//阶乘 (注:使用Static定义的方法不用实例化就能使用)
28.{
29.long y = 1;
30.for (int i = 2; i<=x; i++)
31.{
32.y =y * i;
33.}
34.return y;
35.}
36.public static long gety(int x) //阶加
37.{
38.long y = 0;
39.for (int i = 1; i <= x; i++)
40.{
41.y += i;
42.}
43.return y;
44.}
45.static int Main(string[] args)
46.{
47.if (args.Length != 1)
48.//测试args[]数组的长度 ------即是输入的命令行的参数是多少
49.{
50.Console.WriteLine("程序使用说明:输入一个整数来算出其的阶乘.");
51.Console.WriteLine(" 输入一个整数来算出其的阶加.");
52.}
53.else if (Convert.ToInt32(args[0]) < 1 )
54.{
55.Console.WriteLine("输入参数不能小于1");
56.}
57.else
58.{
59.int x; long y,z;
60.try
61.{
62.x = Convert.ToInt32(args[0]);
63.y = getx(x);
64.z = gety(x);
65.Console.WriteLine(x + "的阶乘为: " + y);
66.Console.WriteLine(x + "的阶加为: " + z);
67.return 1; //返回1表示调用程序成功执行
68.}
69.catch(Exception ex)
70.{
71.Console.WriteLine(ex.ToString());
72.}
73.}
74.}
75.}
76.}
C# Main函数实例执行结果
C# Main函数的概念和实例的基本内容就向你介绍到这里,希望对你了解和学习C# Main 函数有所帮助。