Home

C# Topics: The Main() Function

     

Main()'s Arguments

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;

public class Exercise
{
    static int 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"));
	
	return 0;
    }
}

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. The program would then prompt you for the information it needs.

Specifying the Command Line Arguments

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. This argument is referred to as the command line.

Here is an example of passing an argument that represents the command line:

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:

Command Line

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
{
    public 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

Command Line

Getting the Command Line Arguments

To assist you with knowing the command line passed to an application, the Environment class provides a method named  GetCommentLineArgs(). Its syntax is:

public static string[] GetCommandLineArgs();

As you can see, this method returns a string array that represents the arguments to the command line.

Getting the Command Line Information

To assist you with getting the information about the command line of an application, the Environment class provides a property named CommandLine. Here is an example of accessing it:

using System;

public class Exercise
{
    static void Main()
    {
        Console.WriteLine("Command Line: {0}", Environment.CommandLine);

        Environment.Exit(0);
    }
}
 
 

Home Copyright © 2010-2011 FunctionX