The Main() Method


You saw at the start of this chapter that C# programs start execution at a method named Main(). As you saw earlier, this must be a static method of a class (or struct), and must have a return type of either int or void.

Although it is common to specify the public modifier explicitly, because by definition the method must be called from outside the program, it doesn't actually matter what accessibility level you assign to the entry point method — it will run even if you mark the method as private.

Multiple Main() Methods

When a C# console or Windows application is compiled, by default the compiler looks for exactly one Main() method in any class matching the signature that was just described and makes that class method the entry point for the program. If there is more than one Main() method, the compiler will return an error message. For example, consider the following code called MainExample.cs:

 using System; namespace Wrox.ProCSharp.Basics { class Client { public static int Main() { MathExample.Main(); return 0; } } class MathExample { static int Add(int x, int y) { return x + y; } public static int Main() { int i = Add(5,10); Console.WriteLine(i); return 0; } } } 

This contains two classes, both of which have a Main() method. If you try to compile this code in the usual way, you will get the following errors:

csc MainExample.cs

Microsoft (R) Visual C# .NET Compiler version 8.00.40607.16 for Microsoft (R) Windows (R) .NET Framework version 2.0.40607 Copyright (C) Microsoft Corporation 2001-2003. All rights reserved. MainExample.cs(7,23): error CS0017: Program 'MainExample.exe' one entry point defined: 'Wrox.ProCSharp.Basics.Client.Main()' MainExample.cs(21,23): error CS0017: Program 'MainExample.exe' one entry point defined: 'Wrox.ProCSharp.Basics.MathExample.Main()'

However, you can explicitly tell the compiler which of these methods to use as the entry point for the program using the /main switch, together with the full name (including namespace) of the class to which the Main() method belongs:

csc MainExample.cs /main:Wrox.ProCSharp.Basics.MathExample

Passing Arguments to Main()

The examples so far have only shown the Main() method without any parameters. However, when the program is invoked, you can get the CLR to pass any command-line arguments to the program by including a parameter. This parameter is a string array, traditionally called args (although C# will accept any name). The program can use this array to access any options passed through the command line when the program is started.

The following sample, ArgsExample.cs, loops through the string array passed in to the Main() method and writes the value of each option to the console window:

 using System; namespace Wrox.ProCSharp.Basics { class ArgsExample { public static int Main(string[] args) { for (int i = 0; i < args.Length; i++) { Console.WriteLine(args[i]); } return 0; } } } 

You can compile this as usual using the command line. When you run the compiled executable, you can pass in arguments after the name of the program, for example:

ArgsExample /a /b /c

/a /b /c

More on Compiling C# Files

So far, you have seen how to compile console applications using csc.exe, but what about other types of applications? What if you want to reference a class library? The full set of compilation options for the C# compiler is of course detailed in the MSDN documentation, but we list here the most important options.

To answer the first question, you can specify what type of file you want to create using the /target switch, often abbreviated to /t. This can be one of the following:

Option

Output

/t:exe

A console application (the default)

/t:library

A class library with a manifest

/t:module

A component without a manifest

/t:winexe

A Windows application (without a console window)

If you want a non-executable file (such as a DLL) to be loadable by the .NET runtime, you must compile it as a library. If you compile a C# file as a module, no assembly will be created. Although modules cannot be loaded by the runtime, they can be compiled into another manifest using the /addmodule switch.

Another option we need to mention is /out. This allows you to specify the name of the output file produced by the compiler. If the /out option isn't specified, the compiler will base the name of the output file on the name of the input C# file, adding an extension according to the target type (for example, exe for a Windows or console application, or dll for a class library). Note that the /out and /t, or /target, options must precede the name of the file you want to compile.

If you want to reference types in assemblies that aren't referenced by default, you can use the /reference or /r switch, together with the path and file name of the assembly. The following example demonstrates how you can compile a class library and then reference that library in another assembly. It consists oftwo files:

  • The class library

  • A console application, which will call a class in the library

The first file is called MathLibrary.cs and contains the code for your DLL. To keep things simple, it contains just one (public) class, MathLib, with a single method that adds two ints:

 namespace Wrox.ProCSharp.Basics { public class MathLib { public int Add(int x, int y) { return x + y; } } } 

You can compile this C# file into a .NET DLL using the following command:

csc /t:library MathLibrary.cs

The console application, MathClient.cs, will simply instantiate this object and call its Add() method, displaying the result in the console window:

 using System; namespace Wrox.ProCSharp.Basics { class Client { public static void Main() { MathLib mathObj = new MathLib(); Console.WriteLine(mathObj.Add(7,8)); } } } 

You can compile this code using the /r switch to point at or reference the newly compiled DLL:

csc MathClient.cs /r:MathLibrary.dll

You can then run it as normal just by entering MathClient at the command prompt. This displays the number 15 — the result of your addition.




Professional C# 2005
Pro Visual C++ 2005 for C# Developers
ISBN: 1590596080
EAN: 2147483647
Year: 2005
Pages: 351
Authors: Dean C. Wills

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net