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, you'll 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 simple C# program.

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

  class Hello
    {
        static void Main(string[] args)
        {
            // Use the system console object
            System.Console.WriteLine("Hello World!");
        }
    }

Compiling and running this code displays the words "Hello World!" at the console. Before you compile and run it, let's first take a closer look at this simplest of programs.

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 ...

Get Programming C# 3.0, 5th Edition 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.