Recently I realized that I am overusing "as" keyword. It is probably because its syntax is more fluent for me than ordinary cast. I see this overuse even in Microsoft examples. Let the following code show you what I mean: 1: object x = "1.0"; 2: Version v = x as Version; 3: Console.WriteLine(v.Major); and just for reference reasons ordinary cast: 1: object x = "1.0"; 2: Version v = (Version)x; 3: Console.WriteLine(v.Major); When you run first snippet it will crash on third line throwing NullReferenceException. ......
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 ......
I found that Linq’s ZIP is really great for adjacent item computations. For example, let’s have a collection of dates: 1: var dates = new DateTime[] 2: { 3: new DateTime(2000,1,1), 4: new DateTime(2000,1,2), 5: new DateTime(2000,1,5) 6: }; How would you compute time difference of adjacent items ? I like to use Zip for this kind of job: 1: dates.Zip(dates.Skip(1), (d1, d2) => d2 - d1); As you might expect, the result will be: { 1 day, 3 days } ......
Today I faced following problem: User picks many files by OpenFileDialog control. They must be loaded asynchronously. Loading some of them may fail. I want to receive callback when loading completes. How can I encapsulate all of this in single repository Load method signature ? After some time I came up with following interface: 1: interface IAsyncResult<out T> 2: { 3: T Result { get; } 4: Exception Error { get; } 5: bool IsCompleted { get; } 6: bool Wait(int timeout); 7: void AttachCallback(Action<IA... ......