O'Reilly logo

C# Primer: A Practical Approach by Stanley B. Lippman

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

1.1. A First C# Program

The traditional first program in a new language is one that prints Hello, World! on the user's console. In C# this program is implemented as follows:

// our first C# program
using System;
class Hello
{
      public static void Main()
      {
             Console.WriteLine( "Hello, World!" );
      }
}

When compiled and executed, this code generates the canonical

Hello, World!

Our program consists of four elements: (1) a comment, introduced by the double slash (//), (2) a using directive, (3) a class definition, and (4) a class member function (alternatively called a class method) named Main().

A C# program begins execution in the class member function Main(). This is called the program entry point. Main() must be defined as static. In our example, ...

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