Main 方法的返回值

来源:互联网 发布:有哪些好听的网络歌曲 编辑:程序博客网 时间:2024/04/29 12:12
C# 程序员参考 

返回值

Main 方法可以是 void 类型:

static void Main() {}

它还可以返回 int

static int Main() {   return 0;}

示例

在此示例中,程序包含两个类 FactorialMainClass。驻留在 MainClass 类中的 Main 方法用于从键盘读取数字,从 Factorial 类调用 Fac 方法,以及计算并显示输入数字的阶乘。

// cs_main.csusing System; public class Factorial {   public static long Fac(long i)    {      return ((i <= 1) ? 1 : (i * Fac(i-1)));    } }class MainClass {   public static void Main()    {      // Read a string from the keyboard:      Console.Write("Enter an integer: ");      string s = Console.ReadLine();      // Convert the string to long:            try      {         long num = Int64.Parse(s);         Console.WriteLine("The Factorial of {0} is {1}.",                           num, Factorial.Fac(num));       }      catch (System.FormatException)      {         Console.WriteLine("Invalid input specified");      }    }}

输入

5

示例输出

Enter an integer: 5The Factorial of 5 is 120.