The Main
method can be of the type void:
public static void Main() { ... }
It can also return an int:
public static int Main() { ... return 0; }
In this example, the program contains two classes, Factorial
and MainClass
. The Main
method, which resides in the MainClass
class, is used to read a number from the keyboard, invoke the Fac()
method from the Factorial
class, and calculate and display the factorial of the input number.
// The Main method using System; public class Factorial { public static long Fac(long i) { return ((i <= 1) ? 1 : (i * Fac(i-1))); } } class MyClass { public static void Main() { // Read a string from the keyboard: Console.Write("Enter an integer: "); string s = Console.ReadLine(); // Convert the string to long: long num = Int64.FromString(s); Console.WriteLine("The Factorial of {0} is {1}.", num, Factorial.Fac(num)); } }
Enter an integer: 5 The Factorial of 5 is 120.