Export (0) Print
Expand All

Lab 1: Hello World

Visual Studio .NET 2003
 

Visual Studio Team
Microsoft Corporation

August 2001

Summary: In this hands-on lab, you will create the Hello World application in C# using the Microsoft Visual Studio .NET Integrated Development Environment (IDE) to create, compile, and run the lab. (7 printed pages)

Download the Experience Visual Studio .NET Lab files from the introduction topic.

Contents

Introduction
Creating a New Project in the IDE
Creating the Hello.cs Source File
Building the Executable File
Running the Program
Closing Out of Lab 1

Introduction

In this hands-on lab, you will create the Hello World application in C# using the Microsoft Visual Studio .NET Integrated Development Environment (IDE) to create, compile, and run the lab.

Creating a New Project in the IDE

  1. To open the IDE, click Start, click Programs, click Microsoft Visual Studio .NET, and then click Microsoft Visual Studio .NET. The IDE Start page appears as in Figure 1.

    Figure 1. Start page in the Visual Studio .NET IDE

    Note   The Start page opens by default when you start the IDE. It provides links to the last four projects that you have worked on within the environment, and also provides additional resource links. You can change all of the default settings by clicking Tools and then clicking Options to choose your preferences.
  2. Click File, click New, and then click Project. The New Project window appears as in Figure 2.

    The default location is C:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\.

    Figure 2. New Project window

  3. In the Project Types pane, click Visual C# Projects.
  4. In the Templates pane, click Empty Project.
  5. In the Name field, type hello and then choose a location for your project.
  6. Click OK.

Creating the Hello.cs Source File

The Hello.cs file is the source file used in this lab. You will create the file within the Hello project.

  1. Click File, and then click Add New Item. The Add New Item window appears as in Figure 3.
  2. In the Categories pane, click Local Project Items.
  3. In the Templates pane, click Code File.
  4. Type hello.cs in the name field, and then click Open.

    Figure 3. Add New Item window

  5. Type the following code into the page.
    using System;
    public class HelloWorld {
           static void Main() {
                  Console.WriteLine("Hello World!");
           }
    }
    
  6. Click File, and then click Save hello.cs (or click the Save icon). The file is saved to your project folder.

    Figure 4. The Hello.cs source file

    Note   In C# (unlike C or C++), there are no global methods. All methods belong to a class.
    The .NET Framework requires that all method names begin with a capital letter. So, a method named Main() would work in the .NET Framework while a method named main() would not.
    The using System; line tells the compiler that all classes and types from the System namespace are available to this program. The system namespace includes basic types, such as int and string.
    The public class HelloWorld line declares a class named HelloWorld. This means that the HelloWorld class can be accessed by any other class.
    The static void Main() line is the program's entry point. The entry point is the point where the program, or executable file, starts. This line is required at the beginning of every executable file within a program.
    The Console.WriteLine("Hello World!"); line uses the WriteLine() method of the Console class within the system namespace. The parameter Hello World! is passed to Console.WriteLine(), and "Hello World!" is displayed on the screen when the program is executed.
    Console.WriteLine() can be used instead of System.Console.WriteLine() because the System namespace is declared at the top of the file.

Building the Executable File

To build the executable file from within the IDE, on the Build menu, click Build (or click the Build icon).

Note   The IDE builds the executable file hello.exe and places it in the C:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\hello\bin\Debug folder.
During the build, the Output window appears in the IDE as in Figure 5. If any errors occur during the build, they will be reported in this window.

Figure 5. Output window within the IDE

Running the Program

You can now run the program from within the IDE.

  1. Select Debug > Start Without Debugging. Hello World! is printed to a Microsoft MS-DOS® window (Figure 6).
    Note   Because this program is a very small executable file that prints text to an MS-DOS screen, you will run it with debugging turned off. This allows you to see the results. Keep in mind that the "Press any key to continue" line after "Hello World!" is not part of the program.
    The first time an executable file is run in the .NET framework, it is compiled to native code. This may cause a delay in displaying the results. The executable file runs faster the next time it is executed. No object (.obj) files are created as a result of invoking the C# compiler; output files are created directly. Consequently, the C# compiler does not need a linker.

    Figure 6. Hello World! Program results

  2. Close the MS-DOS window.

Closing Out of Lab 1

To close the Hello World! Program, click File, and then click Exit.

Other articles and labs in the Experience Visual Studio .NET set include:

Introducing the Visual Studio .NET Lab Series

Lab 2: XML Comments

Lab 3: ASP vs. ASP.NET

Lab 4: Server Controls Walkthrough

Lab 5: Using the Visual Basic Upgrade Wizard

Lab 6: Building a Browser-Based Application

Lab 7: Inheritance and Override

Show:
© 2014 Microsoft