Saturday, December 26, 2009

Challenge yourself

I have been participating in some contests in december by submitting some code to microsoft. The following is two of my contributions that weren't picked as the winner.

The assignment for my first contribution was to make an extension method for one of the new types in .net 4.0. What I made was an extension method that would return a text presentation of a BigInteger in whatever base you want (16 being the maximum as I don't know any standard for higher bases).

The second assignment was to implement group functionality for collections that would take several properties to group by. I chose to make an extension method that would take strings as input and group the collection by the values of the properties identifier by the strings. The result is a collection that contains a collection for each group. This was my first time for using Linq together with reflection.

Friday, December 18, 2009

The design flaw of foreach

The foreach loop contruct in C# is very useful, but it has one major design flaw. The design flaw I'm thinking of is the NullPointerException it throws when it's given a collection that is null.
I do realize that it makes sense to throw an exception when no input is provided. The problem is that I have never seen an application that expects an exception from the foreach contruct.
With foreach you must know that the collection is set, or wrap the whole thing in a check for null. This adds an extra codeblock to your code.

I have created an extension method for collections so I can avoid the extra codeblock and do something like this:

IList<SomeObject> list = GetObjects();
foreach(SomeObject o in list.NullChecked()){

Even if the GetObjects() method returns null your code wont fail. It just doesn't execute the loop.

public static IEnumerable<T> NullChecked<T>(this IEnumerable<T> input){
        return input;
        return new List<T>();

Tuesday, December 08, 2009

The Gartner Report

The other day I read an article that quoted a Gartner Report that said something like: "In the future there will be a demand for fast computer drives". Maybe its just me... but aren't statements like that a bit vague. Its the kind of visions you might read in the dilbert comic. They are so obvious that if they don't come true its because the world has collapsed in the meantime. Anyone can state the obvious like that.

Here's my vision for the future:
In the future things will be different.
If you want this elaborated you have to buy my report which I will have to make up. :)

Monday, December 07, 2009

Easy loops

Loops are common in a lot of code. I made a few contructs in plain to make it even easier to do simple iterations in .net.
//do something 3 times

//count an integer from 3 to 8 (both included) and do some action for each value.
3.To(8).Do(i=>Console.WriteLine("The number is: " + i ))

I know this is sort of thing is common in languages like Ruby or F# but that's not gonna stop me from doing it in C# :)

The implementation can be found at