RSS

A Quick Introduction to C#

Wed, Oct 8, 2008    (Click to Rate!) Loading ... Loading ...

Linux, Programming


With Mono’s 2.0 release it seems there’s no better time than now to start investing in C#, especially if you are looking for platform independence. To help move that investment along, I’ve written up a small tutorial that should get you up and running with C#. Before we begin, you’ll need to grab a couple of things like the Mono runtime environment and the Mono compiler gmcs.

If you are running Ubuntu the install is pretty trivial:

$ sudo apt-get install mono-2.0-devel mono-gmcs

Now we are ready to start.

Java developers will find that C# feels like a very familiar language.  Being a Java coder there were some nuances that caught me off guard while writing my first basic programs.  For instance, a basic “Hello World!” example can be written in a variety of ways.

For now, we’ll stick to the absolute basics which is conveniently the closest to Java. Add the following code to a file called hello.cs:

public class HelloWorld {
  public static void Main(){
    System.Console.WriteLine("Hello World!");
  }
}

Then, compile the example using gmcs:

$ gmcs hello.cs

You will see that the compiler created a hello.exe file which can be run using the Mono runtime.

$ mono hello.exe
Hello World!
$

One of the nuances I was talking about earlier was in reference to namespaces.  If you are familiar with C++, you will find that namespaces in C# function similarly. If, however, a bulk of your education was invested in Java, such as mine, you might be a little confused at first. Let me assuage any of those fears by stating that namespaces help more with code organization than anything else.  Let us rewrite the “Hello World!” example one more time using a namespace.

Add the following code to a file called hellonamespace.cs:

using System;

public class HelloWorld {
  public static void Main(){
    //Notice how the following line
    //is different from the first example
    Console.WriteLine("Hello World!");
  }
}

Now compile and run it….

$ gmcs hellonamespace.cs
$ mono hellonamespace.exe
Hello Namespace...errr World!

To explain a bit, the second hello world example used the System namespace. By using this namespace we were able to call WriteLine from the Console class without needing to fully qualify the class (ie. System.Console.Writeline). Imagine, for a moment, that a namespace is like a tool shed where multiple tools are housed.  If you exist in the tool shed you have the ability to use a hammer object or a saw object that also exist in the same shed.  That being said, multiple tool sheds can exist and some  may even have similar objects like a hammer or a saw.

If you are a Java programmer you might be thinking that package names are the equivalent.  You would be correct, but C# does not require class files to be stored in certain locations like Java convention does.  This is a great way to organize and allow for more flexiblility in a single file.

This is just a quick tutorial, however, and there are some more neat tricks that can be done with namespaces like namespace aliasing, external aliasing, and using the global:: operator which you can read more about here.

Let’s continue on.  Since I like the golden ratio, and because I think it makes pretty pictures,
lets go ahead and implement a Fibonacci example using C#.

Start by adding the following to a fib.cs file:

//Namespace
using System;

public class Fib {
  public static void Main(){
    Fib fibGenerator = new Fib();
    //Notice the subtle difference in outputting
    //variables.  This is similar to C/C++
    Console.WriteLine("The fib number of {0} is {1}",
                      10, fibGenerator.grabFib(10));
  }
  //Constructor Class
  public Fib(){}

  //Fibonacci Computer
  public int grabFib(int num){
    if(num < 2)
      return num;
    else
      return grabFib(num-1)+grabFib(num-2);
  }
}

Now compile and run it….

$ gmcs fib.cs
$ mono fib.exe
The fib number of 10 is 55
$

You should note that writing to the console was a little different here.  I was able to specify multiple variables in the Console.WriteLine call by using the “{0}” and “{1}” notation and placing the variables at the end respectively.  This borrows a bit from the earlier C languages, and is a nice way to keep console messages clean.

As you can see, if you want to learn C# and you have a Java background you will be able to pick the syntax up very quickly.  If you have had any object oriented experience it will definitely help in understanding basic C# conventions. The only way to find out is to dive in.  Give it a shot, and let us know what you think.

Share This on Your Favorite Social Network:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Furl
  • Propeller
  • Reddit
  • Technorati
  • StumbleUpon
  • DZone
  • MisterWong
  • TwitThis
  • Slashdot
  • SphereIt
, , , ,

This post was written by:

Ray Gomez - who has written 46 posts on kallasoft.

Ray, a Linux and Unix nut, spends a majority of his daily ritual programming and testing for Big Blue. In his free time he manages to tweak the currently running thinkpad+KDE4 (WHOA) setup, read, and he occasionally gets out of the fluorescent lights to play roller hockey.

3 Comments For This Post

  1. Robert Says:

    Does it have to compile to an exe? That seems so un-Unix.

  2. Robert Says:

    Oh…and thanks for the article.

  3. rgomez Says:

    Yes, you make a very good point, and how rude of me! If you’d like to compile it to something a little more Unix’y try this:

    $gmcs fib.cs -out:fib.o

    or for something nostalgic…

    $gmcs fib.cs -out:a.out

    That is much better. Thanks for your comment!!

1 Trackbacks For This Post

  1. Using Namespaces in C# | kallasoft Says:

    [...] lets explain what a namespace is. In my previous article, I described a namespace as a tool shed that can house many tools. If you exist in the too shed you [...]

Leave a Reply