Home > Articles > Programming > C#

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Your First C# Console Application

To build a console application using C#, start the Visual Studio. Select the File | New | Project sequence to open the New Project dialog box, as shown in Figure 1–1.

Figure 1Figure 1–1 The New Project dialog box allows us to specify a C# console application.

Name this project HelloWorld, and specify a subdirectory under the root directory, as shown in Figure 1–1.

When you click the OK button, the C# AppWizard creates the template code shown in Figure 1–2.

Figure 2Figure 1–2 The AppWizard's C# template code for a console application.

This template code can now be modified to suite your purposes. Figure 1–3 shows how we altered the template code for our HelloWorld project.

Examine Figure 1–3 and compare it with the following complete listing. Note the addition of just one line of code:

using System;

namespace HelloWorld
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class Class1
    {
        static void Main(string[] args)
        {
            //
            // TODO: Add code to start application here
            //
            Console.WriteLine("Hello C# World!");
        }
    }
}

Examine Figure 1–3, once again. Notice that the Build menu has been opened and the Rebuild option is about to be selected. Clicking this menu item will build the project.

Figure 3Figure 1–3 The template code is altered for the HelloWorld project.

When you examine this simple portion of code, you notice many of the elements that you are already familiar with from writing C or C++ console applications. Figure 1–4 shows the Debug menu opened and the Start Without Debugging option about to be selected. Make this selection to run the application within the integrated environment of the Visual Studio.

Figure 4Figure 1–4 Running the program from within Visual Studio.

When the program is executed, a console (command-line or DOS) window will appear with the programs output. Figure 1–5 shows the output for this application.

Now, let's briefly examine the familiar elements and the new additions. First, the application uses the System directive. The System namespace, provided by the NGWS at runtime, permits access to the Console class used in the Main method. The use of Console.WriteLine() is actually an abbreviated form of System.Console.WriteLine() where System represents the namespace, Console a class defined within the namespace, and WriteLine() is a static method defined within the Console class.

Figure 5Figure 1–5 The console window shows the project's output.

Additional Program Details

In C# programs, functions and variables are always contained within class and structure definitions and are never global.

You will probably notice the use of "." as a separator in compound names. C# uses this separator is place of "::" and "->". Also, C# does not need forward declarations because the order in not important. The lack of #include statements is an indicator that the C# language handles dependencies symbolically. Another feature of C# is automatic memory management, which frees developers from dealing with this complicated problem.

  • + Share This
  • 🔖 Save To Your Account