C# type inference and extension methods: an abuse

code 23 February 2011 | 0 Comments

Here’s a little example of statically-sized stacks in C#. They’re implemented with a linked-list as the backing store: using System;   namespace ConsoleApplication { public static class MainClass { // Example:   public static void Main(string[] args) { var stack = Stack.New<int>(); // real type Stack<Z,int>   var s1 = stack.Push(1).Push(2).Push(3); // real type Stack<S<S<S<Z>>>> [...]

Tagged in , , , ,

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

code 20 September 2008 | 0 Comments

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

Tagged in , , ,

Implementing IEnumerable easily

code 4 April 2008 | 0 Comments

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

Tagged in , ,

object.Equals handles null values correctly

code 28 March 2008 | 0 Comments

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

Tagged in , , ,