Hello World (C Sharp)

From LiteratePrograms

Jump to: navigation, search
Other implementations: Ada | ALGOL 68 | Alice ML | Amiga E | Applescript | AspectJ | Assembly Intel x86 Linux | Assembly Intel x86 NetBSD | AWK | bash | BASIC | Batch files | C | C, Cairo | C, Xlib | Candle | Clojure | C++ | C# | Delphi | Dylan | E | Eiffel | Erlang | Forth | FORTRAN | Fortress | Go | Groovy | Haskell | Hume | IBM PC bootstrap | Inform 7 | Java | Java, Swing | JavaScript | LaTeX | Lisp | Logo | Lua | Maple | MATLAB | Mercury | OCaml/F Sharp | occam | Oz | Pascal | Perl | PHP | Pic | PIR | PLI | PostScript | Prolog | Python | Rexx | Ruby | Scala | Scheme | Seed7 | sh | Smalltalk | SQL | Standard ML | SVG | Tcl | Tcl Tk | Visual Basic | Visual Basic .NET | XSL

This short C# program prints a message to the console.

<<HelloWorld.cs>>=
class HelloWorld
{
    static void Main()
    {
        System.Console.WriteLine("Hello world!");
    }
}

All code in C# must be placed in a class - here we arbitrarily call our class HelloWorld. Execution begins at the beginning of the function Main(). It executes the static method WriteLine() of the type Console in the namespace System, a standard .NET Framework method which prints a string to the console. The string to print is given as an argument, and ends in \n which indicates a newline or carriage return.

The method Main() is static because static methods do not require an object to be called and no objects have yet been created when the program first begins.

Download code
Views
Personal tools