Your First Program: Hello World

At the most fundamental level, a C# application consists of source code. Source code is human-readable text written in a text editor. A text editor is like a word processor, but it puts no special characters into the file to support formatting, only the text. You could use any old text editor to write your code, but since you’ll be using Visual Studio throughout this book, that’s the best choice. Start up C# Express or Visual Studio. The first thing you’ll see is the Start Page, which will look similar to Figure 1-2.

The Start Page for Visual C# 2008 Express. It looks pretty empty now, but that won’t last long. You’ll be using the Create link on the lefthand side.

Figure 1-2. The Start Page for Visual C# 2008 Express. It looks pretty empty now, but that won’t last long. You’ll be using the Create link on the lefthand side.

There’s a lot of news in the middle, which you don’t need to pay attention to right now. We’ll give you a full tour of the Visual Studio interface in Chapter 2, but for now you need the Recent Projects box on the left. If you just installed Visual Studio, that box is empty at the moment, because you haven’t created any projects yet. That’s about to change. Click Project, next to the Create link. The New Project dialog box opens, as you can see in Figure 1-3.

There are lots of options here that we’ll discuss later, but for now we just want to get you started. Select Console Application from the row of templates at the top. When you do that, the content of the Name field at the bottom will change to ConsoleApplication1, which is a fine name, but not very descriptive. Change it to HelloWorld (without a space) and then click OK.

This is where you’ll create all your Visual Studio projects. There are a lot of template options here, but for now just select Console Application and type HelloWorld in the Name field.

Figure 1-3. This is where you’ll create all your Visual Studio projects. There are a lot of template options here, but for now just select Console Application and type HelloWorld in the Name field.

Visual Studio creates the project for you, along with the necessary files. Again, you don’t need to know about most of this yet, but it’s nice that Visual Studio does it for you. It also creates the program where you’ll write your code, called Program.cs. Finally, Visual Studio opens Program.cs in an editing window for you to work on. Visual Studio provides some basic code that’s common to all C# console programs, called a skeleton, which saves you even more time. Your Visual Studio screen should now look like Figure 1-4.

In this first example, you will create a very simple application that does nothing more than display the words Hello World to your monitor. This console application is the traditional first program for learning any new language, and it demonstrates some of the basic elements of a C# program.

After you write your Hello World program and compile it, we’ll provide a line-by-line analysis of the source code. This analysis will give you a brief preview of the language; we’ll describe the fundamentals much more fully in Chapter 3.

As we mentioned, the skeleton of the program is already there for you, but you still need to write a little code. The editing window you’re looking at now works much like any word processing program you’re familiar with, or even like Windows Notepad. However, you’ll find that Visual Studio has a lot of helpful features for writing code that those other applications lack. Right now, click after the open brace ({) underneath static void Main. Press Enter once to open up some space (notice that Visual Studio indents for you automatically—this is a good thing), and then type the following:

Visual Studio does all the work of setting up your application automatically, which saves a lot of time. It even creates this program skeleton for you, ready for you to add your own code.

Figure 1-4. Visual Studio does all the work of setting up your application automatically, which saves a lot of time. It even creates this program skeleton for you, ready for you to add your own code.

// every console app starts with Main
System.Console.WriteLine("Hello World!");

As you type, you’ll notice that Visual Studio automatically colors your code, and that it’ll open small windows (called IntelliSense windows) suggesting code that you might want to include. Don’t worry about any of that for now; just type the code as shown here.

Example 1-1 shows the code that you should see in your editing window right now. The lines that you added are shown here in bold. Be sure to pay attention to the capitalization, especially capitals where you wouldn’t normally expect them, as in WriteLine. C# is case-sensitive, and if you lowercase the L here, you’ll get an error message (and not necessarily a helpful error message).

Example 1-1. A simple source code file; this application doesn’t look like much, but it’s a fully functional application that you’ll run in just a moment

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HelloWorld
{
   class Program
   {
      static void Main(string[] args)
      {
         // every console app starts with Main
         System.Console.WriteLine("Hello World!");
      }
   }
}

You should save your code before you go any further. Click the Save All button () on the toolbar. You’ll see a dialog box asking you where you want to save your work; the My Documents/Visual Studio 2008/Projects folder is the default, but you can save your work wherever you like. Each project you create will have its own subfolder.

We’ll explain this program detail in a bit. For now, just look at the language—the program is readable; it is in normal text. The words may be strange and the layout unusual, but there are no special characters—just the normal text produced by your keyboard.

The source code makes up a set of instructions for the application to follow. The syntax for these instructions is strictly defined by the language. In C#, source code consists of a series of statements. A statement is an instruction to the compiler. Each instruction must be formed correctly, and one task you’ll face when learning C# will be to learn the correct syntax of the language. For example, in C#, every statement ends with a semicolon.

Each instruction has a semantic meaning that expresses what you are trying to accomplish. Although you must follow the rules of C# syntax, the semantics of the language are far more important in developing effective object-oriented programs. This book will provide insight into both the syntax and the semantics of good C# programs.

We know you’ll want to run your new program right away, but bear with us for just a moment while we explain just what Visual Studio has to do to make that happen.

Get Learning C# 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.