This is meant to be an introduction to C# and Mono. First, we need to download the mono runtime and compiler:
sudo apt-get install monodevelop mono-devel
For the purpose of this post, we are just going to create a simple program which prints out the text “Hello World”. Go ahead and make a directory for our project so we don’t make our home directory entirely too messy. I just make a directory called ‘mono’ under /home/sharms/mono for the duration of this post.
Go ahead and create a file called ‘helloWorld.cs’, and paste the following:
// Declare which namespace we want to use.
// This allows us to use Console.WriteLine
// instead of System.Console.WriteLine
// Basically like a python 'import' statement, or a
// php 'include' statement
using System;
// We create a class to contain it
class HelloWorld
{
// Must have a main function as this is what is
// first called when executing this
static void Main()
{
// C# requires ; after statements
// Actually output "Hello World"
Console.WriteLine("Hello World");
}
}
To compile it, run:
gmcs helloWorld.cs
This will generate a file called ‘helloWorld.exe’. To run it, type:
mono helloWorld.exe Hello World
And there you have it. I also stumbled on a Makefile tutorial today, so we can make a simple make file (I won’t describe all of this, just look at how it’s used). The tutorial is at: http://www.wlug.org.nz/MakefileHowto
[sharms@sparrow mono]$ cat Makefile COMPILER=gmcs all: helloWorld.exe helloWorld.exe: helloWorld.cs $(COMPILER) helloWorld.cs clean: rm -f helloWorld.exe .PHONY: all clean [sharms@sparrow mono]$ make gmcs helloWorld.cs [sharms@sparrow mono]$ make clean rm -f helloWorld.exe
Related posts:
- Annoying people with code: A gentle introduction to C# and Mono Part 2 – Data Types In part one of this series we covered making a...
- Annoying people with code: A gentle introduction to C# and Mono Part 3: Creating a GUI (Graphical) Mono / C# Program In this post we will cover how to make a...
- NCPFS NCPFS each release generally is sent out completely not working...
- Finding the difference between two files Overview of diff, colordiff, and meld...
- Quick tip: Counting lines of code I wanted to count the lines of code on a...
#1 by Joseph James Frantz on July 16, 2009 - 5:03 pm
Quote
I don’t think you know how to annoy people with code. Let me give you an example.
IDENTIFICATION DIVISION.
PROGRAM-ID. HelloWorld.
DATA DIVISION.
PROCEDURE DIVISION.
Display “Hello World”
Goback.
(Yup you can mix case with cobol and nope you dont need periods after every line).
See, THAT’s how you irritate people. Nice try though:)
#2 by sharms on July 16, 2009 - 5:07 pm
Quote
@Joseph Haha thanks for the tip
#3 by joe on July 16, 2009 - 5:16 pm
Quote
You should do a 10 part series on programming with C# and Mono!
#4 by Joseph James Frantz on July 16, 2009 - 5:46 pm
Quote
Steven, YW:)
I always dig your blog man. Even if you use disgusting languages like…anything NOT Cobol:D
#5 by Jo Shields on July 16, 2009 - 6:34 pm
Quote
Your list of packages is a bit sucky, man. “monodevelop mono-devel” should be the entire list of required packages. We haven’t had a “mono” binary package for years!
#6 by leonel on July 16, 2009 - 6:57 pm
Quote
.exe really is annoying
#7 by interested on July 16, 2009 - 8:41 pm
Quote
hey, could you recommend a good online tutorial for getting started with mono?
#8 by Vadim P. on July 16, 2009 - 9:28 pm
Quote
Lol.
Maek one for Vala too
#9 by Courtney on July 16, 2009 - 10:09 pm
Quote
Why is it that everyone on Ubuntu is flooding their blogs with pro_mono things? Do we need any .exe in our ubuntus? What makes mono so special apart from being risky? It seems to be a pile of Microsoft crafted thing to make us engulf Silverlight and follow their ways,… FlossGods, assist us!
#10 by Anshul on July 16, 2009 - 11:46 pm
Quote
For all you Ubuntueros, thanks but no thanks to Mono. I’m gonna stay with Mandriva or Fedora’s GNOME and stay away from Mono
#11 by Justin on July 17, 2009 - 11:15 am
Quote
I agree with Vadim P.: A Vala tutorial would be awesome with Mono. You can use the same IDE to code both!
When you get done with the Mono tuts, you should go back and do the same examples in Vala.
Cheers!
#12 by Jo Shields on July 17, 2009 - 4:09 pm
Quote
Vala examples would be interesting – although I suspect only *really* interesting once you get to complex examples, not this “hello world” stuff