C# 4

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

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

In C#, it’s pos­si­ble to com­bine two del­e­gates, A and B to cre­ate a new mul­ti­cast del­e­gate, C:   When the mul­ti­cast del­e­gate is exe­cut­ed, the com­bined del­e­gates are exe­cut­ed in order as you can see from the exam­ple above. But before you can start mix and match­ing your del­e­gates like a kid in a …

.Net Tips – Make sure the run­time types match when com­bin­ing del­e­gatesRead More »

Things I didn’t know about expando objects

I found out two inter­est­ing things about the ExpandoOb­ject class intro­duced in C# 4 this bank hol­i­day week­end: 1. you can spec­i­fy cus­tom events on them 2. it imple­ments the INo­ti­fyProp­er­ty­Changed inter­face Here are some quick demos to show you how to use these fea­tures: Cus­tom Events To add a cus­tom event is the same …

Things I didn’t know about expando objectsRead More »

Turn ExpandoObject into static type

As many oth­ers have shown, the new ExpandoOb­ject intro­duced in C# 4 can be pret­ty use­ful in some sce­nar­ios. How­ev­er, on the odd occa­sion when you want to con­vert an ExpandoOb­ject to a sta­t­ic type you have defined you can be for­giv­en for feel­ing a lit­tle lost as there are no well doc­u­ment­ed ways to …

Turn ExpandoOb­ject into sta­t­ic typeRead More »

IDictionary<string, object> to ExpandoObject extension method

As you know, the ExpandoOb­ject class imple­ments the IDictionary<string, object> inter­face, so if you have an ExpandoOb­ject you can eas­i­ly cast it to an IDictionary<string, object> but there’s no built-in way to eas­i­ly do the reverse. Luck­i­ly, I came across a very use­ful exten­sion method today which con­verts an IDictionary<string, object> into an ExpandoOb­ject, which …

IDictionary<string, object> to ExpandoOb­ject exten­sion methodRead More »

Performance Test – Delegates vs Methods

Quite some time ago I was asked to cov­er a C# devel­op­er inter­view for one of my neigh­bour­ing teams, and on the sheet of ques­tions the orig­i­nal inter­view­er want­ed me to ask was this ques­tion: Q. Why should you NEVER use del­e­gates? I thought: “Well, at least I can rule myself out for the role!” …

Per­for­mance Test – Del­e­gates vs Meth­odsRead More »

No covariance for value type

For a while now I’ve been won­der­ing why C#‘s sup­port for covari­ance does not cov­er val­ue types, both in nor­mal array covari­ance and covari­ance in the gener­ic para­me­ter intro­duced in C# 4: 1: void Main() 2: { 3: int i = 0; 4: string str = “hel­lo world”; 5: 6: TestMethod(i); // legal 7: TestMethod(str); …

No covari­ance for val­ue typeRead More »

Interesting observation on C# 4’s optional parameter

The oth­er day I had an inter­est­ing obser­va­tion on the option­al para­me­ters in C# 4, where­by if you spec­i­fy a para­me­ter as option­al on an inter­face you don’t actu­al­ly have to make that para­me­ter option­al on any imple­ment­ing class: 1: pub­lic inter­face MyIn­ter­face 2: { 3: void TestMethod(bool flag=false); 4: } 5: 6: pub­lic class …

Inter­est­ing obser­va­tion on C# 4’s option­al para­me­terRead More »

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

Hav­ing spent quite a bit of time cod­ing in F# recent­ly I have thor­ough­ly enjoyed the expe­ri­ence of cod­ing in a func­tion­al style and come to real­ly like the fact you can do so much with so lit­tle code. One of the counter-claims against F# has always been the con­cerns over per­for­mance in the most …

Per­for­mance Test — Prime num­bers with LINQ vs PLINQ vs F#Read More »

ThreadStatic vs ThreadLocal<T>

Occa­sion­al­ly you might want to make the val­ue of a sta­t­ic or instance field local to a thread (i.e. each thread holds an inde­pen­dent copy of the field), what you need in this case, is a thread-local stor­age. In C#, there are main­ly two ways to do this. Thread­Sta­t­ic You can mark a field with …

Thread­Sta­t­ic vs ThreadLocal<T>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.