The Canonical Hello World Example


The Canonical "Hello World" Example

I'm fairly certain that by law it is required for every programming book to provide the famous "Hello World" example. So, rather than face potential criminal charges, a walkthrough for creating a Console C# "Hello World" application follows.

Begin by staring Visual Studio .NET and selecting File, New Project. Next, select a C# console application and name it Hello World as shown in Figure 2.3.

Figure 2.3. Creating a .NET console application for Hello World.


Visual Studio .NET will create a default source file, Class1.cs, and provide some basic boilerplate code as shown in Listing 2.4.

Listing 2.4. Generated Boilerplate Code for a C# Console Application
    using System;    namespace Hello_World    {        /// <summary>        /// Hello World example        /// </summary>        class Class1        {            /// <summary>            /// The main entry point for the application.            /// </summary>            [STAThread]            static void Main(string[] args)            {              //                // TODO: Add code to start application here                //            }        }    } 

Replace lines 16 though 18 with the following line of code:

 Console.WriteLine( "Hello World" ); 

The Main method should now look like Listing 2.5.

Listing 2.5. Updated Main Method Replacing Comments with Code
 1: static void Main(string[] args) 2: { 3:     Console.WriteLine( "Hello World" ); 4: } 

The Console.WriteLine method will print the string Hello World to a console window. Figure 2.3 shows the results from executing the code. To execute the code, select Debug, Start Without Debugging or simply press Ctrl+F5 to launch the application.



    Visual C#. NET 2003 Unleashed
    Visual C#. NET 2003 Unleashed
    ISBN: 672326760
    EAN: N/A
    Year: 2003
    Pages: 316

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