notes on working with C# Main()

Every executable C# application must contain a class defining a Main() method, which is
used to signify the entry point of the application. As we all know, this signature of Main() is adorned
with the public and static keywords. sure also we should understand that public members are accessible from other types, while static members are scoped at the class level (rather than the object level) and can thus
be invoked without the need to first create a new class instance.

In addition to the public and static keywords, this Main() method has a single parameter,
which happens to be an array of strings (string[] args). Although you are not currently bothering
to process this array, this parameter may contain any number of incoming command-line arguments.
let's look at this famouse code :

using System;
class HelloClass
{
public static int Main(string[] args)
{
Console.WriteLine("Hello World!");
Console.ReadLine();
return 0;
}
}
The program logic of HelloClass is within Main() itself. Here, you make use of the Console class,
which is defined within the System namespace. Among its set of members is the static WriteLine()
which, as you might assume, pumps a text string to the standard output. You also make a call to
Console.ReadLine() to ensure the command prompt launched by Visual Studio 2010 remains visible
during a debugging session until you press the Enter key.
Because this Main() method has been defined as returning an integer data type, we return zero
(success) before exiting.

Variations on the Main() Method
The previous iteration of Main() was defined to take a single parameter (an array of strings) and
return an integer data type. This is not the only possible form of Main(), however. It is permissible to
construct your application’s entry point using any of the following signatures (assuming it is contained
within a C# class or structure definition):
// No return type, array of strings as argument.
public static void Main(string[] args)
{
}

// No return type, no arguments.
public static void Main()
{
}

// Integer return type, no arguments.
public static int Main()
{
}




Note your choice of how to construct Main() will be based on two questions. First, do you
need to process any user-supplied command-line parameters? If so, they will be stored in the array
of strings. Second, do you want to return a value to the system when Main() has completed? If so,
you need to return an integer data type rather than void.

Note The Main() method may also be defined as private as opposed to public. Doing so ensures other
assemblies cannot directly invoke an application’s entry point. Visual Studio 2005 automatically defines a program’s Main() method as private.

Note C# is a case-sensitive programming language. Therefore, “Main” is not the same as “main”, and “Readline” is not the same as “ReadLine”. Given this, be aware that all C# keywords are in lowercase (public, lock, global, and so on), while namespaces, types, and member names begin (by convention) with an initial capital letter and have capitalized any embedded words (e.g., Console.WriteLine, System.Windows.Forms.MessageBox, System.Data.SqlClient, and so on).

Comments