CHAPTER 3 CORE C# PROGRAMMING CONSTRUCTS, PART I
them momentarily). Finally, this Main() method has been set up with a void return value, meaning we do
not explicitly define a return value using the return keyword before exiting the method scope.
The logic of Program 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, sends a text string and carriage return to the standard output. You also make a call to
Console.ReadLine() to ensure the command prompt launched by the Visual Studio IDE remains visible
during a debugging session until you press the Enter key. You will learn more about the System.Console
class shortly.
Variations on the Main() Method
By default, Visual Studio will generate a Main() method that has a void return value and an array of
string types as the single input parameter. 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):
// int return type, array of strings as the parameter.
static int Main(string[] args)
{
// Must return a value before exiting!
return 0;
}
// No return type, no parameters.
static void Main()
{
}
// int return type, no parameters.
static int Main()
{
// Must return a value before exiting!
return 0;
}
Note The Main() method may also be defined as public as opposed to private, which is assumed if you do not
supply a specific access modifier. Visual Studio automatically defines a program’s Main() method as implicitly
private.
Obviously, your choice of how to construct Main() will be based on two questions. First, do you want
to return a value to the system when Main() has completed and your program terminates? If so, you need
to return an int data type rather than void. Second, do you need to process any user-supplied,
command- Ɩ