HelloMono - Mono on Windows XP

Introduction

If you think that you have to have Visual Studio .NET in order to develop in .NET, you are in for a pleasant surprise. Mono - in Spanish the word means monkey, in geek-speak it means choice. Either way, it is most assuredly a good thing. Mono is an open source, free implementation of the .NET Framework. On May 4, 2004, the Mono project released the first public beta of their work.

A few days after I completed my previous article - hello, world - a primitive view of the state of the art, the Mono Beta 1 was released, and I wanted so badly to include a HelloMono example in my article. The only problem was that I absolutely couldn't get Mono to work on my system. Now, I am about as tech savvy as they come. I run Windows XP, Windows 2000, Solaris 10 (Software Express), Libranet, FreeBSD, and Whitebox (basically Redhat Enterprise), and occasionally a half dozen other OSes, all on the same box. When in Windows, I have Cygwin with XFree86 available and in constant use. I thought Mono would be a breeze to setup, after all it has a windows installer. Not so, I am afraid. However, all hope is not lost - it is a few days later and I have figured out how to make Mono purr, and it runs fine. Thanks to the open source community, I was able to add a bug report to the project and while the bug is still open, some kind soul posted a reasonable workaround that got me up and running.

First, here is what got me started on the whole hello, world kick again after all of these years. Kernighan and Ritchie's ubiquitous "The C Programming Language" is considered by many to be the most influential programming book of our time. Written in 1978, it immortalized the now well known phrase - "hello, world". The book states that "hello, world" is the basic hurdle to learning all languages: "to leap over it you have to be able to create the program text somewhere, compile it successfully, load it, run it, and find out where your output went. With these mechanical details mastered, everything else is comparatively easy."

You need to have some perspective to truly appreciate this statement or you might be tempted to consider it overreaching. Dennis Ritchie designed the C programming language and it was the C language that made portability possible for the Unix operating system. Unix and Linux, Minix, BSD, OSX, YouNameItOS and even Windows owe a great debt of gratitude to the language. Even the language C# is a derivative of Ritchie's C. So when in his book, it says that "hello, world" is important, we should listen.

So it is that I have taken my inspiration from K&R's work. I can think of no better method to expose the foundations of the development environment even in the year 2004 than the old, "hello, world" approach.

This article is a "hello, world" tutorial for Mono running on Windows XP:

Background

A basic understanding of .NET, the command-line, and working in Windows is assumed. Here are some good resources to help you get up to speed on these topics:

Well, ok, it is not a requirement, but it will sure make things easier. Get it at http://ultraedit.com/. If you do not have a decent text editor, Notepad will work ok.

You should now be set up to develop with the Mono on the command-line.

HelloMono

This is a very simple tutorial and does not pretend to exercise Mono to its fullest.

OK, let's get started. What are the goals? Borrowing directly from Kernighan and Ritchie, these will be our goals for this tutorial:

Create the program text

Compile the program

Load the program

Run the program

Locate the Output

Create the program text

In order to create the program text, you will need to use a text editor of some sort. I prefer using UltraEdit, although Textpad or Notepad will work as well. Do not even bother with Word or Wordpad, these are arguably word processors and they tend to do funny things to text files. Fire up the editor and you will be ready to begin.

Here is K&R's "hello, world" C program in all its splendor, it is the model I am replicating, so I feel it is fitting to include it here:

// reader Ryan Beesley noticed that the original quote lacked an include <BR>// needed to compile so I added the following include - it is not part <BR>// of the original K&R hello, world program
#include "stdio.h"
main()
{
printf("hello, world\n");
}

The C#.NET code that is required to print "hello, world" to the console is similarly simple:

Line 1 This is a using directive that tells the compiler that we will be referencing objects that are included in the Systemnamespace. According to Microsoft, "The System namespace contains fundamental classes and base classes that define commonly-used value and reference data types, events and event handlers, interfaces, attributes, and processing exceptions." For our purposes, the Systemnamespace provides the handy, dandy Console object that will allow us to print "hello, world" to the console.

Line 2 This is an empty line White-space, space, tab, carriage return, etc. in code (not strings) is generally considered insignificant, I tend to think of white-space as condensing to a single space or blank line.

Line 3 This is a namespace directive that tells the compiler to segregate our code in its own namespace semantically. This is not really required here, but it is good form. If you put any classes that you create in your own namespace, you will not be bitten by namespace collisions down the road. A namespace collision is what happens when there exist two semantic elements that have the same name. For instance, let's say that I create a class called Car and somewhere else within the same project someone else also creates a Car class. If I include both classes in my project, their names will collide unless they are in their own namespaces.

Line 4 This is the class declaration, our class is publicly accessible and is called HelloWorld

Line 5 The Main method. Console applications are required to have a Main method. The Main method is the method that will be executed by the operating system when the program is loaded and run. Main is public and static, it does not return anything and it takes an array of strings as arguments. static basically means that the method can be called without instantiating a HelloWorld object. The arguments are include by convention, we will not be passing in any arguments.

Line 6 "hello, world" gets printed by this call to the Console object's WriteLine method which writes the argument to the console followed by a carriage return.

Line 7 End of the Main method.

Line 8 End of the HelloWorldclass definition

Line 9 End of the namespace

Save the file to your hard drive as HelloMono.cs and open a command prompt.

Compile the program

Compile the application using the Mono C# compiler:

mcs HelloMono.cs

This should produce output similar to the following, without error.

Compilation succeeded

Compiling the application will generate an executable named HelloMono.exe

Load and run the program

Loading and running the program are accomplished in a single step.

Load and run the application from the command-line by typing:

mono HelloMono.exe

Locate the Output

Console output is incredibly easy to find. Generally, it appears on the line immediately following the command itself. Running the program should produce the following output:

hello, world

Congratulations, you have completed the HelloMono tutorial. Now I know what some of you are thinking, "It took nine lines to produce the same output K&R did in four? Ha! Gotcha, I knew .NET was lame." Well, it is not that simple. Here is a minimalist C#.NET HelloWorld console example for the purist:

General Resources

History

Version 1.0This is the first version of this article.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

Share

About the Author

Will Currently serves as the Senior Director of Research and Development at Underwriters Laboratories Workplace Health and Safety in Franklin, Tennessee. He is Responsible for the direction of a globally distributed, talented, and diverse group of architects, software engineers, user experience designers and developers in the development of advanced software platforms and products for the Workplace Health and Safety business unit of UL.

Comments and Discussions

To develop .NET applications in Windows without VS.NET, you don't really need Mono: the .NET runtime contains a C#-compiler, a VB.NET-compiler and some dev-tools. From the Microsoft-website you can download the .NET SDK that has extra tools, extra examples and the documentation. You can even use it on Windows 98 (normal install doesn't work on Win98, but you can use a little trick to do the installation anyway). Then you can use SharpDevelop as an IDE, or use a texteditor (i like/use TextPad).
I'm not saying this to be negative about Mono, because I like the idea behind Mono, but from reading the article I had the impression that you meant to say that Mono is the only alternative to VS.NET. And Mono is just in Beta 1-stage and doesn't offer all the base classes.

You are right. the .NET Framework is currenly available for free and I am in the process of reading the book - Dissecting a C# Application - Inside SharpDevelop, quite possibly one of the three best programming books I've read. I'll take a look at the language I used in the article and see where I went astray on the tone - if you hunt down the other article I wrote (links in the first paragraph) you will see that it is ALL about using the .NET Framework and a text editor (UltraEdit in my case)