The canonical "hello, world" program can be written as follows:
using System;
class Hello
{
static void Main() {
Console.WriteLine("hello, world");
}
}
The source code for a C# program is typically stored in one or more text files with a file extension of
.cs, as in hello.cs. Using the command-line compiler provided with Visual Studio .NET, such a program can be compiled with the command-line directivecsc hello.cs
which produces an application named
hello.exe. The output produced by this application when it is run is:hello, world
Close examination of this program is illuminating:
- The
using System;directive references a namespace calledSystemthat is provided by the Microsoft .NET Framework class library. This namespace contains theConsoleclass referred to in theMainmethod. Namespaces provide a hierarchical means of organizing the elements of one or more programs. A "using" directive enables unqualified use of the types that are members of the namespace. The "hello, world" program usesConsole.WriteLineas shorthand forSystem.Console.WriteLine. - The
Mainmethod is a member of the classHello. It has thestaticmodifier, and so it is a method on the classHellorather than on instances of this class. - The entry point for an application — the method that is called to begin execution — is always a static method named
Main. - The "hello, world" output is produced using a class library. The language does not itself provide a class library. Instead, it uses a class library that is also used by Visual Basic .NET and Visual C++ .NET.
No comments:
Post a Comment