Chapter 2. Getting Started: “Hello World”

It is a time-honored tradition to start a programming book with a “Hello World” program. In this chapter, we create, compile, and run a simple "Hello World” program written in C#. The analysis of this brief program will introduce key features of the C# language.

Example 2-1 illustrates the fundamental elements of a very elementary C# program.

Example 2-1. A simple “Hello World” program in C#

class Hello
    static void Main( )
        // Use the system console object
        System.Console.WriteLine("Hello World");

Compiling and running this code displays the words “Hello World” at the console. Before we compile and run it, let’s first take a closer look at this simple program.

Classes, Objects, and Types

The essence of object-oriented programming is the creation of new types. A type represents a thing. Sometimes the thing is abstract, such as a data table or a thread; sometimes it is more tangible, such as a button in a window. A type defines the thing’s general properties and behaviors.

If your program uses three instances of a button type in a window—say, an OK, a Cancel, and a Help button—each button will have a size, though the specific size of each button may differ. Similarly, all the buttons will have the same behaviors (draw, click), though how they actually implement these behaviors may vary. Thus, the details might differ among the individual buttons, but they are all of the same type.

As in many object-oriented programming languages, in C# a type ...

Get Programming C#, 4th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.