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

Get C# Primer: A Practical Approach 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.