C#

Igor Ostrovsky on February 23rd, 2010

The memory model is a fascinating topic – it touches on hardware, concurrency, compiler optimizations, and even math. The memory model defines what state a thread may see when it reads a memory location modified by other threads. For example, if one thread updates a regular non-volatile field, it is possible that another thread reading […]

Continue reading about Volatile keyword in C# – memory model explained

Igor Ostrovsky on April 1st, 2009

Notice that this post was published on April 1, 2009. For decades, computer science students have been taught that so-called NP-hard problems do not have known efficient solutions. These problems include the infamous Travelling salesman problem, subset sum, 3SAT, and many more. But – as is often the case – where theoretical Computer Science failed, […]

Continue reading about Choose expression: proposal for a revolutionary C# construct

Igor Ostrovsky on February 2nd, 2009

Here is a little puzzle for C# developers reading my blog. What is the error in the program below? using System.Collections.Generic; class Program { public static void Main() { int[] arr = new int[10]; IEnumerator<int> e = arr.GetEnumerator(); } }

Continue reading about Puzzling over arrays and enumerators in C#

Igor Ostrovsky on September 23rd, 2008

SelectMany is a fascinating operator in LINQ to Objects. For one thing, it is not as intuitive as most other LINQ operators. MSDN says that SelectMany “projects each element of a sequence to an IEnumerable(T) and flattens the resulting sequences into one sequence.” I still remember reading this description of SelectMany for the first time, […]

Continue reading about One LINQ operator to rule them all

Igor Ostrovsky on September 12th, 2008

I was discussing the little LINQ puzzle with Stephen Toub, and he brought up an idea which lead to another puzzle. I like this one even more than the previous one. Why does the last line throw StackOverflowException? IEnumerable<int> q = new int[] { 1, 2 }; q = from x in new int[] { […]

Continue reading about Another LINQ puzzle

Igor Ostrovsky on September 12th, 2008

Why does the last line hang? IEnumerable<int> empty = Enumerable.Empty<int>(); for (int i = 0; i < 40; i++) { empty = empty.Concat(empty); } int[] emptyArray = empty.ToArray(); Answer in the comments section. For a slightly harder challenge, check out the next puzzle.

Continue reading about Little LINQ puzzle

I realized that there is a very clean way to express a multi-clause if statement by composing ternary conditional operators like this: var result = condition1 ? result1 : condition2 ? result2 : condition3 ? result4 … : conditionN ? resultN : default;

Continue reading about A neat way to express multi-clause if statements in C-based languages

Igor Ostrovsky on May 26th, 2008

In responses to my last week’s post, several readers mentioned LINQ-like operators they implemented themselves. I also had ideas for operators that would lead to neat solutions for some problems, so I decided to give it some thought and collect up the most useful operators into a reusable library. My goal was to include operators […]

Continue reading about Extended LINQ: additional operators for LINQ to objects

Igor Ostrovsky on May 18th, 2008

Ever since I learned about LINQ, I keep discovering new ways to use it to improve my code. Every trick makes my code a little bit faster to write, and a little bit easier to read. This posting summarizes some of the tricks that I came across. I will show you how to use LINQ […]

Continue reading about 7 tricks to simplify your programs with LINQ

Igor Ostrovsky on September 6th, 2007

Today, I am writing about a design problem related to C# generics that I’ve seen arise a few times. The problem occurs when we need to manipulate a generic class given a reference to its non-generic base class. For example, if a generic class Node<T> inherits from a non-generic class Node, and we are holding […]

Continue reading about Fun with C# generics: down-casting to a generic type