C# 4

.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 »

.Net Tips – Make sure the runtime types match when combining delegates

In C#, it’s possible to combine two delegates, A and B to create a new multicast delegate, C:   When the multicast delegate is executed, the combined delegates are executed in order as you can see from the example above. But before you can start mix and matching your delegates like a kid in a …

.Net Tips – Make sure the runtime types match when combining delegates Read More »

Things I didn’t know about expando objects

I found out two interesting things about the ExpandoObject class introduced in C# 4 this bank holiday weekend: 1. you can specify custom events on them 2. it implements the INotifyPropertyChanged interface Here are some quick demos to show you how to use these features: Custom Events To add a custom event is the same …

Things I didn’t know about expando objects Read More »

IDictionary<string, object> to ExpandoObject extension method

As you know, the ExpandoObject class implements the IDictionary<string, object> interface, so if you have an ExpandoObject you can easily cast it to an IDictionary<string, object> but there’s no built-in way to easily do the reverse. Luckily, I came across a very useful extension method today which converts an IDictionary<string, object> into an ExpandoObject, which …

IDictionary<string, object> to ExpandoObject extension method Read More »

Performance Test – Delegates vs Methods

Quite some time ago I was asked to cover a C# developer interview for one of my neighbouring teams, and on the sheet of questions the original interviewer wanted me to ask was this question: Q. Why should you NEVER use delegates? I thought: “Well, at least I can rule myself out for the role!” …

Performance Test – Delegates vs Methods Read More »

Interesting observation on C# 4’s optional parameter

The other day I had an interesting observation on the optional parameters in C# 4, whereby if you specify a parameter as optional on an interface you don’t actually have to make that parameter optional on any implementing class: 1: public interface MyInterface 2: { 3: void TestMethod(bool flag=false); 4: } 5: 6: public class …

Interesting observation on C# 4’s optional parameter Read More »

Performance Test – Prime numbers with LINQ vs PLINQ vs F#

Having spent quite a bit of time coding in F# recently I have thoroughly enjoyed the experience of coding in a functional style and come to really like the fact you can do so much with so little code. One of the counter-claims against F# has always been the concerns over performance in the most …

Performance Test – Prime numbers with LINQ vs PLINQ vs F# 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