2. A First Program

Recall that a program is simply a list of instructions, called statements, for the computer to follow. Take a look at a simple C# program.

Example 2.1. HelloWorld.cs

class HelloWorld { 

  static void Main() { 
    System.Console.WriteLine("Hello World!"); 
  } 

}  

This program may look anything but simple, but don't panic! There is really only one statement in the program:

System.Console.WriteLine("Hello World!");

When you run the program, this command displays the text inside the quotes on the screen.

What about the other stuff? Everything else in the program is simply related to program organization. You see, C# is designed for the creation of large, sophisticated programs, with thousands of lines of code. To keep the code manageable to work with, C# requires programmers to group statements into units called methods. These methods, in turn, must be grouped into units called classes. A method is simply a named group of statements; a class is a named group of methods.

To get a grasp on the relationship between statements, methods, and classes, think about this book. This sentence is in a chapter, which itself is in a book. Just as sentences are grouped into chapters, which are grouped into books, statements are grouped into methods, which are grouped into classes.

When you create a program, you can't start typing statements. The statements have to be organized into methods, which are grouped in classes. It's bothersome to have to do this for simple programs, but C#'s rules of syntax require it. I'm going to try to give just a brief explanation here to help you understand what is going on, deferring the details to later chapters.

To create a C# program, you begin by creating a class like this:

class HelloWorld {

}

The word "class" tells the C# compiler that a new class is being defined. The name "HelloWorld" is something the programmer makes up -- it's whatever he wants to call the program. (We'll talk more about names later.) The curly braces { } denote the beginning and ending of the class definition; in between the braces come the methods that belong to the class.

Next, the programmer creates a main method inside the class, like this:

class HelloWorld {
  static void Main() { 
     
  } 
}

To summarize, the words "static void Main()" tell the compiler that the "Main" method is being defined. What is the "Main" method, you ask? A program might have more than one method in it, each with its own list of statements. We have to have a way to tell the computer which one it should execute first. When you run the program, the computer looks for the magic words "static void Main()" and executes the statements it finds inside the curly braces. Eventually you will understand what those magic words mean; for now, you'll have to take my word for it that they are required.

So, the bad news is that to create even simple C# programs, you have to type a lot of "wrapping" material. The good news is that every program has exactly the same basic layout. So for now I suggest that when you look at the examples in the rest of the chapter, ignore the program "wrapping," and focus your attention on what happens inside the main method. That is the "meat" of the program.