Home

The Main() Method

 

Introduction

When a program starts, it looks for an entry point. This is the role of the Main() method. In fact, a program, that is an executable program, starts by, and stops with, the Main() method. The way this works is that, at the beginning, the compiler looks for a method called Main. If it doesn't find it, it produces an error. If it finds it, it enters the Main() method in a top-down approach, starting just after the opening curly bracket. If it finds a problem and judges that it is not worth continuing, it stops and lets you know. If, or as long as, it doesn't find a problem, it continues line after line, with the option to even call or execute a method in the same file or in another file. This process continues to the closing curly bracket "}". Once the compiler finds the closing bracket, the whole program has ended and stops.

If you want the user to provide additional information when executing your program, you can take care of this in the Main() method. Consider the following code written in a file saved as Exercise.cs:

using System;

namespace CSharpLessons
{
    class Exercise
    {
	static void Main()
	{
	    string FirstName = "James";
     	    string LastName  = "Weinberg";
             double WeeklyHours = 36.50;
	    double HourlySalary = 12.58;
	   
	    string FullName = LastName + ", " + FirstName;
	    double WeeklySalary = WeeklyHours * HourlySalary;

	    Console.WriteLine("Employee Payroll");
	    Console.WriteLine("Full Name:    {0}", FullName);
	    Console.WriteLine("WeeklySalary: {0}", WeeklySalary.ToString("C"));
	}
    }
}

To execute the application, at the Command Prompt and after Changing to the Directory that contains the file, you would type

csc Exercise.cs

and press Enter. To execute the program, you would type the name Exercise and press Enter. This would produce:
 

C:\CSharp Lessons\CommandLine1>csc Exercise.cs
Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4
for Microsoft (R) .NET Framework version 1.1.4322
Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.


C:\CSharp Lessons\CommandLine1>Exercise
Employee Payroll
Full Name:    Weinberg, James
WeeklySalary: $459.17

C:\CSharp Lessons\CommandLine1>
 

Returning a Value From the Main Method

So far, we have used the Main() method as it is defined by default when you create an application using using New Project dialog box. This default implementation of the Main() method is of type void. Another way to implement the Main() method is to make it return an integer. The rule is the same as for any method of type int. The Main() method can return any type of integer as long as it is a valid integer. Here is an example:

using System;

class Exercise
{
    static int Main()
    {
        return 244006;
    }
}

Command Request from Main()

To compile a program, you would simply type the csc command at the command prompt. Then, to execute a program, you would type its name at the prompt. If you distribute a program, you would tell the user to type the name of the program at the command prompt. In some cases, you may want the user to type additional information besides the name of the program. To request additional information from the user, you can pass a string argument to the Main() method. The argument should be passed as an array and make sure you provide a name for the argument. Here is an example:

using System;

class ObjectName
{
    static int Main(string[] args)
    {
        return 0;
    }
}

The reason you pass the argument as an array is so you can use as many values as you judge necessary. To provide values at the command prompt, the user types the name of the program followed by each necessary value. Here is an example:

The values the user would provide are stored in a zero-based array without considering the name of the program. The first value (that is, after the name of the program) is stored at index 0, the second at index 1, etc. Based on this, the first argument is represented by args[0], the second is represented by args[1], etc.

Each of the values the user types is a string. If any one of them is not a string, you should/must convert/cast its string first to the appropriate value. Consider the following source code:

using System;

namespace CSharpLessons
{
    class Exercise
    {
	static int Main(string[] Argument)
	{
	    string FirstName;
     	    string LastName;
                    Double WeeklyHours;
	    Double HourlySalary;
	   
	    FirstName    = Argument[0];
                    LastName     = Argument[1];
	    WeeklyHours  = Double.Parse(Argument[2]);
	    HourlySalary = Double.Parse(Argument[3]);

	    string FullName = LastName + ", " + FirstName;
	    Double WeeklySalary = WeeklyHours * HourlySalary;

	    Console.WriteLine("Employee Payroll");
	    Console.WriteLine("Full Name:       {0}", FullName);
	    Console.WriteLine("WeeklySalary: {0}", WeeklySalary.ToString("C"));

	    return 0;
	}
    }
}

To compile it at the Command Prompt, after switching to the directory that contains the file, you would type

csc Exercise.cs

and press Enter. To execute the program, you would type Exercise followed by a first name, a last name, and two decimal values. An example would be Exercise Catherine Engolo 42.50 20.48

 

Home Copyright © 2006-2016, FunctionX, Inc.