February 4th, 2009

String.Format is a very powerful method but the documentation at MSDN is quite wordy and spreads the details over many pages. For this post I have made quick reference to its many specifiers and for good measure added some examples to help get you started.If you would like to format a date, currency, number or just the time, String.Format is your friend.
Read the rest of this entry »
Tags: Learn C#, string.format
Posted in Beginner, Learn C# | 1 Comment already!»
January 7th, 2009

Occasionally you need to time the performance of a C# function to see if its as fast, or as slow, as you think it is. You could of course run your code through a profiler. But in situations that that would be overkill there exists a quick solution. Originally I looked at the standard DateTime class, but NET 2.0 introduced the Stopwatch class which is much more suitable. In this post I look at how you can measure your functions performance down to the last fraction of a millisecond.
Read the rest of this entry »
Tags: datetime, function execution, Learn C#, stopwatch, timing
Posted in Beginner, Learn C# | 1 Comment already!»
January 2nd, 2009

Cool (Linux) applications have their own icon in the notification area. To add my own icon in the Gnome notification area using Mono only took a few lines of code. In the post below I show how you can add your icon and associate a tooltip and right click menu to it.
Read the rest of this entry »
Tags: GTK, Learn C#, mono, StatusIcon, Tooltip
Posted in Beginner, GTK, Learn C# | 2 Comments - getting there! »
January 1st, 2009
Sorting a basic generic list in C# is trivial as long as you store basic elements such as strings or integers for which default comparison classes have been defined. In this post we will look into how we can reverse the string sort by implementing your own ICompare comparer, and how you can build your own comparison routines to compare other types. In the example we sort a list (List<>) of people by both their name and their age.
Read the rest of this entry »
Tags: generic, ICompare, Learn C#, list, quicksort, sorting
Posted in Beginner, Learn C# | No Comments, yet!
December 29th, 2008
As soon as I heard that the MonoDevelop Subversion code included support for actually debugging my C# code I tried installing it to give it a go. Optimistically I kept notes, figuring that it would be useful to write a post about the install process later. As my notes started to turn into a book, and the arrows back and forth and crossed out scribbles increased I gave up on writing the ultimate “ten step guide to installing Monodevelop on Ubuntu”.
When I finally had MonoDevelop compiled and installed — the promised debugging didn’t actually work. Bummer. I gave up, and went back to coding the old way (with ticker tape). This morning I checked out the latest SVN, installed it and ran it, just for laughs.
And behold as shown below — debugging actually worked. I can step and trace through the code, and set watches making MonoDevelop suddenly that much more useful to me.
The best thing? The Mono C# libraries are not black boxes but open source — you can step and trace directly into the Mono libraries themselves and find out what Mono is doing as you call library functions.

The problem with installing MonoDevelop 2.0alpha 2 is that the guides online don’t actually produce a working MonoDevelop installation. If you run MonoDevelop under Ubuntu like I do, the number of dependencies and sub-dependencies you need to resolve is large and I ended up compiling, re-compiling and changing version numbers of modules multiple times to find combinations that worked. In addition I had to change, add and resolve many missing library conflicts within Ubuntu as well.
Concluding: Yes, it is possible. Just do not expect a quick and easy install of MonoDevelop 2.0 alpha 2 on your Ubuntu installation.
This is fun only if you have plenty of spare time.
Some hints — this does not produce a working Mono Develop installation
- Remove Mono 1.9, MonoDevelop 1.0, and any and all other mono related things from your ubuntu installation before attempting to compile MonoDevelop 2.0 Alpha 2 to avoid conflicts down the road.
- Install mono 2.3, the debugger and mono develop from SVN, not from the packages provided on the website (they do not work)
- $ svn co svn://anonsvn.mono-project.com/source/trunk/mono
$ svn co svn://anonsvn.mono-project.com/source/trunk/mcs
$ svn co svn://anonsvn.mono-project.com/source/trunk/debugger
$ svn co svn://anonsvn.mono-project.com/source/trunk/mono-addins
$ svn co svn://anonsvn.mono-project.com/source/trunk/olive
$ svn co svn://anonsvn.mono-project.com/source/trunk/monodevelop
- I installed gtk-sharp-2.12.5, gnome-sharp-2.20.0 and libglade-2.0.1 as support libraries
- Compile and install monodevelop last of all
Mono JIT compiler version 2.3 (/trunk/mono r121276 Thu Dec 11 13:04:53 CST 2008)
Copyright (C) 2002-2008 Novell, Inc and Contributors. www.mono-project.com
Tags: Learn C#, monodevelop
Posted in Intermediate, Learn C# | No Comments, yet!
December 12th, 2008
This post looks at how to implement a binary tree using generics in C#. A binary tree is a data structure in which each node has at most two children. They are a good way to store unsorted data, as the data becomes sorted as you insert it into the tree.
Another benefit of storing information in a tree structure is the opportunity for faster search times. In an array (or list) you need to traverse the whole array to discover if it contains a particular value. A well-balanced binary tree can drastically reduce the number of look-ups required.
Read the rest of this entry »
Tags: binary tree, generics, Learn C#
Posted in Algorithms, Beginner, Learn C# | No Comments, yet!
December 9th, 2008

Hash values have many uses in computing: for storing password tokens, securing that a file hasn’t been tampered with, or to create a short semi-unique signature for a larger data set. A hash algorithm takes a data set — such as a string — and turns it into a numeric value of a certain length.
This article will go into how to create a hash of your passwords and how salting them makes them more secure.
Read the rest of this entry »
Tags: crc, hash, Learn C#, md5, salted, sha-1, sha-256
Posted in Algorithms, Intermediate, Learn C# | No Comments, yet!
December 8th, 2008
.NET 2.0 added the NetworkInformation namespace which allow you to discover and query all the network interfaces in the local machine. The information this provides is very similar to what you can find by opening the “Network Connections” in Windows. In this post I have a look into the kinds information it provides — and what to look out for if your code has to run on Mono as well.
Read the rest of this entry »
Tags: Learn C#, network
Posted in Beginner, Learn C# | No Comments, yet!
December 8th, 2008
A common problem is the find out the IP address of the machine your C# program is running on. C# provides two methods for obtaining the information. The first one is easy; but somewhat undocumented. The other one is just a little harder. If your code has to run on Mono then the second option is your only alternative.
Read the rest of this entry »
Tags: Learn C#, local ip
Posted in Beginner, Learn C# | 1 Comment already!»
November 26th, 2008

In the dark ages before the Internet there was “war-dialing”: randomly calling telephone numbers in the hope that on the other side a computer modem would pick up. War Dialing was glamorized by the movie “Wargames” but portscanning is just like that: you too can help the world narrowly avoid nuclear Armageddon. This article shows how you can build a simple Portscanner in C#.
Read the rest of this entry »
Tags: Learn C#, portscanner, tcpclient
Posted in Beginner, Learn C# | No Comments, yet!