<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Igor Ostrovsky Blogging &#187; C#</title>
	<atom:link href="http://igoro.com/archive/category/csharp/feed/" rel="self" type="application/rss+xml" />
	<link>http://igoro.com</link>
	<description>On programming, technology, and random things of interest</description>
	<lastBuildDate>Fri, 23 Jul 2010 05:24:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Volatile keyword in C# – memory model explained</title>
		<link>http://igoro.com/archive/volatile-keyword-in-c-memory-model-explained/</link>
		<comments>http://igoro.com/archive/volatile-keyword-in-c-memory-model-explained/#comments</comments>
		<pubDate>Tue, 23 Feb 2010 09:57:22 +0000</pubDate>
		<dc:creator>Igor Ostrovsky</dc:creator>
				<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://igoro.com/?p=468</guid>
		<description><![CDATA[div.mynote { border: black 1px solid; padding: 10px; background-color: #f0f0ff; margin-top: 20px; margin-bottom: 20px; margin-left: 10px; > 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. [...]]]></description>
			<content:encoded><![CDATA[<style>
div.mynote { border: black 1px solid; padding: 10px; background-color: #f0f0ff; margin-top: 20px; margin-bottom: 20px; margin-left: 10px; ></style>
<p>The <strong>memory model</strong> is a fascinating topic – it touches on hardware, concurrency, compiler optimizations, and even math.</p>
<p>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 the field will <strong>never </strong>observe the new value. This program never terminates (in a release build):</p>
<p>   <span id="more-468"></span>
<pre class="code"><span style="color: blue">class </span><span style="color: #2b91af">Test
</span>{
    <span style="color: blue">private </span><span style="color: blue">bool </span>_loop = <span style="color: blue">true</span>;

    <span style="color: blue">public static void </span>Main()
    {
        <span style="color: #2b91af">Test </span>test1 = <span style="color: blue">new </span><span style="color: #2b91af">Test</span>();

        <span style="color: green">// Set _loop to false on another thread
        </span><span style="color: blue">new </span><span style="color: #2b91af">Thread</span>(() =&gt; { test1._loop = <span style="color: blue">false</span>;}).Start();

        <span style="color: green">// Poll the _loop field until it is set to false
        </span><span style="color: blue">while </span>(test1._loop == <span style="color: blue">true</span>) ;

        <span style="color: green">// The loop above will never terminate!</span>
    }
}</pre>
<p>There are two possible ways to get the while loop to terminate:</p>
<ol>
<li><strong>Use a lock </strong>to protect all accesses (reads and writes) to the _loop field </li>
<li><strong>Mark the _loop field as volatile</strong> </li>
</ol>
<p>There are two reasons why a read of a non-volatile field may observe a stale value: <strong>compiler optimizations</strong> and <strong>processor optimizations</strong>.</p>
<div class="mynote">
<p>In concurrent programming, threads can get interleaved in many different ways, resulting in possibly many different outcomes. But as the example with the infinite loop shows, threads do not just get interleaved – they potentially interact in more complex ways, unless you correctly use locks and volatile fields.</p>
</div>
<h3>Compiler optimizations</h3>
<p>The first reason why a non-volatile read may return a stale value has to do with compiler optimizations. In the infinite loop example, the JIT compiler optimizes the while loop from this:</p>
<pre class="code"><span style="color: blue">while </span>(test1._loop == <span style="color: blue">true</span>) ;</pre>
<p>To this:</p>
<pre class="code"><span style="color: blue">if </span>(test1._loop) { <span style="color: blue">while </span>(<span style="color: blue">true</span>); }</pre>
<p>This is an entirely reasonable transformation if only one thread accesses the _loop field. But, if another thread changes the value of the field, this optimization can prevent the reading thread from noticing the updated value.</p>
<p>If you mark the _loop field as volatile, the compiler will not hoist the read out of the loop. The compiler will know that other threads may be modifying the field, and so it will be careful to avoid optimizations that would result in a read of a stale value.</p>
<div class="mynote">
<p>The code transformation I showed is a close approximation of the optimization done by the CLR JIT compiler, but not completely exact.</p>
<p>The full story is that the assembly code emitted by the JIT compiler will store the value test1._loop in the EAX register. The loop condition will keep polling the register, and will read test1._loop from memory again. Even when the thread is pre-empted, the CPU registers get saved. Once the thread is again scheduled to run, the same stale EAX register value will be restored, and the loop never terminates.</p>
<p>The assembly code generated by the while loop looks as follows:</p>
<pre>00000068  test        eax,eax
0000006a  jne         00000068</pre>
<p>If you make the _loop field volatile, this code is generated instead:</p>
<pre>00000064  cmp         byte ptr [eax+4],0
00000068  jne         00000064</pre>
<p>If the _loop field is not volatile, the compiler will store _loop in the EAX register. If _loop is volatile, the compiler will instead keep the test1 variable in EAX, and the value of _loop will be re-fetched from memory on each access (by “ptr [eax+4]”).</p>
</div>
<div class="mynote">
<p>From my experience playing around with the current version of the CLR, I get the impression that these kinds of compiler optimizations are not terribly frequent. On x86 and x64, often the same assembly code will be generated regardless of whether a field is volatile or not. On IA64, the situation is a bit different &#8211; see the next section.</p>
</div>
<h3>Processor optimizations</h3>
<p>On some processors, not only must the compiler avoid certain optimizations on volatile reads and writes, it also has to use special instructions. On a multi-core machine, different cores have different caches. The processors may not bother to keep those caches coherent by default, and special instructions may be needed to flush and refresh the caches.</p>
<p>The mainstream <strong>x86 </strong>and <strong>x64 </strong>processors implement a strong memory model where memory access is effectively volatile. So, a volatile field forces the compiler to avoid some high-level optimizations like hoisting a read out of a loop, but otherwise results in the same assembly code as a non-volatile read.</p>
<p>The <strong>Itanium </strong>processor implements a weaker memory model. To target Itanium, the JIT compiler has to use special instructions for volatile memory accesses: <strong>LD.ACQ</strong> and <strong>ST.REL</strong>, instead of LD and ST. Instruction LD.ACQ effectively says, “refresh my cache and then read a value” and ST.REL says, “write a value to my cache and then flush the cache to main memory”. LD and ST on the other hand may just access the processor’s cache, which is not visible to other processors.</p>
<div class="mynote">
<p>For the reasons explained in this section and the previous sections, marking a field as volatile will often incur <strong>zero </strong>performance penalty on x86 and x64.</p>
</div>
<div class="mynote">
<p>The x86/x64 instruction set actually does contains three fence instructions: LFENCE, SFENCE, and MFENCE. LFENCE and SFENCE are apparently not needed on the current architecture, but MFENCE is useful to go around one particular issue: if a core reads a memory location it previously wrote, the read may be served from the store buffer, even though the write has not yet been written to memory. <a href="http://software.intel.com/en-us/forums/showthread.php?t=47577">[Source]</a> I don&#8217;t actually know whether the CLR JIT ever inserts MFENCE instructions.</p>
</div>
<h3>Volatile accesses in more depth</h3>
<p>To understand how volatile and non-volatile memory accesses work, you can imagine each thread as having its own cache. Consider a simple example with a <strong>non-volatile </strong>memory location (i.e. a field) <strong>u</strong>, and a <strong>volatile </strong>memory location <strong>v</strong>.</p>
<p>&#160;<img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://igoro.com/wordpress/wp-content/uploads/2010/03/image.png" width="346" height="295" /></p>
<p>A <strong>non-volatile write</strong> could just update the value in the thread’s cache, and not the value in main memory:</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://igoro.com/wordpress/wp-content/uploads/2010/03/image1.png" width="346" height="295" />&#160; </p>
<p>However, in C# <strong>all writes are volatile</strong> (unlike say in Java), regardless of whether you write to a volatile or a non-volatile field. So, the above situation actually never happens in C#.</p>
<p>A <strong>volatile write </strong>updates the thread’s cache, and then flushes the entire cache to main memory. If we were to now set the volatile field v to 11, both values u and v would get flushed to main memory:</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://igoro.com/wordpress/wp-content/uploads/2010/03/image2.png" width="346" height="295" /></p>
<p>Since all C# writes are volatile, you can think of all writes as going straight to main memory.</p>
<p>A regular, <strong>non-volatile read</strong> can read the value from the thread’s cache, rather than from main memory. Despite the fact that thread 1 set u to 11, when thread 2 reads u, it will still see value 10:</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://igoro.com/wordpress/wp-content/uploads/2010/03/image3.png" width="346" height="295" /></p>
<p>When you read a non-volatile field in C#, a non-volatile read occurs, and you may see a stale value from the thread’s cache. Or, you may see the updated value. Whether you see the old or the new value depends on your compiler and your processor.</p>
<p>Finally, let’s take a look at an example of a <strong>volatile read</strong>. Thread 2 will read the volatile field v:</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://igoro.com/wordpress/wp-content/uploads/2010/03/image4.png" width="346" height="294" />&#160;</p>
<p>Before the volatile read, thread 2 refreshes its <strong>entire cache</strong>, and then reads the updated value of v: 11. So, it will observe the value&#160; that is really in main memory, and also refresh its cache as a bonus.</p>
<p>Note that the thread caches that I described are <strong>imaginary </strong>– there really is no such thing as a thread cache. Threads only appear to have these caches as an artifact of compiler and processor optimizations.</p>
<div class="mynote">
<p>One interesting point is that all writes in C# are volatile according to the memory model as documented <a href="http://msdn.microsoft.com/en-us/magazine/cc163715.aspx">here</a> and <a href="http://www.bluebytesoftware.com/blog/CommentView,guid,efdf6840-c071-4c5b-addf-9c3205de7f1d.aspx">here</a>, and are also presumably implemented as such. The ECMA specification of the C# language actually defines a weaker model where writes are not volatile by default.</p>
</div>
<div class="mynote">
<p>You may find it surprising that a volatile read refreshes the <strong>entire </strong>cache, not just the&#160; read value. Similarly, a volatile write (i.e., every C# write) flushes the <strong>entire </strong>cache, not just the written value. These semantics are sometimes referred to as “strong volatile semantics”.</p>
<p>The original Java memory model designed in 1995 was based on weak volatile semantics, but was changed in 2004 to strong volatile. The weak volatile model is very inconvenient. One example of the problem is that the “safe publication” pattern is not safe. Consider this example:</p>
<pre class="code"><span style="color: blue">volatile string</span>[] _args = <span style="color: blue">null</span>;</pre>
<pre class="code"><span style="color: blue">public void </span>Write() {
    <span style="color: blue">string</span>[] a = <span style="color: blue">new string</span>[2];
    a[0] = <span style="color: #a31515">&quot;arg1&quot;</span>;
    a[1] = <span style="color: #a31515">&quot;arg2&quot;</span>;
    _args = a;
    ...
}

<span style="color: blue">public void </span>Read() {
    <span style="color: blue">if </span>(_args != <span style="color: blue">null</span>) {
        <span style="color: green">// Under weak volatile semantics, this assert could fail!</span>
        <span style="color: #2b91af">Debug</span>.Assert(_args[0] != <span style="color: blue">null</span>);
    }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a><font face="Courier New"></font></p>
<p>Under strong volatile semantics (i.e., the .NET and C# volatile semantics), a non-null value in the _args field guarantees that the elements of _args are also not null. The safe publication pattern is very useful and commonly used in practice.</p>
</div>
<h3>Memory model and .NET operations</h3>
<p>Here is a table of how various .NET operations interact with the imaginary thread cache:</p>
<table border="1" cellspacing="0" cellpadding="2" width="689">
<tbody>
<tr>
<td valign="top" width="160"><strong>Construct</strong></td>
<td valign="top" width="120"><strong>Refreshes thread cache before?</strong></td>
<td valign="top" width="104"><strong>Flushes thread cache after?</strong></td>
<td valign="top" width="303"><strong>Notes</strong></td>
</tr>
<tr>
<td valign="top" width="160">Ordinary read</td>
<td valign="top" width="120">No</td>
<td valign="top" width="104">No</td>
<td valign="top" width="303">Read of a non-volatile field</td>
</tr>
<tr>
<td valign="top" width="160">Ordinary write</td>
<td valign="top" width="120">No</td>
<td valign="top" width="104"><strong>Yes</strong></td>
<td valign="top" width="303">Write of a non-volatile field</td>
</tr>
<tr>
<td valign="top" width="160">Volatile read</td>
<td valign="top" width="120"><strong>Yes</strong></td>
<td valign="top" width="104">No</td>
<td valign="top" width="303">Read of volatile field, or Thread.VolatileRead</td>
</tr>
<tr>
<td valign="top" width="160">Volatile write</td>
<td valign="top" width="120">No</td>
<td valign="top" width="104"><strong>Yes</strong></td>
<td valign="top" width="303">Write of a volatile field – same as non-volatile</td>
</tr>
<tr>
<td valign="top" width="160"><a href="http://msdn.microsoft.com/en-us/library/system.threading.thread.memorybarrier.aspx">Thread.MemoryBarrier</a></td>
<td valign="top" width="120"><strong>Yes</strong></td>
<td valign="top" width="104"><strong>Yes</strong></td>
<td valign="top" width="303">Special memory barrier method</td>
</tr>
<tr>
<td valign="top" width="160"><a href="http://msdn.microsoft.com/en-us/library/system.threading.interlocked.aspx">Interlocked</a> operations</td>
<td valign="top" width="120"><strong>Yes</strong></td>
<td valign="top" width="104"><strong>Yes</strong></td>
<td valign="top" width="303">Increment, Add, Exchange, etc.</td>
</tr>
<tr>
<td valign="top" width="160">Lock acquire</td>
<td valign="top" width="120"><strong>Yes</strong></td>
<td valign="top" width="104">No</td>
<td valign="top" width="303"><a href="http://msdn.microsoft.com/en-us/library/de0542zz.aspx">Monitor.Enter</a> or entering a lock {}&#160; region</td>
</tr>
<tr>
<td valign="top" width="160">Lock release</td>
<td valign="top" width="120">No</td>
<td valign="top" width="104"><strong>Yes</strong></td>
<td valign="top" width="303"><a href="http://msdn.microsoft.com/en-us/library/system.threading.monitor.exit.aspx">Monitor.Exit</a> or exiting a lock {} region</td>
</tr>
</tbody>
</table>
<p>&#160;</p>
<p>For each operation, the table shows two things:</p>
<ul>
<li>Is the entire imaginary thread cache <strong>refreshed </strong>from main memory <strong>before </strong>the operation? </li>
<li>Is the entire imaginary thread cache <strong>flushed </strong>to main memory <strong>after </strong>the operation? </li>
</ul>
<h3>Disclaimer and limitations of the model</h3>
<p>This blog post reflects my personal understanding of the .NET memory model, and is based purely on publicly available information.</p>
<p>I find the explanation based on imaginary thread caches more intuitive than the more commonly used explanation based on operation reordering. The thread cache model is also accurate for most intents and purposes.</p>
<p>To be even more accurate, you should assume that the thread caches can form an arbitrary large hierarchy, and so you cannot assume that a read is served only from two possible places – main memory or the thread’s cache. I think that you would have to construct a somewhat of a clever case in order for the cache hierarchy to make a difference, though. If anyone is aware of a case where the hierarchical thread cache model makes a prediction different from the reordering-based model, I would love to hear about it.</p>
<p>If you are interested in the .NET memory model, I encourage you to read <a href="http://msdn.microsoft.com/en-us/magazine/cc163715.aspx">Understand the Impact of Low-Lock Techniques in Multithreaded Apps</a> in the MSDN Magazine, and the <a href="http://blogs.msdn.com/cbrumme/archive/2003/05/17/51445.aspx">Memory model</a> blog post from Chris Brumme.</p>
<div class="mynote">
<p>Read more of my articles:</p>
<p><a href="http://igoro.com/archive/gallery-of-processor-cache-effects/">Gallery of processor cache effects</a></p>
<p><a href="http://igoro.com/archive/what-really-happens-when-you-navigate-to-a-url/">What really happens when you navigate to a URL</a></p>
<p><a href="http://igoro.com/archive/human-heart-is-a-turing-machine-research-on-xbox-360-shows-wait-what/">Human heart is a Turing machine, research on XBox 360 shows. Wait, what?</a></p>
<p><a href="http://igoro.com/archive/7-tricks-to-simplify-your-programs-with-linq/">7 tricks to simplify your programs with LINQ</a></p>
<p>And if you like my blog, <a href="http://igoro.com/feed/">subscribe</a>!</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://igoro.com/archive/volatile-keyword-in-c-memory-model-explained/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Choose expression: proposal for a revolutionary C# construct</title>
		<link>http://igoro.com/archive/choose-expression-proposal-for-a-revolutionary-c-construct/</link>
		<comments>http://igoro.com/archive/choose-expression-proposal-for-a-revolutionary-c-construct/#comments</comments>
		<pubDate>Wed, 01 Apr 2009 09:04:36 +0000</pubDate>
		<dc:creator>Igor Ostrovsky</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Algorithms]]></category>

		<guid isPermaLink="false">http://igoro.com/?p=122</guid>
		<description><![CDATA[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 &#8211; as is often the case &#8211; where theoretical Computer Science failed, [...]]]></description>
			<content:encoded><![CDATA[<p><span style="color: #ff0000;">Notice that this post was published on <strong>April 1</strong>, 2009.</span></p>
<p>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 <a href="http://en.wikipedia.org/wiki/Travelling_salesman_problem">Travelling salesman problem</a>, <a href="http://en.wikipedia.org/wiki/Subset_sum">subset sum</a>, <a href="http://en.wikipedia.org/wiki/3SAT">3SAT</a>, and many more.</p>
<p>But &#8211; as is often the case &#8211; where theoretical Computer Science failed, sound software engineering practices will succeed. By using loosely-coupled OOP, agile methodologies and the model-view-controller architectural pattern, I developed a solution that someone trapped in the world of formulas and big Ohs would never dream of.</p>
<p>Enough with the background, and let&#8217;s take a deep dive into the intriguing design.<br />
<span id="more-122"></span><br />
<strong>Introducing the choose expression</strong></p>
<p>As most other elegant designs, this one is very simple. My proposal calls for a choose expression with this syntax:</p>
<pre class="code">    <span style="color: blue">choose </span>{ boolean_expression1, boolean_expression2 }</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Choose expression is basically the || operator, only with a slight twist. The semantics of the choose expression are similarly simple:</p>
<ol>
<li>If boolean_expression1 or boolean_expression2 will evaluate to true, the runtime will evaluate the true expression, but not the other expression. The return value of the choose expression will be true in this case.</li>
<li>If both expressions will evaluate to false, the runtime will evaluate neither expression. The return value of the choose expression is false in this case.</li>
</ol>
<p>Let&#8217;s look at a few simple usage examples:</p>
<pre class="code"><span style="color: blue">    bool </span>a = <span style="color: blue">choose </span>{
        1 == 2,
        1 &lt; 2
    };</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Variable a will be set to true, because the condition 1 &lt; 2 is true.</p>
<p>Here is another example:</p>
<pre class="code"><span style="color: blue">    bool </span>a = <span style="color: blue">choose </span>{
        ((<span style="color: #2b91af">Func</span>&lt;<span style="color: blue">bool</span>&gt;)(() =&gt; { <span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">"Hello"</span>); <span style="color: blue">return false</span>; }))(),
        1 &lt; 2
    };</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>There is no point executing the first function, because it would return false anyways. So, this code sample does not print anything to screen. Instead, the choose expression will execute the second function. The second expression returns true, so variable a will be set to true.</p>
<p>And another simple one:</p>
<pre class="code"><span style="color: blue">    bool </span>a = <span style="color: blue">choose </span>{
        ((<span style="color: #2b91af">Func</span>&lt;<span style="color: blue">bool</span>&gt;)(() =&gt; { <span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">"Hello1"</span>); <span style="color: blue">return false</span>; })(),
        ((<span style="color: #2b91af">Func</span>&lt;<span style="color: blue">bool</span>&gt;)(() =&gt; { <span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">"Hello2"</span>); <span style="color: blue">return false</span>; })(),
    };</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>This code sample will not be print anything to screen either. It is obvious; why evaluate either of the two functions if they are going to return false anyways? This code simply assigns false to variable a.</p>
<p>Now, let&#8217;s cut to the chase, and use choose expressions to give an efficient implementation of an NP-hard problem. Let&#8217;s look at subset sum:</p>
<pre class="code"><span style="color: blue">    bool </span>SubsetSum(<span style="color: blue">int</span>[] arr)
    {
        <span style="color: blue">return </span>SubsetSumHelper(arr, 0, 0);
    }

<span style="color: blue">    bool </span>SubsetSumHelper(<span style="color: blue">int</span>[] arr, <span style="color: blue">int </span>index, <span style="color: blue">int </span>sumSoFar)
    {
        <span style="color: blue">if </span>(index == arr.Length)
        {
            <span style="color: blue">return </span>sumSoFar == 0;
        }

        <span style="color: blue">return </span><span style="color: blue">choose</span> {
            () =&gt; SubsetSumHelper(arr, index + 1, sumSoFar + arr[index]),
            () =&gt; SubsetSumHelper(arr, index + 1, sumSoFar)
        };
    }</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Yes, that&#8217;s right! An O(N) implementation of the subset sum problem. There you have it, computer scientists. You said it was impossible. If anyone at the University of British Columbia needs my mailing address to send me a refund check for my education, you can find my contact information in the margin.</p>
<p><strong>Under the hood of the choose expression</strong></p>
<p>After a couple hours of coding, I was able to develop a simple prototype. It works perfectly, but since it is only a prototype, I simplified my life a little bit by allowing choose to execute both functions. After all, I don&#8217;t have to slave through all the nitty-gritty details in the initial prototype, right? The performance of my implementation is not that great either, but I haven&#8217;t had the time to fire up the profiler so far. Perhaps I need to unroll a loop somewhere, or ensure that method calls are getting inlined optimally.</p>
<p>To further prove the feasibility of my design, I developed a <a href="http://en.wikipedia.org/wiki/Non-deterministic_turing_machine">non-deterministic Turing machine</a> construction that evaluates choose expressions extremely efficiently.</p>
<p>Non-deterministic Turing machines are known to be a good realistic abstraction of computing hardware; there was a study that proved that. To be exact, the study was only a moderate success. The researchers built a mechanical non-deterministic Turing machine that solved a Travelling Salesperson problem with 5 cities without a hitch. On the 6-city version of the problem, the experiment had to be abruptly interrupted after sprawling machine replicas filled up the room, and the head of one of the researchers got caught in a loop of tape.</p>
<p>So, it is clear that this design is sound. There may be performance issues in the first release, but they will improve as the technology matures. And once CPU manufacturers include a non-deterministic branching instruction in the instruction set, the cost of evaluating the choose expression will drop down to a couple of instructions.</p>
<p><strong>Summary</strong></p>
<p>I don&#8217;t know the detailed plans surrounding the C# language, but if there is a C# 4.1., I would like to see the choose expression included.</p>
<p>And the larger lesson of this post is simple: computer science is largely obsolete in today&#8217;s world of technology. Computer science says that this is impossible, that is impossible&#8230; As you just saw, anything is possible, so long as you have enough paper to print out all the UML diagrams.</p>
]]></content:encoded>
			<wfw:commentRss>http://igoro.com/archive/choose-expression-proposal-for-a-revolutionary-c-construct/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Puzzling over arrays and enumerators in C#</title>
		<link>http://igoro.com/archive/puzzling-over-arrays-and-enumerators-in-c/</link>
		<comments>http://igoro.com/archive/puzzling-over-arrays-and-enumerators-in-c/#comments</comments>
		<pubDate>Mon, 02 Feb 2009 22:59:08 +0000</pubDate>
		<dc:creator>Igor Ostrovsky</dc:creator>
				<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://igoro.com/archive/puzzling-over-arrays-and-enumerators-in-c/</guid>
		<description><![CDATA[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&#60;int&#62; e = arr.GetEnumerator(); } } If you don&#8217;t see it, don&#8217;t worry.&#160; I was surprised by this C# behavior as [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a little puzzle for C# developers reading my blog. What is the error in the program below?</p>
<pre class="code"><span style="color: blue">   using </span>System.Collections.Generic;
<span style="color: blue"></span><span style="color: blue">   class </span><span style="color: #2b91af">Program
</span>   {
       <span style="color: blue">public static void </span>Main()
       {
           <span style="color: blue">int</span>[] arr = <span style="color: blue">new int</span>[10];
           <span style="color: #2b91af">IEnumerator</span>&lt;<span style="color: blue">int</span>&gt; e = arr.GetEnumerator();
       }
   }</pre>
<p><span id="more-86"></span></p>
<p>If you don&#8217;t see it, don&#8217;t worry.&#160; I was surprised by this C# behavior as well. Just come back in a couple days to see the solution. Or try to compile the program in Visual Studio. <img src='http://igoro.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p><strong>UPDATE: </strong>Finally, I am back with a solution.</p>
<p>To understand what’s going on, let’s take a look at a fixed version of the code:</p>
<pre class="code"><span style="color: blue">using </span>System.Collections.Generic;
<span style="color: blue">class </span><span style="color: #2b91af">Program
</span>{
    <span style="color: blue">public static void </span>Main()
    {
        <span style="color: blue">int</span>[] arr = <span style="color: blue">new int</span>[10];
        <span style="color: #2b91af">IEnumerator</span>&lt;<span style="color: blue">int</span>&gt; e = ((<span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: blue">int</span>&gt;)arr).GetEnumerator();
    }
}</pre>
<p>The problem of the original code sample is that the GetEnumerator() method on arrays returns a non-generic IEnumerator. That’s because .NET arrays had GetEnumerator() even before there were generics in .NET.</p>
<p>When generics got introduced in .NET 2.0, arrays got another GetEnumerator() method, one that returns a generic IEnumerator&lt;&gt;. In order to have two GetEnumerator() methods with different return types, one of them had to be a part of an <a href="http://msdn.microsoft.com/en-us/library/aa288461(VS.71).aspx">explicit interface implementation</a>.</p>
<p>As a result, arr.GetEnumerator() binds to a method that returns a non-generic IEnumerator, and IEnumerable&lt;int&gt;)arr).GetEnumerator() binds to a method that returns a generic IEnumerator&lt;int&gt;. Somewhat surprising, but understandable.</p>
<p>Disclaimer: this explanation is based on public information and my guesses, and should not be considered official, authoritative, or anything like that.</p>
]]></content:encoded>
			<wfw:commentRss>http://igoro.com/archive/puzzling-over-arrays-and-enumerators-in-c/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>One LINQ operator to rule them all</title>
		<link>http://igoro.com/archive/one-linq-operator-to-rule-them-all/</link>
		<comments>http://igoro.com/archive/one-linq-operator-to-rule-them-all/#comments</comments>
		<pubDate>Tue, 23 Sep 2008 07:52:46 +0000</pubDate>
		<dc:creator>Igor Ostrovsky</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://igoro.com/archive/one-linq-operator-to-rule-them-all/</guid>
		<description><![CDATA[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 &#8220;projects each element of a sequence to an IEnumerable(T) and flattens the resulting sequences into one sequence.&#8221; I still remember reading this description of SelectMany for the first time, [...]]]></description>
			<content:encoded><![CDATA[<p>SelectMany is a fascinating operator in LINQ to Objects. For one thing, it is not as intuitive as most other LINQ operators. MSDN <a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.selectmany.aspx">says</a> that SelectMany &#8220;projects each element of a sequence to an IEnumerable(T) and flattens the resulting sequences into one sequence.&#8221; I still remember reading this description of SelectMany for the first time, and wondering why that would that be useful.</p>
<p>Of course, SelectMany is not only incredibly useful, but also surprisingly powerful. In fact, a variety of LINQ operators are really just constrained versions SelectMany. Select, Concat, Where, Take, Skip, TakeWhile, SkipWhile and Distinct can all be easily rewritten using a single SelectMany.</p>
<p><span id="more-80"></span></p>
<p>To get started, let&#8217;s compare SelectMany with Select. SelectMany projects each element into some number of elements. In comparison, Select projects each element into exactly one element. Since SelectMany is more general than Select, it can be used to implement Select:</p>
<pre class="code"><span style="color: blue">public static </span><span style="color: #2b91af">IEnumerable</span>&lt;U&gt; Select&lt;T, U&gt;(
    <span style="color: blue">this </span><span style="color: #2b91af">IEnumerable</span>&lt;T&gt; source,
    <span style="color: #2b91af">Func</span>&lt;T, U&gt; func)
{
    <span style="color: blue">return </span>source.SelectMany(x =&gt; <span style="color: #2b91af">Enumerable</span>.Repeat(func(x), 1));
}</pre>
<p>But, Select is not the only operator less general SelectMany. Similarly, you can think of the Where operator as producing zero or one element for each element in the sequence. So, Where can also be easily implemented using SelectMany:</p>
<pre class="code"><span style="color: blue">public static </span><span style="color: #2b91af">IEnumerable</span>&lt;T&gt; Where&lt;T&gt;(
    <span style="color: blue">this </span><span style="color: #2b91af">IEnumerable</span>&lt;T&gt; source,
    <span style="color: #2b91af">Func</span>&lt;T, <span style="color: blue">bool</span>&gt; filter)
{
    <span style="color: blue">return </span>source.SelectMany(x =&gt; <span style="color: #2b91af">Enumerable</span>.Repeat(x, filter(x) ? 1 : 0));
}</pre>
<p>Another operator that can be easily implemented with SelectMany is Concat:</p>
<pre class="code"><span style="color: blue">public static </span><span style="color: #2b91af">IEnumerable</span>&lt;T&gt; Concat&lt;T&gt;(
    <span style="color: blue">this </span><span style="color: #2b91af">IEnumerable</span>&lt;T&gt; source1,
    <span style="color: #2b91af">IEnumerable</span>&lt;T&gt; source2)
{
    <span style="color: blue">return new int</span>[] { 0, 1 }
        .SelectMany(
            x =&gt; x == 0 ? source1 : source2);
}</pre>
<p>Take and Skip can be implemented using the SelectMany variant that passes indices into the user delegate:</p>
<pre class="code"><span style="color: blue">public static </span><span style="color: #2b91af">IEnumerable</span>&lt;T&gt; Take&lt;T&gt;(
    <span style="color: blue">this </span><span style="color: #2b91af">IEnumerable</span>&lt;T&gt; source,
    <span style="color: blue">int </span>toTake)
{
    <span style="color: blue">return </span>source.SelectMany((x, i) =&gt; <span style="color: #2b91af">Enumerable</span>.Repeat(x, i &lt; toTake ? 1 : 0));
}

<span style="color: blue">public static </span><span style="color: #2b91af">IEnumerable</span>&lt;T&gt; Skip&lt;T&gt;(
    <span style="color: blue">this </span><span style="color: #2b91af">IEnumerable</span>&lt;T&gt; source,
    <span style="color: blue">int </span>toSkip)
{
    <span style="color: blue">return </span>source.SelectMany((x, i) =&gt; <span style="color: #2b91af">Enumerable</span>.Repeat(x, i &gt;= toSkip ? 1 : 0));
}</pre>
<p>And finally, with a few closure tricks, we can even implement TakeWhile, SkipWhile and Distinct:</p>
<pre class="code"><span style="color: blue">public static </span><span style="color: #2b91af">IEnumerable</span>&lt;T&gt; TakeWhile&lt;T&gt;(
    <span style="color: blue">this </span><span style="color: #2b91af">IEnumerable</span>&lt;T&gt; source,
    <span style="color: #2b91af">Func</span>&lt;T, <span style="color: blue">bool</span>&gt; func)
{
    <span style="color: blue">bool </span>stopped = <span style="color: blue">false</span>;
    <span style="color: blue">return </span>source.SelectMany((x, i) =&gt; <span style="color: #2b91af">Enumerable</span>.Repeat(x, (!stopped &amp;&amp; (stopped = !func(x))) ? 1 : 0));
}

<span style="color: blue">public static </span><span style="color: #2b91af">IEnumerable</span>&lt;T&gt; SkipWhile&lt;T&gt;(
    <span style="color: blue">this </span><span style="color: #2b91af">IEnumerable</span>&lt;T&gt; source,
    <span style="color: #2b91af">Func</span>&lt;T, <span style="color: blue">bool</span>&gt; func)
{
    <span style="color: blue">bool </span>started = <span style="color: blue">false</span>;
    <span style="color: blue">return </span>source.SelectMany((x, i) =&gt; <span style="color: #2b91af">Enumerable</span>.Repeat(x, (started || (started = func(x))) ? 1 : 0));
}

<span style="color: blue">public static </span><span style="color: #2b91af">IEnumerable</span>&lt;T&gt; Distinct&lt;T&gt;(
    <span style="color: blue">this </span><span style="color: #2b91af">IEnumerable</span>&lt;T&gt; source)
{
    <span style="color: blue">var </span>dict = <span style="color: blue">new </span><span style="color: #2b91af">Dictionary</span>&lt;T, <span style="color: blue">int</span>&gt;();
    <span style="color: blue">return </span>source.SelectMany((x) =&gt; {
        <span style="color: blue">if </span>(!dict.ContainsKey(x))
        {
            dict.Add(x, 0);
            <span style="color: blue">return new </span>T[] { x };
        }
        <span style="color: blue">return new </span>T[] { };
    });
}</pre>
<p>If you enjoyed this article, check out these <a href="http://igoro.com/archive/little-linq-puzzle/">LINQ</a> <a href="http://igoro.com/archive/another-linq-puzzle/">puzzles</a>. Also, read my article on the <a title="7 tricks to simplify your programs with LINQ" href="http://igoro.com/archive/7-tricks-to-simplify-your-programs-with-linq/">7 tricks to simplify your programs with LINQ</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://igoro.com/archive/one-linq-operator-to-rule-them-all/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Another LINQ puzzle</title>
		<link>http://igoro.com/archive/another-linq-puzzle/</link>
		<comments>http://igoro.com/archive/another-linq-puzzle/#comments</comments>
		<pubDate>Fri, 12 Sep 2008 21:17:50 +0000</pubDate>
		<dc:creator>Igor Ostrovsky</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://igoro.com/archive/another-linq-puzzle/</guid>
		<description><![CDATA[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&#60;int&#62; q = new int[] { 1, 2 }; q = from x in new int[] { [...]]]></description>
			<content:encoded><![CDATA[<p>I was discussing the <a href="http://igoro.com/archive/little-linq-puzzle/">little LINQ puzzle</a> with <a href="http://blogs.msdn.com/toub/">Stephen Toub</a>, and he brought up an idea which lead to another puzzle. I like this one even more than the previous one.</p>
<p>Why does the last line throw StackOverflowException?</p>
<pre class="code"><span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: blue">int</span>&gt; q = <span style="color: blue">new int</span>[] { 1, 2 };
q = <span style="color: blue">from </span>x <span style="color: blue">in new int</span>[] { 1, 2 }
    <span style="color: blue">from </span>y <span style="color: blue">in </span>q
    <span style="color: blue">select </span>x + y;
q.ToArray();</pre>
<p><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a></p>
<p>And, how come the code sample runs just fine if you switch the order of the from clauses?</p>
]]></content:encoded>
			<wfw:commentRss>http://igoro.com/archive/another-linq-puzzle/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Little LINQ puzzle</title>
		<link>http://igoro.com/archive/little-linq-puzzle/</link>
		<comments>http://igoro.com/archive/little-linq-puzzle/#comments</comments>
		<pubDate>Fri, 12 Sep 2008 08:36:37 +0000</pubDate>
		<dc:creator>Igor Ostrovsky</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://igoro.com/archive/little-linq-puzzle/</guid>
		<description><![CDATA[Why does the last line hang? IEnumerable&#60;int&#62; empty = Enumerable.Empty&#60;int&#62;(); for (int i = 0; i &#60; 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.]]></description>
			<content:encoded><![CDATA[<p>Why does the last line hang?</p>
<pre class="code"><span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: blue">int</span>&gt; empty = <span style="color: #2b91af">Enumerable</span>.Empty&lt;<span style="color: blue">int</span>&gt;();
<span style="color: blue">for </span>(<span style="color: blue">int </span>i = 0; i &lt; 40; i++)
{
    empty = empty.Concat(empty);
}
<span style="color: blue">int</span>[] emptyArray = empty.ToArray();</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Answer in the comments section. <img src='http://igoro.com/wordpress/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>For a slightly harder challenge, check out the <a href="http://igoro.com/archive/another-linq-puzzle/">next puzzle</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://igoro.com/archive/little-linq-puzzle/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>A neat way to express multi-clause if statements in C-based languages</title>
		<link>http://igoro.com/archive/a-neat-way-to-express-multi-clause-if-statements-in-c-based-languages/</link>
		<comments>http://igoro.com/archive/a-neat-way-to-express-multi-clause-if-statements-in-c-based-languages/#comments</comments>
		<pubDate>Mon, 02 Jun 2008 06:49:30 +0000</pubDate>
		<dc:creator>Igor Ostrovsky</dc:creator>
				<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://igoro.com/archive/a-neat-way-to-express-multi-clause-if-statements-in-c-based-languages/</guid>
		<description><![CDATA[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; Traditionally, this would be written in a much more verbose way: MyType [...]]]></description>
			<content:encoded><![CDATA[</p>
<style>
<p><!--
   div.from_the_readers {
      border: #ccc 1px solid;
      padding: 0px 10px;
      background: #ffb;
   }
   span.from_the_readers {
      font-weight: bold;
   }
--></style>
<p style="margin-top: 0px">I realized that there is a very clean way to express a multi-clause if statement by composing ternary conditional operators like this:</p>
<pre class="code"><span style="color: blue">var </span>result =
    <em>condition1</em> ? <em>result1</em>
    : <em>condition2</em> ? <em>result2</em>
    : <em>condition3</em> ? <em>result4</em>
          ...
    : <em>conditionN</em> ? <em>resultN</em>
    : <em>default</em>;</pre>
<p><span id="more-62"></span></p>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Traditionally, this would be written in a much more verbose way:</p>
<p><a href="http://11011.net/software/vspaste"></a></p>
<pre class="code"><span style="color: #2b91af">MyType </span>result;
<span style="color: blue">if </span>(<em>condition1</em>) result = <em>result1</em>;
<span style="color: blue">else if </span>(<em>condition2</em>) result = <em>result2</em>;
<span style="color: blue">else if </span>(<em>condition3</em>) result = <em>result3</em>;
   ...
<span style="color: blue">else if </span>(<em>conditionN</em>) result = <em>resultN</em>;
<span style="color: blue">else </span>result = <em>default</em>;</pre>
<p>Here is a simple real-world application of this trick:</p>
<pre class="code"><span style="color: blue">string </span>commentCount =
    n == 0 ? <span style="color: #a31515">&quot;no comments&quot;
    </span>: n == 1 ? <span style="color: #a31515">&quot;1 comment&quot;
    </span>: n &lt; 100 ? n + <span style="color: #a31515">&quot; comments&quot;
    </span>: <span style="color: #a31515">&quot;100+ comments&quot;</span>;</pre>
<p>I really like this pattern because the code is very concise and clean. I am surprised that I have never seen it used anywhere.</p>
<div class="from_the_readers"><span class="from_the_readers">From the readers</span> </p>
<p><a href="http://iwebthereforeiam.com/">Hugh Brown</a> suggests an alternative way to rewrite the above code sample, which also nests conditional expressions:</p>
<pre class="code"><span style="color: blue">string </span>commentCount =
    <span style="color: blue">string</span>.Format(<span style="color: #a31515">&quot;{0} comment%s&quot;</span>,
        (n == 0 ? <span style="color: #a31515">&quot;no&quot;
        </span>: n &lt; 100 ? n.ToString()
        : <span style="color: #a31515">&quot;100+&quot;</span>),
        (n == 1 ? <span style="color: #a31515">&quot;&quot; </span>: <span style="color: #a31515">&quot;s&quot;</span>));</pre>
</div>
<p><strong>Gotchas</strong></p>
<p>Suprisingly, I don&#8217;t believe there are any major ones. The conditional operator has a very low operator precedence in <a href="http://msdn.microsoft.com/en-us/library/aa691323(VS.71).aspx">C#</a>, <a href="http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html">Java</a> and <a href="http://www.cppreference.com/operator_precedence.html">C++</a>. In C# and Java, only the assignment operators (=, +=, &lt;&lt;=, etc) have a lower precedence than the conditional. In C++, you also have to be cautious around the comma operator, but you should be using that construct rarely anyways.</p>
<p>If you really want to mix the switch expression with assignment operators, other conditionals, or even the C++ comma operator, use brackets to ensure that the conditional operators which are part of the switch expression will be applied last.</p>
<p>In all other cases, the pattern should behave as you&#8217;d expect.</p>
<p><strong>Comments and Conclusion</strong></p>
<p>It is great to find a neat trick in the good old C-based languages. Not only functional languages are cool. <img src='http://igoro.com/wordpress/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>Any thoughts? Has anyone seen this pattern before? Let me know in the <a href="http://igoro.com/archive/a-neat-way-to-express-multi-clause-if-statements-in-c-based-languages/#comments">comments</a>.</p>
<div class="from_the_readers"><span class="from_the_readers">From the readers</span> </p>
<p>Bodaniel Jeanes mentions a loosely-related trick with a switch statement. Note that this works in C, but not in C# or Java:</p>
<pre class="code"><span style="color: blue">switch </span>(<span style="color: blue">true</span>)
{
    <span style="color: blue">case </span>n == 0:
        <span style="color: green">// do something
        </span><span style="color: blue">break</span>;
    <span style="color: blue">case </span>n &gt; 2:
        <span style="color: green">// do something else
        </span><span style="color: blue">break</span>;
    <span style="color: blue">default</span>:
        <span style="color: blue">return</span>;
}</pre>
</div>
<p><a style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2figoro.com%2farchive%2fa-neat-way-to-express-multi-clause-if-statements-in-c-based-languages%2f"><img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2figoro.com%2farchive%2fa-neat-way-to-express-multi-clause-if-statements-in-c-based-languages%2f" border="0" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://igoro.com/archive/a-neat-way-to-express-multi-clause-if-statements-in-c-based-languages/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>Extended LINQ: additional operators for LINQ to objects</title>
		<link>http://igoro.com/archive/extended-linq-additional-operators-for-linq-to-objects/</link>
		<comments>http://igoro.com/archive/extended-linq-additional-operators-for-linq-to-objects/#comments</comments>
		<pubDate>Mon, 26 May 2008 08:13:00 +0000</pubDate>
		<dc:creator>Igor Ostrovsky</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://igoro.com/archive/extended-linq-additional-operators-for-linq-to-objects/</guid>
		<description><![CDATA[In responses to my last week&#8217;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 [...]]]></description>
			<content:encoded><![CDATA[<p>In responses to my last week&#8217;s <a href="http://igoro.com/archive/7-tricks-to-simplify-your-programs-with-linq/">post</a>, 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.</p>
<p>My goal was to include operators that are simple to use, but applicable to a broad range of problems. I&#160; left out operators that I thought were either too complicated to use, or too specific to a particular problem domain.</p>
<p>You can download the full source code of the library <a href="http://igoro.com/wordpress/wp-content/uploads/2008/05/extendedenumerablecs.txt" target="_blank">here</a> (rename the file to ExtendedEnumerable.cs). Read on to find out what it contains.</p>
<p><span id="more-56"></span></p>
<p><strong>ReadLinesFrom, WriteLinesTo &#8211; I/O in LINQ queries</strong></p>
<p>LINQ is a great programming model for simple file-processing tasks. Treating a file as an enumerable of lines, we can filter, transform and analyze it using various LINQ operators. To support this use case, my library includes several operators to convert between streams and line enumerables. Two most general overloads are ReadLinesFrom and WriteLinesTo, which have the following signatures:</p>
<pre class="code"><span style="color: blue">public static </span><span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: blue">string</span>&gt; ReadLinesFrom(<span style="color: #2b91af">TextReader </span>reader)</pre>
<pre class="code"><span style="color: blue">public static void </span>WriteLinesTo(    <span style="color: blue">this </span><span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: blue">string</span>&gt; lines, <span style="color: #2b91af">TextWriter </span>writer)</pre>
<p>However, in most cases you will want to use one of the more specific overloads, ReadLinesFromConsole, ReadLinesFromFile, WriteLinesToConsole and WriteLinesToFile. For example, the Grep method below reads a file, keeps only lines that contain a particular substring, and writes out the results into another file:</p>
<pre class="code"><span style="color: blue">static void </span>Grep(<span style="color: blue">string </span>inputFile, <span style="color: blue">string </span>outputFile, <span style="color: blue">string </span>substring)
{
    <span style="color: #2b91af">ExtendedEnumerable</span>.ReadLinesFromFile(inputFile)
        .Where(line =&gt; line.Contains(substring))
        .WriteLinesToFile(outputFile);
}</pre>
<p>Isn&#8217;t that neat?</p>
<p><strong>Generate &#8211; generate a sequence from a user delegate </strong></p>
<p>In C# 2, generating arbitrary sequences became much more convenient than it used to be in C# 1. Instead of implementing two classes, the IEnumerable&lt;T&gt; and the IEnumerator&lt;T&gt;, you can implement a single method that yields items using the iterator block syntax (i.e. the yield statements).</p>
<p>However, I still try to avoid creating a method just to generate a simple sequence, particularly if I use that sequence only in one place in my program. The Generate operator below accepts a delegate which generates the sequence element by element. To signal the end of the sequence, the generator returns null.</p>
<p>Since value types cannot be null, we need one overload for reference types, and another overload that uses a nullable wrapper to handle value types:</p>
<pre class="code"><span style="color: blue">public static </span><span style="color: #2b91af">IEnumerable</span>&lt;T&gt; Generate&lt;T&gt;(<span style="color: #2b91af">Func</span>&lt;T&gt; generator)    <span style="color: blue">where </span>T : <span style="color: blue">class</span></pre>
<pre class="code"><span style="color: blue">public static </span><span style="color: #2b91af">IEnumerable</span>&lt;T&gt; Generate&lt;T&gt;(<span style="color: #2b91af">Func</span>&lt;<span style="color: #2b91af">Nullable</span>&lt;T&gt;&gt; generator)    <span style="color: blue">where </span>T : <span style="color: blue">struct</span></pre>
<p>To give a usage example, the ReadLinesFromConsole operator I mentioned above could be implemented as follows:</p>
<pre class="code"><span style="color: blue">public static </span><span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: blue">string</span>&gt; ReadLinesFromConsole()
{
    <span style="color: blue">return </span><span style="color: #2b91af">ExtendedEnumerable</span>.Generate(() =&gt; <span style="color: #2b91af">Console</span>.ReadLine());
}</pre>
<p><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a></p>
<p>As another example, this code sample generates an infinite sequence of random integers:</p>
<pre class="code"><span style="color: #2b91af">Random </span>rand = <span style="color: blue">new </span><span style="color: #2b91af">Random</span>();<span style="color: blue">var </span>randomSeq = <span style="color: #2b91af">ExtendedEnumerable</span>.Generate(() =&gt; (<span style="color: blue">int</span>?)rand.Next());</pre>
<p>This Generate operator has two disadvantages. First, it cannot be used to generate sequences that contain null values, because null is the terminator of the sequence. Second, it is a bit annoying to have to use the cast in the value-type overload (see the cast to int? in the random-sequence example). These are minor disadvantages, though, and I much prefer using the Generate operator over implementing a new method each time I need to generate a simple sequence.</p>
<p>As a side note, apparently Jon Skeet also looked at the problem of generating a sequence from a user&#8217;s delegate, and came up with a similar but slightly different solution, which you can find <a href="http://www.eggheadcafe.com/tutorials/aspnet/159e4793-6b17-4e89-bd94-3bde8a5f2d50/iterators-iterator-block.aspx">here</a>.</p>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p><strong>ForEach &#8211; execute an action for each element in the sequence</strong></p>
<p>As has been suggested by <a href="http://blog.noop.se/">Magnus Martensson</a> in a comment to my previous <a href="http://igoro.com/archive/7-tricks-to-simplify-your-programs-with-linq">posting</a>, as well as by others <a href="http://bartdesmet.net/blogs/bart/archive/2008/02/26/the-missing-operator-foreach.aspx">elsewhere</a>, it is often neat to be able to specify an action at the end of the query using a ForEach operator, rather than having to iterate over the query in a foreach statement.</p>
<p>So, instead of this:</p>
<pre class="code"><span style="color: blue">foreach </span>(<span style="color: blue">int </span>x <span style="color: blue">in </span><span style="color: #2b91af">Enumerable</span>.Range(0,10).Where(i =&gt; (i % 2 == 0)).Take(5))
{
    <span style="color: #2b91af">Console</span>.WriteLine(x);
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>You can write this:</p>
<pre class="code"><span style="color: #2b91af">Enumerable</span>.Range(0,10).Where(i =&gt; (i % 2 == 0)).Take(5)
.ForEach(i =&gt; <span style="color: #2b91af">Console</span>.WriteLine(i));</pre>
<p><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a></p>
<p><strong>Do &#8211; execute side effects in the middle of the query</strong></p>
<p>Sometimes it is useful to add side-effects in the middle of query, rather than to the end. For example, we can log which elements have been processed at a particular stage of the query. The Do operator provides this functionality:</p>
<pre class="code"><span style="color: #2b91af">Enumerable</span>.Range(0,10)
    .Do((e) =&gt; <span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">&quot;Processing {0}&quot;</span>, e))
    .Select(x =&gt; x*2).ToArray();</pre>
<p><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a></p>
<p><strong>Combine &#8211; combine two sequences</strong></p>
<p>The Combine operator exists in various functional languages including F#, sometimes under the name Zip or ZipWith. It accepts two sequences as inputs, and combines their elements into a single sequence. So, the first element in sequence 1 and the first element in sequence 2 will be combined to produce the first element in the output sequence, and so forth. The function which combines an element from one sequence with an element from the other sequence is provided by the user. If one of the sequences is longer, the remaining elements in the longer sequence will be ignored.</p>
<p>To compute the pairwise sum between elements in seq1 and seq2, use the Combine operator like this:</p>
<pre class="code"><span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: blue">int</span>&gt; sumSeq = seq1.Combine(seq2, (a, b) =&gt; a + b);</pre>
<p><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a></p>
<p>As another example, to check whether a sequence of integers seq is increasing, use this query:</p>
<pre class="code"><span style="color: blue">bool </span>isIncreasing = seq.Combine(seq.Skip(1), (a, b) =&gt; a &lt; b).All(x =&gt; x);</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p><strong>ToStringPretty &#8211; convert a sequence to a delimited string</strong></p>
<p>Converting a sequence to a nicely-formatted string is a bit of a pain. The String.Join method definitely helps, but unfortunately it accepts an array of strings, so it does not compose with LINQ very nicely.</p>
<p>My library includes several overloads of the ToStringPretty operator that hides the uninteresting code. Here is an example of use:</p>
<pre class="code"><span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #2b91af">Enumerable</span>.Range(0, 10).ToStringPretty(<span style="color: #a31515">&quot;From 0 to 9: [&quot;</span>, <span style="color: #a31515">&quot;,&quot;</span>, <span style="color: #a31515">&quot;]&quot;</span>));</pre>
<p><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a></p>
<p>The output of this program is:</p>
<pre class="code">From 0 to 9: [0,1,2,3,4,5,6,7,8,9]</pre>
<p><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a></p>
<p><strong>FromEnumerator &#8211; convert an enumerator to an enumerable</strong></p>
<p>Several times I got into a situation where I have an enumerator, but really need an enumerable instead. There does not seem to be a simple way to do the conversion in .Net. Hence, my library of operators includes FromEnumerator which accepts an enumerator and returns an enumerable.</p>
<p>This sample converts enumerator e1 into an enumerable and then iterates over it in a foreach statement:</p>
<pre class="code"><span style="color: blue">foreach </span>(<span style="color: blue">int </span>x inExtendedEnumerable.FromEnumerator(e1)) { ... }</pre>
<p><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a></p>
<p><span style="color: green"><font color="#000000"></font></span></p>
<p><span style="color: green"><font color="#000000">And this sample converts enumerator e2 into an enumerable to use it as a data source in a LINQ query:</font></span></p>
<pre class="code"><span style="color: blue">var </span>query = <span style="color: blue">from </span>x <span style="color: blue">in </span><span style="color: #2b91af">ExtendedEnumerable</span>.FromEnumerator(e2)
            <span style="color: blue">where </span>x % 2 == 0
            <span style="color: blue">select </span>x;</pre>
<p><strong>Single &#8211; convert an item to an enumerable</strong></p>
<p>As I mentioned in my previous posting, I have found converting a single item to an enumerable to be a fairly frequent operation. So, my library includes an operator for the conversion:</p>
<pre class="code"><span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: blue">int</span>&gt; e = <span style="color: #2b91af">ExtendedEnumerable</span>.Single(5);</pre>
<p><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a><font face="Courier New"></font><a href="http://11011.net/software/vspaste"></a></p>
<p><strong>Shuffle &#8211; randomly shuffle a sequence</strong></p>
<p>I find myself regularly re-implementing the Shuffle operator when I am testing my code. Shuffle operator accepts a sequence and returns the same sequence, randomly rearranged.</p>
<p>This example prints digits 0..9 in a random order:</p>
<pre class="code"><span style="color: #2b91af">Enumerable</span>.Range(0, 10).Shuffle().WriteLinesToConsole();</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p><strong>Comments and Conclusion</strong></p>
<p>Again, the source code is available for download <a href="http://igoro.com/wordpress/wp-content/uploads/2008/05/extendedenumerablecs.txt" target="_blank">here</a>. If there operators that I haven&#8217;t included, but you think they are useful, let me know in the comments!</p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2figoro.com%2farchive%2fextended-linq-additional-operators-for-linq-to-objects%2f"><img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2figoro.com%2farchive%2fextended-linq-additional-operators-for-linq-to-objects%2f" border="0" /></a></p>
<p><strong>Related:</strong></p>
<ul>
<li><a title="Permanent Link- 7 tricks to simplify your programs with LINQ" href="http://igoro.com/archive/7-tricks-to-simplify-your-programs-with-linq/">7 tricks to simplify your programs with LINQ</a> [igoro.com] </li>
<li><a href="http://mikehadlow.blogspot.com/2008/02/never-write-for-loop-again-fun-with.html">Never write a for loop again</a> [mikehadlow.blogspot.com] </li>
<li><a href="http://bartdesmet.net/blogs/bart/archive/2008/02/26/the-missing-operator-foreach.aspx">The missing operator &#8211; ForEach</a> [bartdesmet.net] </li>
<li><a href="http://www.amazon.com/gp/product/1933988169?ie=UTF8&amp;tag=igorostrblog-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=1933988169">LINQ in Action</a> [Book by Fabrice Marguerie] </li>
<li><a href="http://www.amazon.com/gp/product/1933988363?ie=UTF8&amp;tag=igorostrblog-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=1933988363">C# in Depth: What you need to master C# 2 and 3</a> [Book by Jon Skeet] </li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://igoro.com/archive/extended-linq-additional-operators-for-linq-to-objects/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>7 tricks to simplify your programs with LINQ</title>
		<link>http://igoro.com/archive/7-tricks-to-simplify-your-programs-with-linq/</link>
		<comments>http://igoro.com/archive/7-tricks-to-simplify-your-programs-with-linq/#comments</comments>
		<pubDate>Sun, 18 May 2008 22:54:59 +0000</pubDate>
		<dc:creator>Igor Ostrovsky</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://igoro.com/archive/7-tricks-to-simplify-your-programs-with-linq/</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>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.</p>
<p>This posting summarizes some of the tricks that I came across. I will show you how to use LINQ to:</p>
<ul>
<li><a href="http://igoro.com/archive/7-tricks-to-simplify-your-programs-with-linq/#Tip1">Initialize an array</a> </li>
<li><a href="http://igoro.com/archive/7-tricks-to-simplify-your-programs-with-linq/#Tip2">Iterate over multiple arrays in a single loop</a> </li>
<li><a href="http://igoro.com/archive/7-tricks-to-simplify-your-programs-with-linq/#Tip3">Generate a random sequence</a> </li>
<li><a href="http://igoro.com/archive/7-tricks-to-simplify-your-programs-with-linq/#Tip4">Generate a string</a> </li>
<li><a href="http://igoro.com/archive/7-tricks-to-simplify-your-programs-with-linq/#Tip5">Convert sequences or collections</a> </li>
<li><a href="http://igoro.com/archive/7-tricks-to-simplify-your-programs-with-linq/#Tip6">Convert a value to a sequence of length 1</a> </li>
<li><a href="http://igoro.com/archive/7-tricks-to-simplify-your-programs-with-linq/#Tip7">Iterate over all subsets of a sequence</a> </li>
</ul>
<div id='extendedEntryBreak' name='extendedEntryBreak'></div>
<p>If you have your own bag of LINQ tricks, please share them in the comments! Also, if you like this article, you may like my next article, <a href="http://igoro.com/archive/extended-linq-additional-operators-for-linq-to-objects/">Extended LINQ: additional operators for LINQ to objects</a>.</p>
<p><strong>1. Initialize an array</strong><a name="Tip1">&#160;</a></p>
<p>Often, you need to initialize elements of an array to either the same value, or to an increasing sequence values, or possibly to a sequence increasing or decreasing by a step different from one. With LINQ, you can do all of this within the array initializer &#8211; no for loops necessary!</p>
<p>In the following code sample, the first line initializes a to an array of length 10 with all elements set to -1, the second line initializes b to (0,1,..9), and the third line initializes c to (100,110,&#8230;,190):</p>
<pre class="code"><span style="color: blue">int</span>[] a = <span style="color: #2b91af">Enumerable</span>.Repeat(-1, 10).ToArray();
<span style="color: blue">int</span>[] b = <span style="color: #2b91af">Enumerable</span>.Range(0, 10).ToArray();
<span style="color: blue">int</span>[] c = <span style="color: #2b91af">Enumerable</span>.Range(0, 10).Select(i =&gt; 100 + 10 * i).ToArray();</pre>
<p>A word of caution: if you are initializing large arrays, you may want to forego the elegance and use the old-fashioned for loop instead. The LINQ solution will grow the array dynamically, so garbage arrays will need to be collected by the runtime. That said, I use this trick all the time when initializing small arrays, or in testing/debugging code.</p>
<p><strong>2. Iterate over multiple arrays in a single loop</strong><a name="Tip2">&#160;</a></p>
<p>A friend asked me a C# question: is there a way to iterate over multiple collections with the same loop? His code looked something like this:</p>
<pre class="code"><span style="color: blue">foreach </span>(<span style="color: blue">var </span>x <span style="color: blue">in </span>array1) {
    DoSomething(x);
}

<span style="color: blue">foreach </span>(<span style="color: blue">var </span>x <span style="color: blue">in </span>array2) {
    DoSomething(x);
}</pre>
<p>In his case, the loop body was larger, and he did not like the duplicated code. But, he also did not want to allocate a new array to hold elements from both array1 and array2.</p>
<p>LINQ provides an elegant solution to this problem: the Concat operator. You can rewrite the above two loops with a single loop as follows:</p>
<pre class="code"><span style="color: blue">foreach </span>(<span style="color: blue">var </span>x <span style="color: blue">in </span>array1.Concat(array2)) {
    DoSomething(x);
}</pre>
<p>Note that since LINQ operates at the enumerator level, it will not allocate a new array to hold elements of array1 and array2. So, on top of being rather elegant, this solution is also space-efficient.</p>
<p><strong>3. Generate a random sequence</strong><a name="Tip3">&#160;</a></p>
<p>This is a simple trick to generate a random sequence of length N:</p>
<pre class="code"><span style="color: #2b91af">Random </span>rand = <span style="color: blue">new </span><span style="color: #2b91af">Random</span>();
<span style="color: blue">var </span>randomSeq = <span style="color: #2b91af">Enumerable</span>.Repeat(0, N).Select(i =&gt; rand.Next());</pre>
<p>Thanks to the lazy nature of LINQ, the sequence is not pre-computed and stored in an array, but instead random numbers are generated on-demand, as you iterate over randomSeq.</p>
<p><strong>4. Generate a string</strong><a name="Tip4">&#160;</a></p>
<p>LINQ is also a nice tool to generate various kinds of strings. I found this quite useful to generate strings for testing and debugging purposes.</p>
<p>Let&#8217;s say that you want to generate a string with the repeating pattern &quot;ABCABCABC&#8230;&quot; of length N. Using LINQ, the solution is quite elegant:</p>
<pre class="code"><span style="color: blue">string </span>str = <span style="color: blue">new string</span>(
    <span style="color: #2b91af">Enumerable</span>.Range(0, N)
    .Select(i =&gt; (<span style="color: blue">char</span>)(<span style="color: #a31515">'A' </span>+ i % 3))
    .ToArray());</pre>
<p><em>[EDIT]</em> Petar Petrov suggested another interesting way to generate strings with LINQ. His approach applies to different scenarios than my solution above:</p>
<pre class="code"><span style="color: blue">string </span>values = <span style="color: blue">string</span>.Join(<span style="color: blue">string</span>.Empty, <span style="color: #2b91af">Enumerable</span>.Repeat(pattern, N).ToArray());</pre>
<p><strong>5. Convert sequences or collections</strong><a name="Tip5">&#160;</a></p>
<p>One thing you cannot do in C# or VB is to cast a sequence of type T to a sequence of type U, even if T us a derived class from U. So, you cannot just simply cast List&lt;string&gt; to List&lt;object&gt;. (For an explanation why, see Bick Byers&#8217; <a href="http://blogs.msdn.com/rmbyers/archive/2005/02/16/375079.aspx">posting</a>).</p>
<p>But, if you are trying to convert IEnumerable&lt;T&gt; to IEnumerable&lt;U&gt;, LINQ has a simple and efficient solution for you:</p>
<pre class="code"><span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: blue">string</span>&gt; strEnumerable = ...;
<span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: blue">object</span>&gt; objEnumerable = strEnumerable.Cast&lt;<span style="color: blue">object</span>&gt;();</pre>
<p>If you need to convert List&lt;T&gt; to List&lt;U&gt;, there is also a simple LINQ solution, but it involves copying the list:</p>
<pre class="code"><span style="color: #2b91af">List</span>&lt;<span style="color: blue">string</span>&gt; strList = ...;
<span style="color: #2b91af">List</span>&lt;<span style="color: blue">object</span>&gt; objList = <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: blue">object</span>&gt;(strList.Cast&lt;<span style="color: blue">object</span>&gt;());</pre>
<p><em>[EDIT]</em> <a href="http://chriscavanagh.wordpress.com/">Chris Cavanagh</a> suggested an alternate solution:</p>
<pre class="code"><span style="color: blue">var </span>objList = strList.Cast&lt;<span style="color: blue">object</span>&gt;().ToList();</pre>
<p><strong>6. Convert a value to a sequence of length 1</strong><a name="Tip6">&#160;</a></p>
<p>When you need to convert a single value to a sequence of length 1, what do you do? You could construct an array of length 1, but I prefer the LINQ Repeat operator:</p>
<pre class="code"><span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: blue">int</span>&gt; seq = <span style="color: #2b91af">Enumerable</span>.Repeat(myValue, 1);</pre>
<p><strong>7. Iterate over all subsets of a sequence</strong><a name="Tip7">&#160;</a></p>
<p>Sometimes it is useful to iterate over all subsets of an array. This situation arises quite frequently in brute-force solutions to hard problems. For small inputs, <a href="http://en.wikipedia.org/wiki/Subset_sum">subset sum</a>, <a href="http://en.wikipedia.org/wiki/Boolean_satisfiability_problem">boolean satisfiability</a> and the <a href="http://en.wikipedia.org/wiki/Knapsack_problem">knapsack problem</a> can all be solved easily by iterating over all subsets of some sequence.</p>
<p>In LINQ, we can generate all subsets of array arr as follows:</p>
<pre class="code">T[] arr = ...;
<span style="color: blue">var </span>subsets = <span style="color: blue">from </span>m <span style="color: blue">in </span><span style="color: #2b91af">Enumerable</span>.Range(0, 1 &lt;&lt; arr.Length)
              <span style="color: blue">select
                  from </span>i <span style="color: blue">in </span><span style="color: #2b91af">Enumerable</span>.Range(0, arr.Length)
                  <span style="color: blue">where </span>(m &amp; (1 &lt;&lt; i)) != 0
                  <span style="color: blue">select </span>arr[i];</pre>
<p>Note that if the number of subsets overflows an int, the above code will not work. So, only use it if you know that the length of arr is at most 30. If the length of arr is greater than 30, chances are that you don&#8217;t want to iterate over all of its subsets anyway because it is going to take minutes or more.</p>
<p><strong>Comments and Conclusion</strong></p>
<p>I hope you find these tricks useful and applicable to your programs.</p>
<p>The code samples in this posting are all implemented in C#, but they can be easily adapted to just about any other .Net language. However, LINQ is most conveniently used from .Net languages that support extension methods, lambda expressions and type inference, such as C# and Visual Basic.</p>
<p>To the best of my knowledge, each code sample in this posting works, but &#8211; as is common on the web &#8211; I don&#8217;t make any guarantees. As always, double check any code before using it.</p>
<p><strong>Related:</strong></p>
<ul>
<li><a href="http://igoro.com/archive/extended-linq-additional-operators-for-linq-to-objects/">Extended LINQ: additional operators for LINQ to objects</a> [igoro.com] </li>
<li><a href="http://www.amazon.com/gp/product/1933988169?ie=UTF8&amp;tag=igorostrblog-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=1933988169">LINQ in Action</a> [Book by Fabrice Marguerie] </li>
<li><a href="http://www.amazon.com/gp/product/1933988363?ie=UTF8&amp;tag=igorostrblog-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=1933988363">C# in Depth: What you need to master C# 2 and 3</a> [Book by Jon Skeet] </li>
</ul>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2figoro.com%2farchive%2f7-tricks-to-simplify-your-programs-with-linq%2f"><img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2figoro.com%2farchive%2f7-tricks-to-simplify-your-programs-with-linq%2f" border="0" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://igoro.com/archive/7-tricks-to-simplify-your-programs-with-linq/feed/</wfw:commentRss>
		<slash:comments>35</slash:comments>
		</item>
		<item>
		<title>Fun with C# generics: down-casting to a generic type</title>
		<link>http://igoro.com/archive/fun-with-c-generics-down-casting-to-a-generic-type/</link>
		<comments>http://igoro.com/archive/fun-with-c-generics-down-casting-to-a-generic-type/#comments</comments>
		<pubDate>Thu, 06 Sep 2007 07:35:25 +0000</pubDate>
		<dc:creator>Igor Ostrovsky</dc:creator>
				<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://igoro.com/?p=22</guid>
		<description><![CDATA[Today, I am writing about a design problem related to C# generics that I&#8217;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&#60;T&#62; inherits from a non-generic class Node, and we are holding [...]]]></description>
			<content:encoded><![CDATA[<p>Today, I am writing about a design problem related to C# generics that I&#8217;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 <span class="dean_ch_inline">Node&lt;T&gt;</span> inherits from a non-generic class <span class="dean_ch_inline">Node</span>, and we are holding a <span class="dean_ch_inline">Node</span> reference to a <span class="dean_ch_inline">Node&lt;T&gt;</span> object, we cannot just cast the object to <span class="dean_ch_inline">Node&lt;T&gt;</span> because we do not have access to T.</p>
<p>I realize that the description is a bit abstract; let&#8217;s look at an example right away! It may look like a bit of code, but the classes are very simple and do just what you&#8217;d expect:</p>
<p><span id="more-22"></span></p>
<pre lang="csharp">// Abstract node in a linked list
public abstract class Node {
	private Node m_next;
	public Node Next { get { return m_next; } }
	public Node(Node next) { m_next = next; }
}    

// Node in a linked list. Different nodes in the same list may carry data
// of different types.
public class Node&lt;T&gt; : Node {
	private T m_data;
	public T Data { get { return m_data; 	} }
	public Node&lt;T&gt;(T data, Node next) : base(next) {
		m_data = data;
	}
}    

// Abstract pair.
public abstract class Pair { }    

// Pair containing two values, possibly of different types.
public class Pair&lt;TFirst, TSecond&gt; : Pair {
	private TFirst m_first;
	private TSecond m_second;
	public Pair(TFirst first, TSecond second) {
		m_first = first;
		m_second = second;
	}
}</pre>
<p>Here, <span class="dean_ch_inline">Node</span> is a node in a linked list where each node may contain a different type of data. <span class="dean_ch_inline">Pair</span> is just a pair of two values.</p>
<p>Now, suppose that we want to implement this method:</p>
<pre lang="csharp">// Returns a pair containing the first two values from a linked list
public static Pair FirstTwoValues(Node node) {
	if (node == null) throw new ArgumentException();
	if (node.Next == null) throw new ArgumentException();    

	// Compilation error:
	return new Pair&lt;U,V&gt;((Node&lt;U&gt;)node, (Node&lt;V&gt;)node.Next);
}</pre>
<p>Unfortunately, what I wrote on the last line is not valid C# because we cannot introduce a type variable in a cast. It is clear what we need to do: we have two Node objects which we assume to be instances of <span class="dean_ch_inline">Node&lt;U&gt;</span> and <span class="dean_ch_inline">Node&lt;V&gt;</span> respectively, and we would like to invoke some generic method &#8211; in this case a <span class="dean_ch_inline">Pair&lt;U,V&gt;</span> constructor &#8211; using both <span class="dean_ch_inline">U</span> and <span class="dean_ch_inline">V</span> as generic type parameters for the method. So how can we implement this?</p>
<p><strong>Solution 1</strong><br />
The easiest solution is to use big nested <span class="dean_ch_inline">if</span> statements:</p>
<pre lang="csharp">public static Pair FirstTwoValues(Node node) {
	// [...]
	if (node is Node&lt;int&gt;) {
		if (node.Next is Node&lt;int&gt;) return new Pair&lt;int,int&gt;(
			(Node&lt;int&gt;)node, (Node&lt;int&gt;)node.Next);
		}
		else if (node.Next is Node&lt;string&gt;) ...
		...
	}
	else if (node is Node&lt;string&gt;) ...
}</pre>
<p>Obviously, this solution only works when the set of possible node data is constrained and very small, and even then the code is quite ugly.</p>
<p><strong>Solution 2</strong></p>
<p>Another possible solution is to use reflection. Reflection is very powerful, but it should be considered a last-resort technique because of its huge negative impact on the performance and general ugliness.</p>
<p><strong>Solution 3</strong></p>
<p>There is a third solution, that is in many cases preferable over the two I already mentioned. One way to &#8220;get access&#8221; to the type parameter <span class="dean_ch_inline">T</span> of the <span class="dean_ch_inline">Node&lt;T&gt;</span> is through a virtual method call. Node implements an abstract method, which <span class="dean_ch_inline">Node&lt;T&gt;</span> overrides. Calling the method on a <span class="dean_ch_inline">Node</span> reference results in that method executing in the context of <span class="dean_ch_inline">Node&lt;T&gt;</span> and we finally get access to that elusive type parameter <span class="dean_ch_inline">T</span>!</p>
<p>But, what if we need to get access to multiple generic type parameters coming from multiple objects, just like when trying to call a <span class="dean_ch_inline">Pair&lt;U,V&gt;</span> constructor given <span class="dean_ch_inline">Node&lt;U&gt;</span> and <span class="dean_ch_inline">Node&lt;V&gt;</span>? We can have multiple chained virtual methods, where each one fixes one of the generic parameters:</p>
<pre lang="csharp">public abstract class Node {
	private Node m_next;
	public Node Next { get { return m_next; } }
	public Node(Node next) { m_next = next; }
	public abstract Pair ConstructPair(Node other);
	public abstract Pair ConstructPair&lt;TFirst&gt;(Node&lt;TFirst&gt; other);
}    

public class Node&lt;T&gt; : Node {
	private T m_data;
	public T Data { get { return m_data; } }
	public Node&lt;T&gt;(T data, Node next) : base(next) {
		m_data = data;
	}    

	public override Pair ConstructPair(Node secondNode) {
		return other.ConstructPair&lt;T&gt;(this);
	}    

	public override Pair ConstructPair&lt;TFirst&gt;(Node&lt;TFirst&gt; firstNode) {
		return new Pair&lt;TFirst,T&gt;(firstNode, this);
	}
}</pre>
<p>Unfortunately, in order for this solution to be applicable, we need to have control over all types that could possibly be specified as the generic parameter, and they also need to share a common base class or implement a common interface that we can modify. Another disadvantage is that the type parameter class (in our case, <span class="dean_ch_inline">Node</span>) needs to know how to perform a possibly unrelated action (in our case, to construct a <span class="dean_ch_inline">Pair</span>). This may or may not be ideal from the design perspective.</p>
<p>Despite the disadvantages, I personally found this little trick quite useful. The problems I applied it to were related to abstract syntax tree manipulations, but I would expect similar issues to arise in other problem domains like data structures, data binding, etc. If you run into a similar design problem with generics, let me know whether you were able to apply this trick or not.</p>
]]></content:encoded>
			<wfw:commentRss>http://igoro.com/archive/fun-with-c-generics-down-casting-to-a-generic-type/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
	</channel>
</rss>
