Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Return Values

The Main method can be of the type void:

static void Main() 
{
}

It can also return an int:

static int Main() 
{
   return 0;
}

Example

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.

// cs_main.cs
using 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");
      } 
   }
}

Input

5

Sample Output

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

See Also

Command-Line Arguments | Main

Show:
© 2015 Microsoft