Works on my machine

LINQ

Code review

I’m going to review 3 C# code snippets I have recently come across. At first glance they all may seem to be fine - they do their job and they can be understood easily. So what is wrong about them? I will try to answer this question thoroughly, but before I do that, I would like to ask you to do that too. Don’t rush into reading the review, think what you would change, how you would implement that. Ideally, write down your solution, then compare. 1. String concatenation This is a pretty common scenario. ......

Slicing for .NET

One year ago I had a little crush on Python. Although I consider myself as a hardcore C# coder I think it is a good thing to try other languages. Even if we do not plan to change our major, it is nice to grasp other techniques and practice new ways of thinking, which can be later incorporated into our favorite language. First odd thing I read about Python was that a developer can use negative array indices. My reaction was: Why would I even want to get an IndexOutOfRangeException? That is just silly. ......

Beware the IEnumerable<T>

If you see IEnumerable<T> as read only collection crippled brother, this post is for you. Many times I found myself loosing couple of hours looking for a bug everywhere, but not in a place where it was. From time to time the bug is caused by my simplified perception of IEnumerable. The thing is, if we do not know the mechanism that serves specific IEnumerable elements, we cannot be sure that each iteration will return equal collections with same objects. It is not a big deal if it hosts immutable ......

Linq’s ZIP for adjacent items

I found that Linq’s ZIP is really great for adjacent item computations. For example, let’s have a collection of dates: var dates = new DateTime[] { new DateTime(2000,1,1), new DateTime(2000,1,2), new DateTime(2000,1,5) }; How would you compute time difference of adjacent items ? I like to use Zip for this kind of job: dates.Zip(dates.Skip(1), (d1, d2) => d2 - d1); As you might expect, the result will be: { 1 day, 3 days } ......