Sample C# Program: HelloWorld
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
This sample C# program is the obligatory HelloWorld, a simple program which merely types "Hello, World!" on the console. (Note: This is probably the first and only grammatically correct HelloWorld program on the planet.)
public class HelloWorld { public static void Main (string[] args) { System.Console.WriteLine ("Hello, World!"); } }
The first line declares a public class named HelloWorld. The next line declares the Main entry point for the class: this is where execution will begin. The last line uses a System method named WriteLine to write the "Hello, World!" to the Console. What could be simpler!
Contents |
Java version of HelloWorld program
Here is the same program written in the Java language:
public class HelloWorld { public static void main (String args[]) { System.out.println ("Hello, World!"); } }
As you can see, the differences between the two languages are slight.
Compile the HelloWorld program
To compile the program, enter the C# source code into a file named HelloWorld.cs. Then, type the following into a Command Prompt window: csc Helloworld.cs. The C# compiler takes the C# source code file (HelloWorld.cs) and compiles it into a Microsoft Intermediate Language (MSIL) file named HelloWorld.exe. To execute the progam, simply type: HelloWorld.
Disassembled HelloWorld MSIL
The MSIL file (HelloWorld.exe) created by the C# compiler can be displayed by the Intermediate Language Assembler (ilasm) like this:
.method public hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 13 (0xd)
.maxstack 8
IL_0000: nop
IL_0001: ldstr "Hello, World!"
IL_0006: call void [mscorlib]System.Console::WriteLine(string)
IL_000b: nop
IL_000c: ret
} // end of method HelloWorld::Main
When compiling a C# source file to managed code, the C# compiler translates your source code into Microsoft Intermediate Language (MSIL) or intermediate language. MSIL is a CPU-independent set of instructions that can be efficiently converted to the native machine code of a specific hardware platform such as an x86 machine. MSIL includes instructions to load, store, initialize, and call object methods, as well as instructions for arithmetic and logical operations, control flow, direct memory access, exception handling, and other operations.
Before the intermediate code can be executed, the intermediate language must be converted to run on a particlular CPU. Typically, the conversion is done by a Just-In-Time (JIT) compiler—so called because it waits until a piece of intermediate code is actually needed before converting it. Because the Common Language Runtime (CLR) supplies a JIT compiler for every hardware platform it supports, the same set of MSIL can be JIT-compiled and run on any supported hardware.
Metadata for HelloWorld MSIL
The C# compiler produces metadata along with the MSIL. Metadata describes the types in your program, including the type definitions, the member signatures, the members referenced by your code, and other data needed by the runtime system at execution time. A Portable Executable (PE) file comprises the metadata and MSIL. A PE file extends the Microsoft PE and common object file format (COFF) historically used for executables. The operating system recognizes this file format as a Common Language Runtime (CLR) image. There is no need for type libraries or Interface Definition Language (IDL), because the metadata and the MSIL describes itself. The CLR finds and extracts the metadata from the file as needed during execution.
The metadata for HelloWorld looks like this:
// Metadata version: v2.0.50727
.assembly extern mscorlib
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 2:0:0:0
}
.assembly HelloWorld
{
.custom instance void [mscorlib]
System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor
(int32) = ( 01 00 08 00 00 00 00 00 )
.custom instance void [mscorlib]
System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor()
= ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..WrapNonEx
63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 ) // ceptionThrows.
.hash algorithm 0x00008004
.ver 0:0:0:0
}
.module HelloWorld.exe
// MVID: {EC8BE6B2-BE42-4AC6-BE50-0C481B249DC6}
.imagebase 0x00400000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000001 // ILONLY
// Image base: 0x00A50000