the class that defines the
Main() method is termed the application object
While it is possible for a single executable application to have more than one application object
(which can be useful when performing unit tests), you must informthe compiler which
Main()
method should be used as the entry point via the
/main option of the command-line compiler.
Note that the signature of
Main() is adorned with the static keyword, implies that static members are scoped to 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
static keyword, 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 (you’ll see
how to access 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, asyou might assume, pumps a text string and carriage return to the standard output.
While a vast majority of your
Main() methods will return void as the return value, the ability to
return an
int from Main() keeps C# consistent with other C-based languages. By convention,
returning the value
0 indicates the program has terminated successfully, while another value (such
as
-1) represents an error condition (do be aware that the value 0 is automatically returned, even ifyou construct a Main() method prototyped to return void).
Processing Command-Line Arguments
Now that you better understand the return value of the
Main() method, let’s examine the incoming
array of
string data. Assume that you now wish to update your application to process any possiblecommand-line parameters.One way to do so is using a C# for loop
As an alternative to the standard
for loop, you may iterate over an incoming string array usingthe C# foreach keyword.
Finally, you are also able to access command-line arguments using the static
GetCommandLineArgs()
method of the System.Environment type. The return value of this method is
an array of
strings. The first index identifies the name of the application itself, while the remaining
elements in the array contain the individual command-line arguments (note that when using this
approach, it is no longer necessary to define
Main() as taking a string array as the input parameter,
although there is no harm in doing so):
static int Main(string[] args)
{
...
// Get arguments using System.Environment.
string[] theArgs = Environment.GetCommandLineArgs();
foreach(string arg in theArgs)
Console.WriteLine("Arg: {0}", arg);
Console.ReadLine();
return -1;
}
Some Additional Members of
the System.Environment ClassEnvironment.GetLogicalDrives()
Environment.OSVersion
ExitCode
Gets or sets the exit code anywhere within the application
MachineName
Gets the name of the current machine
NewLine
Gets the newline symbol for the current environment
StackTrace
Gets the current stack trace information for the application
SystemDirectory
Returns the full path to the system directoryUserName Returns the name of the user that started this application
No comments:
Post a Comment