C# Main Function

In this chapter you will learn:

  1. Four types of man functions
  2. Syntax for Main functions
  3. Example for Main functions
  4. Using foreach to loop through the parameter from Main function

Description

All C# applications begin execution by calling Main( ). Main is the entry point method for a C# application.

Syntax

C# console application regards the following signatures for its entry main function:

  • public static void Main();
  • public static void Main(string[] args);
  • public static int Main();
  • public static int Main(string[] args);

Example

The main function returning an int status.


using System;//  w ww . ja va2  s  .c om

class MainClass
{
    public static int Main()
    {
        Console.WriteLine("Hello, Universe!");
        return(0);
    }
}

The code above generates the following result.

Example 2


using System;// ww  w.j a  va 2  s  . c  o m

class Program
{
    static void Main(string[] args)
    {
       Console.WriteLine(args.Length);
        foreach (string arg in args){
            Console.WriteLine(arg);
        }
    }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. What are Extension Methods
  2. Syntax to create Extension Methods
  3. Example
  4. Extension methods versus instance methods
Home »
  C# Tutorial »
    C# Types »
      C# Method
C# class method
C# method parameters
C# ref parameter
C# out parameter
C# named parameters
C# params parameters
C# Optional parameters
C# method overloading
Recursive methods
C# Return Statement
static method
C# Main Function
C# Extension Methods