fredrik.eriksson

Coffee and a keyboard

2008 the year of the leap second

300px-LeapsecondTonight, a fascinating thing will happen (or not so fascinating depending on how geeky you are :) ), because 2008 is the year of the leap second. After 23:59:59, we have 23:59:60 before rolling over to 00:00:00. This crazy occurrence is the result of the use of stable atomic clocks and the fact that the Earth is slowing down.

Yea and HAPPY NEW YEAR and don’t forget its all about the code.

Array slicing extensions method

Slicing an array means to specify an sub-array of it. If you e.g. had the array [1, 2, 3, 4, 5] slicing it from index 1 to 3 would result in [2, 3, 4]. Its relatively easy to create a extension method for any array of type T. My implementation uses IEnumerable and yield but it can be easily modified to return a standard array of type T instead:

public static class ArrayExtensions
{
    public static IEnumerable<T> Slice<T>(this T[] self, int start, int end)
    {
        if (self == null)
            throw new ArgumentNullException("self");

        if (start < 0 || start >= self.Length)
            throw new ArgumentOutOfRangeException("start");

        if (end < 0)
            end += self.Length + 1;

        if (end < start || end > self.Length)
            throw new ArgumentOutOfRangeException("end");

        for (int i = start; i <= end; i++)
            yield return self[i];
    }
}

Appling the extension method on the example I used above the code would look like this:

var arr = new[] {1, 2, 3, 4, 5};
var result = arr.Slice(1, 3); // result = [2, 3, 4]

Testing internal classes

Q: How do I write tests against internal classes?

A: .NET provides a assembly attribute called InternalsVisibleTo, this attribute lets you specify explicit internal access to your test assembly. If you e.g. had a test assembly named Tests you would put the code bellow into the AssemblyInfo.cs file of the assembly you want to test:

[assembly:InternalsVisibleTo("Tests")]

If the assembly you want to test is strongly named, your test assembly have to be strongly named as well.