1.1 Create a Console Application


Problem

You need to build an application that doesn't require a Windows graphical user interface (GUI) but instead displays output to, or reads input from, the Windows command prompt (console).

Solution

Ensure you implement a static method named Main with one of the following signatures in at least one of your source code files.

 public static void Main(); public static void Main(string[] args); public static int Main(); public static int Main(string[] args); 

Use the /target:exe switch on the C# compiler (csc.exe) when you compile your assembly.

Discussion

By default, the C# compiler will build a Console application unless you specify otherwise . For this reason, it's not necessary to specify the /target:exe switch, but doing so makes your intention clearer, which is useful if you are creating build scripts that will be used by others or will be used repeatedly over a period of time. The following example lists a class named ConsoleUtils that is defined in a file named ConsoleUtils.cs:

 using System; public class ConsoleUtils {          // A method to display a prompt and read a response from the console     public static string ReadString(string msg) {                  Console.Write(msg);         return System.Console.ReadLine();     }          // A method to display a message to the console     public static void WriteString(string msg) {                  System.Console.WriteLine(msg);     }          // Main method used for testing ConsoleUtility methods     public static void Main() {         // Prompt the reader to enter their name         string name = ReadString("Please enter your name : ");                  // Welcome the reader to the C# Cookbook         WriteString("Welcome to the C# Programmer's Cookbook, " + name);     } } 

To build the ConsoleUtils class into a Console application named ConsoleUtils.exe, use the command csc /target:exe ConsoleUtils.cs . You can run the resulting executable assembly directly from the command line. When run, the Main method of the ConsoleUtils.exe application prompts you for your name and then welcomes you to the C# Programmer's Cookbook , as shown here.

 Please enter your name : Rupert Welcome to the C# Programmer's Cookbook, Rupert 

In reality, applications rarely consist of a single source file. As an example, the HelloWorld class listed here uses the ConsoleUtils class to display the message "Hello, world" to the console. ( HelloWorld is contained in the HelloWorld.cs file.)

 public class HelloWorld {     public static void Main() {              ConsoleUtils.WriteString("Hello, world");     } } 

To build a Console application consisting of more than one source code file, you must specify all the source files as arguments to the compiler. For example, the following command builds an application named MyFirstApp.exe from the HelloWorld.cs and ConsoleUtils.cs source files.

 csc /target:exe /main:HelloWorld  /out:MyFirstApp.exe HelloWorld.cs ConsoleUtils.cs 

The /out switch allows you to specify the name of the compiled assembly. Otherwise, the assembly is named after the first source file listedHelloWorld.cs in the example. Because both the HelloWorld and ConsoleUtils classes contain Main methods, the compiler can't automatically determine which method represents the correct entry point for the assembly. You must use the compiler's /main switch to identify the name of the class that contains the correct entry point for your application.




C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

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