Over a million developers have joined DZone.

A Quick Introduction to C#

mono-c-sharpWith 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:

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;
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.

[Original Story Link]


The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}