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

.Net 4.5 intro­duced a handy lit­tle new method Comparer<T>.Create to aid the cre­ation of bespoke com­par­ers, which is great because it means that you don’t have to define a new Com­par­er class when it is going to be need­ed once. In case you’re won­der­ing, it’s still not pos­si­ble to define anony­mous imple­men­ta­tion of inter­faces in …

LINQ Order­By – using Comparer<T>.Create and F#’s Object Expres­sionsRead More »

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

Sup­pose you have an array of num­bers, say, [1, 3, 5, 7, 9, …], and you want to pair each ele­ment up with its neigh­bour in the array, e.g. [[1, 3], [3, 5], [5, 7], [7, 9], …]. Sure, you can iter­ate through the indices of the ele­ments and recur­sive­ly grab the ele­ment at an …

.Net Tips – Use LINQ to cre­ate pairs of adja­cent ele­ments from a col­lec­tionRead More »

Converting List<T> using covariance

I saw an inter­est­ing use of covari­ance today, con­sid­er an inter­face IMy­Class and an imple­ment­ing class MyClass: 1: pub­lic inter­face IMy­Class { } 2: 3: pub­lic class MyClass : IMy­Class { } If you want to con­vert an List<MyClass> to a List<IMyClass> you would nor­mal­ly use the Enumerable.Cast method but did you know that you …

Con­vert­ing List<T> using covari­anceRead More »

IList<T> doesn’t extend IList

Look­ing at these two images snapped right off MSDN, what do you notice? … that IEnumerable<T> extends IEnu­mer­able but IList<T> doesn’t extend IList!? I know, pecu­liar isn’t it?   Turns out, it was for a good rea­son: “A sequence of inte­gers can be treat­ed as a sequence of objects, by box­ing every inte­ger …

IList<T> doesn’t extend IListRead More »

Eric Lippert on the decision to omit Enumerable.ForEach

Found anoth­er inter­est­ing post on Eric Lippert’s blog, this one explain the ratio­nales behind why there’s no built-in Enumerable.ForEach exten­sion method, one which myself and no doubt many oth­ers had decid­ed to imple­ment our­selves. As he explains, there are two main philo­soph­i­cal rea­sons why he’s against such an exten­sion method: “The first rea­son is that …

Eric Lip­pert on the deci­sion to omit Enumerable.ForEachRead More »

LINQ — Some pitfalls to look out for

As great as LINQ is and how it has trans­formed the way we inter­act with data in .Net to the point many of us won­der how we had man­aged with­out it all this time! There are how­ev­er, some pit­falls one can fall into, espe­cial­ly with the con­cept of delayed exe­cu­tion which is eas­i­ly the most …

LINQ — Some pit­falls to look out forRead More »

Enumerable.Except returns distinct items

This one took me by sur­prise a lit­tle but there’s some­thing about the Enumerable.Except method which you ought to know about. At first you might have assumed (as I did) that giv­en an array as the base set and call­ing the Except exten­sion method with a sec­ond array will return all the items in the …

Enumerable.Except returns dis­tinct itemsRead More »

Functional programming with Linq — Enumerable.SequenceEqual

Yet anoth­er use­ful method on the Enu­mer­able class, the SequenceE­qual method does exact­ly what it says on the tin and tells you whether or not two sequences are of equal length and their cor­re­spond­ing ele­ments are equal accord­ing to either the default or sup­plied equal­i­ty com­par­er: As you know, for ref­er­ence types the default equal­i­ty …

Func­tion­al pro­gram­ming with Linq — Enumerable.SequenceEqualRead More »

Enumerable.Append and Enumerable.Prepend extension methods

Anoth­er gem I found on Stack­Over­flow today (I’ve been spend­ing a lot of time there these last cou­ple of days..), this time in the form of a ques­tion on how to append or prepend a sin­gle val­ue to an IEnumerable<T>. Greg pro­vid­ed an ele­gant solu­tion to this par­tic­u­lar prob­lem, and here’s his answer: Which you …

Enumerable.Append and Enumerable.Prepend exten­sion meth­odsRead 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.