为什么Main(string [] args)函数总是始终首先执行,即,在代码中存在任何其他函数之前

时间:2019-05-28 05:31:48

标签: c#

我已经开始学习c#,它也是我的第一门编程语言。 在执行功能时,我注意到所有功能都是通过 Main(string [] args)函数。

就像这段代码来自Microsoft Docs,首先调用Main(string [] args)函数。 这个功能有什么特别之处吗?

using System;
using System.Collections.Generic;

public class Example
{
   public static void Main()
   {
       var (_, _, _, pop1, _, pop2) = QueryCityDataForYears("New York City", 1960, 2010);

       Console.WriteLine($"Population change, 1960 to 2010: {pop2 - pop1:N0}");
   }

   private static (string, double, int, int, int, int) QueryCityDataForYears(string name, int year1, int year2)
   {
      int population1 = 0, population2 = 0;
      double area = 0;

      if (name == "New York City") {
         area = 468.48; 
         if (year1 == 1960) {
            population1 = 7781984;
         }
         if (year2 == 2010) {
            population2 = 8175133;
         }
      return (name, area, year1, population1, year2, population2);
      }

      return ("", 0, 0, 0, 0, 0);
   }
}

1 个答案:

答案 0 :(得分:0)

任何程序开始执行时,操作系统将从主方法开始执行该方法。让我用一个外行的术语解释一下,“有一间房子,您想进入拜访您的朋友。你会怎么做?您将寻找进入房屋的入口门:)。因此,这里的主要功能只是入口,在程序开始执行时,操作系统会在启动时启动。

相关问题