Linq

LINQ OrderBy – using Comparer<T>.Create and F#’s Object Expressions

.Net 4.5 introduced a handy little new method Comparer<T>.Create to aid the creation of bespoke comparers, which is great because it means that you don’t have to define a new Comparer class when it is going to be needed once. In case you’re wondering, it’s still not possible to define anonymous implementation of interfaces in …

LINQ OrderBy – using Comparer<T>.Create and F#’s Object Expressions Read More »

.Net Tips – Use LINQ to create pairs of adjacent elements from a collection

Suppose you have an array of numbers, say, [1, 3, 5, 7, 9, …], and you want to pair each element up with its neighbour in the array, e.g. [[1, 3], [3, 5], [5, 7], [7, 9], …]. Sure, you can iterate through the indices of the elements and recursively grab the element at an …

.Net Tips – Use LINQ to create pairs of adjacent elements from a collection Read More »

Converting List<T> using covariance

I saw an interesting use of covariance today, consider an interface IMyClass and an implementing class MyClass: 1: public interface IMyClass { } 2: 3: public class MyClass : IMyClass { } If you want to convert an List<MyClass> to a List<IMyClass> you would normally use the Enumerable.Cast method but did you know that you …

Converting List<T> using covariance Read More »

Eric Lippert on the decision to omit Enumerable.ForEach

Found another interesting post on Eric Lippert’s blog, this one explain the rationales behind why there’s no built-in Enumerable.ForEach extension method, one which myself and no doubt many others had decided to implement ourselves. As he explains, there are two main philosophical reasons why he’s against such an extension method: "The first reason is that …

Eric Lippert on the decision to omit Enumerable.ForEach Read More »

Enumerable.Except returns distinct items

This one took me by surprise a little but there’s something about the Enumerable.Except method which you ought to know about. At first you might have assumed (as I did) that given an array as the base set and calling the Except extension method with a second array will return all the items in the …

Enumerable.Except returns distinct items Read More »

Functional programming with Linq – Enumerable.SequenceEqual

Yet another useful method on the Enumerable class, the SequenceEqual method does exactly what it says on the tin and tells you whether or not two sequences are of equal length and their corresponding elements are equal according to either the default or supplied equality comparer: As you know, for reference types the default equality …

Functional programming with Linq – Enumerable.SequenceEqual Read More »

Enumerable.Append and Enumerable.Prepend extension methods

Another gem I found on StackOverflow today (I’ve been spending a lot of time there these last couple of days..), this time in the form of a question on how to append or prepend a single value to an IEnumerable<T>. Greg provided an elegant solution to this particular problem, and here’s his answer: Which you …

Enumerable.Append and Enumerable.Prepend extension methods Read More »

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close