Tag Archives: csharp

Things I’d like to see in C#: Conditional interface implementation

To explain this, imagine you are designing a type Wrapper<T>, which is just a simple wrapper around a type T. class Wrapper<T> { T value; public Wrapper(T theObject) { value = theObject; } // Some other methods… } Now, since this is a just a wrapper around some type T, we would like to implement [...]

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 and an extension method for structs): [...]

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) [...]