Welcome to Pete Brown's 10rem.net

First time here? If you are a developer or are interested in Microsoft tools and technology, please consider subscribing to the latest posts.

You may also be interested in my blog archives, the articles section, or some of my lab projects such as the C64 emulator written in Silverlight.

(hide this)

Shortest .NET program I have ever written (or smallest solution anyway)

Pete Brown - 02 March 2010

I was looking for a screenshot of an old app I wrote, and I ran across this code from 2001. This is the shortest .NET program I ever wrote.

Intermediate Language (IL) was new to us back then, so many of us tried writing simple programs in it just to try it out. The whole solution consists of three files: the resulting EXE, the source IL and a batch file to do the compilation:

image

The code itself targets .NET 1.0 Beta 2. Here's the source .IL file. There are more comments than code, but luckily that means it should still be pretty easy to read.

//
// Hello World IL Program
// ------------------------------------
// Written by Peter M. Brown
// August 15, 2001
//
// This is a minimal IL example.  There are other options/attributes
// that can be used (and in many cases, should be used) but if it didn't
// prevent it from compiling, I bagged it :-)
//
// Requires Beta 2 version of ILASM and framework (1.0.2914)
//
// To assemble : ilasm HelloWorld.il
//

.assembly extern mscorlib
{}

.assembly HelloWorld
{}

.class public ILHelloWorld
{
  // This is our main function the .entrypoint attribute is 
  // what defines the "Mainness" of Main.  If you leave this
  // attribute off, you will get an IL assembler error
  //
  // Entry point must be a static function

  .method public static void Main()
  {
    .entrypoint

    // print out a blank line
    IL_0000:  call    void [mscorlib]System.Console::WriteLine()

    // load our "Hello World" string
    IL_0005:  ldstr    "Hello World!  This is an IL program by Pete."

    // print the string to the console using Console.Writeline
    IL_0010:  call    void [mscorlib]System.Console::WriteLine(string)
    
    // return from the function
    IL_0015:  ret    // leave this out, and you'll get an exception

  }
}

and here's the batch file. Again, more comments than code.

echo Make sure you are running version 1.0.2914 (beta 2 or later) 
echo not 1.0.2204 of ilasm.  This fails with beta 1.
echo .
ilasm HelloWorld.il

You could write all your .NET apps this way if you really wanted. IL is the assembler for .NET apps, the abstraction layer between the languages and the runtime.

 
posted by Pete Brown on Tuesday, March 2, 2010
filed under:  

Comment on this Post

Remember me