O'Reilly logo

Programming C# 3.0, 5th Edition by Donald Xie, Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required