The canonical "Hello, world" program can be written in C# as follows:
using System; class Hello { static void Main() { Console.WriteLine("Hello, world"); } }
The default file extension for C# programs is .cs
, as in hello.cs
. Such a program can be compiled with the command line directive
csc hello.cs
which produces an executable program named hello.exe
. The output of the program is:
Hello, world
Close examination of this program is illuminating:
using System;
directive references a namespace called System
that is provided by the NGWS runtime. This namespace contains the Console
class referred to in the Main
method. Namespaces provide a hierarchical means of organizing the elements of a class library. A "using" directive enables unqualified use of the members of a namespace. The "Hello, world" program uses Console.WriteLine
as a shorthand for System.Console.WriteLine
. What do these identifiers denote? System
is a namespace, Console
is a class defined in that namespace, and WriteLine
is a static method defined on that class.Main
function is a static member of the class Hello
. Functions and variables are not supported at the global level; such elements are always contained within type declarations (e.g., class and struct declarations). For C and C++ developers, it is interesting to note a few things that do not appear in the "Hello, world" program.
::
" or "->
" operators. The "::
" is not an operator in C# at all, and the "->
" operator is used in only a small fraction of C# programs. C# programs use ".
" as a separator in compound names such as Console.WriteLine
.#include
to import program text. Dependencies between programs are handled symbolically rather than with program text. This system eliminates barriers between programs written in different languages. For example, the Console
class could be written in C# or in some other language.