Building a Program with C#

One of your first tasks in learning C# is to create a C# program. Listing A.1 shows a simple C# program that simply prints a message to the Console.

Listing A.1. Simple C# Program
using System;
namespace Hello
{
    class HelloMain
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello!");
        }
    }
}

First, notice that a namepace encloses the entire program. This is strictly not required. If the namespace is taken out, leaving the class HelloMain, the program would still compile and run. However, it is a good idea to get in the habit of enclosing your programs with a namespace. This prevents conflicts with other modules and libraries.

Classes can only exist directly in a namespace. You cannot put methods or fields directly ...

Get .NET Common Language Runtime Unleashed 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.