Tag Archives: .net

Implementing IEnumerable easily

Say that you’re implementing a linked list, and you want an enumerator:

public IEnumerator<T> GetEnumerator()
{
return new Stream<T,Node>(first,
node => node.next == null ? null : Tuple.Of(node.next, node.datum).AsNullable());
}

This uses the following utility class to implement the enumerator in one line (along with some code for Tuples [...]

object.Equals handles null values correctly

Here’s the source code, as disassembled by Reflector:

public static bool Equals(object objA, object objB)
{
return ((objA == objB) || (((objA != null) && (objB != null)) && objA.Equals(objB)));
}

It seems that not even Microsoft knows this! I spotted this code, from ASP.NET’s MVC implementation, on Scott Hanselman’s blog:

return (other != null) &&
[...]

A simple BigNum library for .NET

Update
Due to minor demand, the code is also available: BigNum source.
Please note that I haven’t actually touched the code since it was first written. I’m sure there’s some things that don’t work properly. If you’re doing anything big with this you probably want to write some ‘destructive’ update functions for adding, etc. At the moment [...]