<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Alice, Bob and Mallory: Category C#</title>
    <link>http://alicebobandmallory.com/articles/category/c</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>metasyntactics</description>
    <item>
      <title>C# implicit string conversion</title>
      <description>&lt;p&gt;I know how it works and I think I can see why but I'm still not very fond of how eager C# is to perform implicit string conversion.&lt;/p&gt;

&lt;p&gt;Contrived example:&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;string s = -&lt;span style="color:#00D;font-weight:bold"&gt;42&lt;/span&gt; + &lt;span style="color:#04D"&gt;'+'&lt;/span&gt; + &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;+&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; + -&lt;span style="color:#60E;font-weight:bold"&gt;0&lt;/span&gt;&lt;span style="color:#60E;font-weight:bold"&gt;.1&lt;/span&gt; / -&lt;span style="color:#60E;font-weight:bold"&gt;0&lt;/span&gt;&lt;span style="color:#60E;font-weight:bold"&gt;.1&lt;/span&gt; + &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;=&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; + (&lt;span style="color:#00D;font-weight:bold"&gt;7&lt;/span&gt; ^ &lt;span style="color:#00D;font-weight:bold"&gt;5&lt;/span&gt;) + &lt;tt&gt;
&lt;/tt&gt;      &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt; is &lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; + &lt;span style="color:#038;font-weight:bold"&gt;true&lt;/span&gt; + &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt; and not &lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; + AddressFamily.Unknown;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;s will be set to &lt;font color="white"&gt;"1+1=2 is True and not Unknown"&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;The answer is in white text above, select the text to see it.&lt;/p&gt;

&lt;p&gt;A more real problem is something like this&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;string str = &lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt; + &lt;span style="color:#00D;font-weight:bold"&gt;2&lt;/span&gt; + &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;!=&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; + &lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt; + &lt;span style="color:#00D;font-weight:bold"&gt;2&lt;/span&gt;;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;code&gt;str&lt;/code&gt; will be set to "3!=12".&lt;/p&gt;

&lt;p&gt;&lt;font style="color:red"&gt;Edit 2010-02-08&lt;/font&gt;&lt;br/&gt;
This wouldn't be much of a problem if all objects in .NET always returned a decent string representation of their current state/value with ToString() but that's not the case. &lt;a href="http://msdn.microsoft.com/en-us/library/system.object.tostring.aspx"&gt;Instead&lt;/a&gt; "The default implementation returns the fully qualified name of the type of the Object.".&lt;br/&gt;
I don't like the inconsistency. It's way too late now but I think it would have been much better if only objects that really produces a human readable output of the data in the object should implement ToString(). If you want the name of the type of the Object there should be another way.&lt;/p&gt;</description>
      <pubDate>Wed, 03 Feb 2010 17:32:00 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:da6a1f72-b81c-4ee7-b5d1-8f7a821595e3</guid>
      <author>Jonas Elfström</author>
      <link>http://alicebobandmallory.com/articles/2010/02/03/c-implicit-string-conversion</link>
      <category>C#</category>
    </item>
    <item>
      <title>Finding primes in parallel</title>
      <description>&lt;p&gt;&lt;a href="http://www.codethinked.com/page/About-Me.aspx"&gt;Justin Etheredge&lt;/a&gt; has been blogging about his &lt;a href="http://www.codethinked.com/post/2010/01/08/TekPubs-Mastering-LINQ-Challenge.aspx"&gt;challenge&lt;/a&gt; to find prime numbers with LINQ. He &lt;a href="http://www.codethinked.com/post/2010/01/10/The-TekPub-LINQ-Challenge-Part-2-Faster-Algorithms.aspx"&gt;later&lt;/a&gt; used &lt;code&gt;AsParallel()&lt;/code&gt; (coming in .NET 4) to speed things up and then followed that up with &lt;a href="http://www.codethinked.com/post/2010/01/12/The-TekPub-LINQ-Challenge-And-The-Sieve-Of-Eratosthenes.aspx"&gt;a post&lt;/a&gt; about using &lt;a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes"&gt;The Sieve Of Eratosthenes&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;As you can see in the comments of those posts I tried to speed the Sieve of Eratosthenes up by using &lt;code&gt;Parallel.For&lt;/code&gt; in the inner loop. I also tried AsParallel() in the LINQ expression but it made no difference in either case. At most it got 5% faster. I'm not sure but it could be that because SoE is very memory intense we could have a scaling issue and maybe also memory bandwidth exhaustion. This is mere speculation.&lt;/p&gt;

&lt;p&gt;I then searched for other algorithms and found &lt;a href="http://en.wikipedia.org/wiki/Sieve_of_Atkin"&gt;The Sieve of Atkin&lt;/a&gt;. It uses less memory than SoE so I thought I'd give it a try.&lt;/p&gt;

&lt;p&gt;I set the limit to 20,000,000 and then benchmarked it. It timed in on 2.48s so actually worse than the 2.2s that SoE took. Not good!
Then I added &lt;code&gt;Parallel.For&lt;/code&gt; in the loop that did most of the work and  lo and behold, it scaled! I have two cores in my machine (T7200@2.0GHz) and the average runtime went down to 1.26s. That's almost linear and surprisingly good! If you happen have a quad core (or more) and feel like trying it out then please contact me. It would be interesting to see if it scales further. &lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;8&lt;tt&gt;
&lt;/tt&gt;9&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;10&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;11&lt;tt&gt;
&lt;/tt&gt;12&lt;tt&gt;
&lt;/tt&gt;13&lt;tt&gt;
&lt;/tt&gt;14&lt;tt&gt;
&lt;/tt&gt;15&lt;tt&gt;
&lt;/tt&gt;16&lt;tt&gt;
&lt;/tt&gt;17&lt;tt&gt;
&lt;/tt&gt;18&lt;tt&gt;
&lt;/tt&gt;19&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;20&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;21&lt;tt&gt;
&lt;/tt&gt;22&lt;tt&gt;
&lt;/tt&gt;23&lt;tt&gt;
&lt;/tt&gt;24&lt;tt&gt;
&lt;/tt&gt;25&lt;tt&gt;
&lt;/tt&gt;26&lt;tt&gt;
&lt;/tt&gt;27&lt;tt&gt;
&lt;/tt&gt;28&lt;tt&gt;
&lt;/tt&gt;29&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;30&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;31&lt;tt&gt;
&lt;/tt&gt;32&lt;tt&gt;
&lt;/tt&gt;33&lt;tt&gt;
&lt;/tt&gt;34&lt;tt&gt;
&lt;/tt&gt;35&lt;tt&gt;
&lt;/tt&gt;36&lt;tt&gt;
&lt;/tt&gt;37&lt;tt&gt;
&lt;/tt&gt;38&lt;tt&gt;
&lt;/tt&gt;39&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;40&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;41&lt;tt&gt;
&lt;/tt&gt;42&lt;tt&gt;
&lt;/tt&gt;43&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#080;font-weight:bold"&gt;static&lt;/span&gt; List&amp;lt;&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt;&amp;gt; FindPrimesBySieveOfAtkins(&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; max)&lt;tt&gt;
&lt;/tt&gt;{&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; isPrime = new BitArray((&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt;)max+&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;, &lt;span style="color:#038;font-weight:bold"&gt;false&lt;/span&gt;);&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; sqrt = (&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt;)Math.Sqrt(max);&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;    Parallel.For(&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;, sqrt, x =&amp;gt;&lt;tt&gt;
&lt;/tt&gt;    {&lt;tt&gt;
&lt;/tt&gt;        &lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; xx = x * x;&lt;tt&gt;
&lt;/tt&gt;        &lt;span style="color:#080;font-weight:bold"&gt;for&lt;/span&gt; (&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; y = &lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;; y &amp;lt;= sqrt; y++)&lt;tt&gt;
&lt;/tt&gt;        {&lt;tt&gt;
&lt;/tt&gt;            &lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; yy = y * y;&lt;tt&gt;
&lt;/tt&gt;            &lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; n = &lt;span style="color:#00D;font-weight:bold"&gt;4&lt;/span&gt; * xx + yy;&lt;tt&gt;
&lt;/tt&gt;            &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (n &amp;lt;= max &amp;amp;&amp;amp; (n % &lt;span style="color:#00D;font-weight:bold"&gt;12&lt;/span&gt; == &lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt; || n % &lt;span style="color:#00D;font-weight:bold"&gt;12&lt;/span&gt; == &lt;span style="color:#00D;font-weight:bold"&gt;5&lt;/span&gt;))&lt;tt&gt;
&lt;/tt&gt;                isPrime[n] = !isPrime[n];&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;            n = &lt;span style="color:#00D;font-weight:bold"&gt;3&lt;/span&gt; * xx + yy;&lt;tt&gt;
&lt;/tt&gt;            &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (n &amp;lt;= max &amp;amp;&amp;amp; n % &lt;span style="color:#00D;font-weight:bold"&gt;12&lt;/span&gt; == &lt;span style="color:#00D;font-weight:bold"&gt;7&lt;/span&gt;)&lt;tt&gt;
&lt;/tt&gt;                isPrime[n] = !isPrime[n];&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;            n = &lt;span style="color:#00D;font-weight:bold"&gt;3&lt;/span&gt; * xx - yy;&lt;tt&gt;
&lt;/tt&gt;            &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (x &amp;gt; y &amp;amp;&amp;amp; n &amp;lt;= max &amp;amp;&amp;amp; n % &lt;span style="color:#00D;font-weight:bold"&gt;12&lt;/span&gt; == &lt;span style="color:#00D;font-weight:bold"&gt;11&lt;/span&gt;)&lt;tt&gt;
&lt;/tt&gt;                isPrime[n] = !isPrime[n];&lt;tt&gt;
&lt;/tt&gt;        }&lt;tt&gt;
&lt;/tt&gt;    });&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; primes = new List&amp;lt;&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt;&amp;gt;() { &lt;span style="color:#00D;font-weight:bold"&gt;2&lt;/span&gt;, &lt;span style="color:#00D;font-weight:bold"&gt;3&lt;/span&gt; };&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;for&lt;/span&gt; (&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; n = &lt;span style="color:#00D;font-weight:bold"&gt;5&lt;/span&gt;; n &amp;lt;= sqrt; n++)&lt;tt&gt;
&lt;/tt&gt;    {&lt;tt&gt;
&lt;/tt&gt;        &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (isPrime[n])&lt;tt&gt;
&lt;/tt&gt;        {&lt;tt&gt;
&lt;/tt&gt;            primes.Add(n);&lt;tt&gt;
&lt;/tt&gt;            &lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; nn = n * n;&lt;tt&gt;
&lt;/tt&gt;            &lt;span style="color:#080;font-weight:bold"&gt;for&lt;/span&gt; (&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; k = nn; k &amp;lt;= max; k += nn)&lt;tt&gt;
&lt;/tt&gt;                isPrime[k] = &lt;span style="color:#038;font-weight:bold"&gt;false&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;        }&lt;tt&gt;
&lt;/tt&gt;    }&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;for&lt;/span&gt; (&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; n = sqrt + &lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;; n &amp;lt;= max; n++)&lt;tt&gt;
&lt;/tt&gt;        &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (isPrime[n])&lt;tt&gt;
&lt;/tt&gt;            primes.Add(n);&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; primes;&lt;tt&gt;
&lt;/tt&gt;}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;This is C# 4.0 code, compiled in Visual C# 2010 Express Beta 2.&lt;/p&gt;

&lt;p&gt;&lt;font style="color:red;font-weight:bold"&gt;Edit 2010-01-20&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;Indications are that this does in fact not scale very good on a quad core. It's even worse, it seems it scales good on my old T7200 but not on a dual core E6320. I don't know why but of course the shared state of the &lt;strong&gt;isPrime&lt;/strong&gt; &lt;code&gt;BitArray&lt;/code&gt; is a huge problem and maybe it could be that differences in CPU architecture (FSB speed, caches and so on) in the E6320 is an explanation. Average execution time on the E6320 was 1290ms in a single thread and 1064ms in two.&lt;/p&gt;

&lt;p&gt;If you want to try this in an older version of C# than 4.0 then check out &lt;a href="http://coding-time.blogspot.com/2008/03/implement-your-own-parallelfor-in-c.html"&gt;this post&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;A reader asked how I timed the executions. Here's how.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;8&lt;tt&gt;
&lt;/tt&gt;9&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;10&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;11&lt;tt&gt;
&lt;/tt&gt;12&lt;tt&gt;
&lt;/tt&gt;13&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; steps = new List&amp;lt;&lt;span style="color:#339;font-weight:bold"&gt;long&lt;/span&gt;&amp;gt;();&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; watch = new Stopwatch();&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#080;font-weight:bold"&gt;for&lt;/span&gt; (&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; i = &lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;; i &amp;lt; &lt;span style="color:#00D;font-weight:bold"&gt;10&lt;/span&gt;; i++) &lt;tt&gt;
&lt;/tt&gt;{&lt;tt&gt;
&lt;/tt&gt;    watch.Reset();&lt;tt&gt;
&lt;/tt&gt;    watch.Start();&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; primes = FindPrimesBySieveOfAtkins(&lt;span style="color:#00D;font-weight:bold"&gt;20000000&lt;/span&gt;);&lt;tt&gt;
&lt;/tt&gt;    watch.Stop();&lt;tt&gt;
&lt;/tt&gt;    Console.WriteLine(watch.ElapsedMilliseconds.ToString());&lt;tt&gt;
&lt;/tt&gt;    steps.Add(watch.ElapsedMilliseconds);&lt;tt&gt;
&lt;/tt&gt;}&lt;tt&gt;
&lt;/tt&gt;Console.WriteLine(&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;Average: &lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; + steps.Average().ToString());&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;</description>
      <pubDate>Thu, 14 Jan 2010 22:55:00 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:2c932b6d-8c9c-4532-9b8f-85ea02fdd193</guid>
      <author>Jonas Elfström</author>
      <link>http://alicebobandmallory.com/articles/2010/01/14/prime-factorization-in-parallel</link>
      <category>C#</category>
      <category>Java</category>
    </item>
    <item>
      <title>Infinite ranges in C#</title>
      <description>&lt;p&gt;I &lt;a href="http://blogs.msdn.com/ericlippert/archive/2009/10/15/as-timeless-as-infinity.aspx"&gt;recently learned&lt;/a&gt; that C# is compliant with the &lt;a href="http://en.wikipedia.org/wiki/IEEE_754-1985"&gt;IEEE 754-1985&lt;/a&gt; for floating point arithmetics. That wasn't a big surprise but that &lt;a href="http://en.wikipedia.org/wiki/Division_by_zero#In_computer_arithmetic"&gt;division by zero&lt;/a&gt; is defined as &lt;code&gt;Infinity&lt;/code&gt; in it was! It actually kind of bothers me that I didn't know this.&lt;/p&gt;

&lt;p&gt;In mathematics division by zero is &lt;a href="http://en.wikipedia.org/wiki/Defined_and_undefined"&gt;undefined&lt;/a&gt; for real numbers but I guess &lt;code&gt;Infinity&lt;/code&gt; is a more pragmatic result. Or as a friend put it &lt;em&gt;"IEEE stands for Institute of Electrical and Electronics Engineers not Institute of Mathematics"&lt;/em&gt;&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#339;font-weight:bold"&gt;double&lt;/span&gt; n = &lt;span style="color:#60E;font-weight:bold"&gt;1&lt;/span&gt;&lt;span style="color:#60E;font-weight:bold"&gt;.0&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;n = n / &lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (n &amp;gt; &lt;span style="color:#00D;font-weight:bold"&gt;636413622384679305&lt;/span&gt;)&lt;tt&gt;
&lt;/tt&gt;  System.Console.WriteLine(&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;Yes it certainly is!&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;);&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;
This C# code does not throw an exception, it simply leaves n defined as Infinity and writes a line to the console.&lt;/p&gt;

&lt;p&gt;Ruby is &lt;a href="http://weblog.jamisbuck.org/2007/2/7/infinity"&gt;also&lt;/a&gt; IEEE 754-1985 compliant. It even lets you define &lt;a href="http://banisterfiend.wordpress.com/2009/10/02/wtf-infinite-ranges-in-ruby/"&gt;infinite ranges&lt;/a&gt;.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#036;font-weight:bold"&gt;Infinity&lt;/span&gt;=&lt;span style="color:#60E;font-weight:bold"&gt;1.0&lt;/span&gt;/&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;=&amp;gt;&lt;span style="color:#036;font-weight:bold"&gt;Infinity&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;(&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;..&lt;span style="color:#036;font-weight:bold"&gt;Infinity&lt;/span&gt;).include?(&lt;span style="color:#00D;font-weight:bold"&gt;162259276829213363391578010288127&lt;/span&gt;)&lt;tt&gt;
&lt;/tt&gt;=&amp;gt; &lt;span style="color:#038;font-weight:bold"&gt;true&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;(&lt;span style="color:#00D;font-weight:bold"&gt;7&lt;/span&gt;..&lt;span style="color:#036;font-weight:bold"&gt;Infinity&lt;/span&gt;).step(&lt;span style="color:#00D;font-weight:bold"&gt;7&lt;/span&gt;).take(&lt;span style="color:#00D;font-weight:bold"&gt;3&lt;/span&gt;).inject(&amp;amp;&lt;span style="color:#A60"&gt;:+&lt;/span&gt;) &lt;span style="color:#666"&gt;# 7+14+21&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;=&amp;gt; &lt;span style="color:#00D;font-weight:bold"&gt;42&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;
I can't say I see very much &lt;a href="http://www.michaelharrison.ws/weblog/?p=163"&gt;use&lt;/a&gt; of this but it brings a kind of completeness to the handling of infinities. Unfortunately it seems we don't get that in C# out of the box because &lt;code&gt;Enumerable.Range&lt;/code&gt; takes &lt;code&gt;&amp;lt;int&amp;gt;,&amp;lt;int&amp;gt;&lt;/code&gt; as parameters and there's no &lt;code&gt;Infinity&lt;/code&gt; definition for &lt;code&gt;int&lt;/code&gt;.  That's unless someone wrote a generic Range class. Turns out &lt;a href="http://stackoverflow.com/users/22656/jon-skeet"&gt;none other&lt;/a&gt; than &lt;a href="http://www.yoda.arachsys.com/csharp/"&gt;Jon Skeet&lt;/a&gt; did in his &lt;a href="http://www.yoda.arachsys.com/csharp/miscutil/"&gt;MiscUtil&lt;/a&gt;. Download MiscUtil and then by &lt;code&gt;using MiscUtil.Collections;&lt;/code&gt; you can:&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#339;font-weight:bold"&gt;double&lt;/span&gt; n = &lt;span style="color:#60E;font-weight:bold"&gt;1&lt;/span&gt;&lt;span style="color:#60E;font-weight:bold"&gt;.0&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; infinity = n / &lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; r = new Range&amp;lt;&lt;span style="color:#339;font-weight:bold"&gt;double&lt;/span&gt;&amp;gt;(&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;, infinity);&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (r.Contains(&lt;span style="color:#00D;font-weight:bold"&gt;4711&lt;/span&gt;))&lt;tt&gt;
&lt;/tt&gt;  System.Console.WriteLine(&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;Yes it certainly does!&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;);&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; sum = r.Step(&lt;span style="color:#60E;font-weight:bold"&gt;7&lt;/span&gt;&lt;span style="color:#60E;font-weight:bold"&gt;.0&lt;/span&gt;).Take(&lt;span style="color:#00D;font-weight:bold"&gt;3&lt;/span&gt;).Sum();&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;
And guess what, it works like a charm! &lt;code&gt;4711&lt;/code&gt; is part of positive infinity and &lt;code&gt;sum&lt;/code&gt; is 42.0 and all is good.&lt;/p&gt;

&lt;p&gt;&lt;font style="color:red;font-weight:bold"&gt;Edit&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;There's also a couple of predefined constants. Thanks to Eric for pointing that out.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; r = new Range&amp;lt;&lt;span style="color:#339;font-weight:bold"&gt;double&lt;/span&gt;&amp;gt;(&lt;span style="color:#00D;font-weight:bold"&gt;7&lt;/span&gt;,  System.Double.PositiveInfinity);&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; sum = r.Step(&lt;span style="color:#60E;font-weight:bold"&gt;7&lt;/span&gt;&lt;span style="color:#60E;font-weight:bold"&gt;.0&lt;/span&gt;).Take(&lt;span style="color:#00D;font-weight:bold"&gt;3&lt;/span&gt;).Sum();&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;&lt;/p&gt;</description>
      <pubDate>Tue, 20 Oct 2009 20:41:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:1337fd12-5058-4b35-bbf4-c9d0c4e9ade8</guid>
      <author>Jonas Elfström</author>
      <link>http://alicebobandmallory.com/articles/2009/10/20/infinite-ranges-in-c</link>
      <category>Ruby</category>
      <category>C#</category>
      <category>Math</category>
    </item>
    <item>
      <title>The Thrush combinator in C#</title>
      <description>&lt;p&gt;Last year I read &lt;a href="http://github.com/raganwald/homoiconic/blob/master/README.markdown"&gt;Reg "Raganwald'" Braithwaite's&lt;/a&gt; excellent post &lt;a href="http://github.com/raganwald/homoiconic/blob/master/2008-10-30/thrush.markdown#readme"&gt;The Thrush&lt;/a&gt; and he  explains it as &lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;The thrush is written Txy = yx. It reverses evaluation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Back then I didn't even consider trying to implement it in C#. That was before I digged deeper into &lt;a href="http://msdn.microsoft.com/en-us/library/bb397687.aspx"&gt;lambda expressions&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/bb383977.aspx"&gt;extension methods&lt;/a&gt; in C# 3.0 and way before last night when I read Debasish Ghosh's post on how to &lt;a href="http://debasishg.blogspot.com/2009/09/thrush-combinator-in-scala.html"&gt;implement the Thrush in Scala&lt;/a&gt;. After reading that my first thought was if it was possible to do the same in C#. Here's my attempt.&lt;/p&gt;

&lt;p&gt;At first I struggled with the static typing and headed for an easy way out using Object in the extension method of Object:&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;public &lt;span style="color:#080;font-weight:bold"&gt;static&lt;/span&gt; object Into(this Object obj, Func&amp;lt;object, object&amp;gt; f)&lt;tt&gt;
&lt;/tt&gt;{  &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; f.Invoke(obj); }&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;pre&gt;&lt;/pre&gt;

&lt;p&gt;My goal was to translate the Ruby example&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;(&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;..&lt;span style="color:#00D;font-weight:bold"&gt;100&lt;/span&gt;).select(&amp;amp;&lt;span style="color:#A60"&gt;:odd?&lt;/span&gt;).inject(&amp;amp;&lt;span style="color:#A60"&gt;:+&lt;/span&gt;).into { |x| x * x }&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;in Raganwald's post to C#.&lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;Which reads "Take the numbers from 1 to 100, keep the odd ones, take the sum of those, and then answer the square of that number."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But with the Object based extension method I had to do some ugly casts.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; r = Enumerable.Range(&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;, &lt;span style="color:#00D;font-weight:bold"&gt;100&lt;/span&gt;).Where(x =&amp;gt; Odd(x)).Sum().Into(x =&amp;gt; (&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt;)x * (&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt;)x);&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;pre&gt;&lt;/pre&gt;

&lt;p&gt;With som added typing I could do: &lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; result = Enumerable.Range(&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;, &lt;span style="color:#00D;font-weight:bold"&gt;100&lt;/span&gt;).Where(x =&amp;gt; Odd(x)).Sum().Into(x =&amp;gt; x * x);&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;pre&gt;&lt;/pre&gt;

&lt;p&gt;But that merely moved the cast to the ext. method and also made it work for integers only.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;public &lt;span style="color:#080;font-weight:bold"&gt;static&lt;/span&gt; &lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; Into(this Object obj, Func&amp;lt;&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt;, &lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt;&amp;gt; f)&lt;tt&gt;
&lt;/tt&gt;{ &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; f.Invoke((&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt;)obj); }&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;pre&gt;&lt;/pre&gt;

&lt;p&gt;Then I remembered generics and method type inference.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;public &lt;span style="color:#080;font-weight:bold"&gt;static&lt;/span&gt; T Into&amp;lt;T&amp;gt;(this T obj, Func&amp;lt;T, T&amp;gt; f)&lt;tt&gt;
&lt;/tt&gt;{ &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; f(obj); }&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;And now not only the casts were gone but I also got a thrush combinator almost as flexible as the one in Ruby.&lt;/p&gt;

&lt;p&gt;Contrived example follows:&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; test = &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;ball&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; ball = test.Into(s =&amp;gt; &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;Are we having a &lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; + s + &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt; yet?&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;);&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;pre&gt; &lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;The odd part&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;Odd(x)&lt;/em&gt; method call in the calculation above is a plain static method.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;private &lt;span style="color:#080;font-weight:bold"&gt;static&lt;/span&gt; &lt;span style="color:#339;font-weight:bold"&gt;bool&lt;/span&gt; Odd(&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; n)&lt;tt&gt;
&lt;/tt&gt;{ &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; (n % &lt;span style="color:#00D;font-weight:bold"&gt;2&lt;/span&gt; != &lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;); }&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;If you want an even more terse syntax you could try an ext. method on IEnumerable like this:&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;public &lt;span style="color:#080;font-weight:bold"&gt;static&lt;/span&gt; IEnumerable&amp;lt;&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt;&amp;gt; Odd(this IEnumerable&amp;lt;&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt;&amp;gt; en)&lt;tt&gt;
&lt;/tt&gt;{ &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; en.Where(n =&amp;gt; n % &lt;span style="color:#00D;font-weight:bold"&gt;2&lt;/span&gt; != &lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;); }&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;Gives:&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; result = Enumerable.Range(&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;, &lt;span style="color:#00D;font-weight:bold"&gt;100&lt;/span&gt;).Odd().Sum().Into(x =&amp;gt; x * x);&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;pre&gt;&lt;/pre&gt;

&lt;p&gt;Also as a general alternative to &lt;code&gt;.Sum()&lt;/code&gt; I could have used &lt;code&gt;.Aggregate((x, y) =&amp;gt; x + y))&lt;/code&gt; but I found it a bit verbose.&lt;/p&gt;

&lt;p&gt;In C# I don't think it's possible to pull off the Symbol#to_proc stuff that Ruby does. That's the &lt;em&gt;&amp;amp;:&lt;/em&gt; in the &lt;em&gt;select(&amp;amp;:odd?)&lt;/em&gt; and the &lt;em&gt;inject(&amp;amp;:+)&lt;/em&gt; in the Ruby example. Raganwald has a great &lt;a href="http://weblog.raganwald.com/2007/11/fun-with-symboltoproc.html"&gt;post&lt;/a&gt; on that too.&lt;/p&gt;

&lt;p&gt;&lt;font style="color:red;font-weight:bold"&gt;Edit&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;Check out &lt;a href="http://stackoverflow.com/questions/1528319/operators-as-method-parameters-in-c-and-the-thrush-combinator"&gt;Jon Skeet's nice answer&lt;/a&gt; on StackOverflow to my question on how to make this even more Ruby-like. I have to try out that Operator class later though.&lt;/p&gt;

&lt;p&gt;&lt;font style="color:red;font-weight:bold"&gt;Edit 2009-10-07&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;One thing I found a bit surprising is that by implementing the Into ext. method in this way it not only works for all objects based on &lt;code&gt;System.Object&lt;/code&gt; but it also works  for &lt;a href="http://msdn.microsoft.com/en-us/library/s1ax56ch.aspx"&gt;value types&lt;/a&gt;.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; n=&lt;span style="color:#00D;font-weight:bold"&gt;4711&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#339;font-weight:bold"&gt;int&lt;/span&gt; oddOrZero = n.Into(x =&amp;gt; x % &lt;span style="color:#00D;font-weight:bold"&gt;2&lt;/span&gt; !=&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt; ? x : &lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;); &lt;span style="color:#666"&gt;// 4711&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;n = &lt;span style="color:#00D;font-weight:bold"&gt;4712&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;oddOrZero = n.Into(x =&amp;gt; x % &lt;span style="color:#00D;font-weight:bold"&gt;2&lt;/span&gt; != &lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt; ? x : &lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt;); &lt;span style="color:#666"&gt;// 0&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;pre&gt;&lt;/pre&gt;

&lt;p&gt;&lt;font style="color:red;font-weight:bold"&gt;Edit 2009-10-12&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;My confusion did stem from my lack of understanding of &lt;a href="http://msdn.microsoft.com/en-us/library/bb383977(loband).aspx"&gt;extension methods&lt;/a&gt;. Ex. methods are in fact not extending &lt;code&gt;System.Object&lt;/code&gt; or any other type, they are "&lt;a href="http://blogs.msdn.com/ericlippert/about.aspx"&gt;nothing more than a pleasant syntax for calling a static method&lt;/a&gt;" in  case no instance method with the same name can be found.&lt;/p&gt;</description>
      <pubDate>Tue, 06 Oct 2009 20:35:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:b02ff8f0-44fb-44ca-a309-eb0fd074a495</guid>
      <author>Jonas Elfström</author>
      <link>http://alicebobandmallory.com/articles/2009/10/06/the-thrush-combinator-in-c</link>
      <category>Ruby</category>
      <category>C#</category>
    </item>
    <item>
      <title>No var for me in the foreach</title>
      <description>&lt;p&gt;In C# 3.0 we got type inference or implicit typing as Microsoft likes to call it. As a Ruby programmer I've got a thing for essence over ceremony and those repetive declarations in C# (and Java) has always bothered me. So of course I quickly put &lt;em&gt;var&lt;/em&gt; in my tool belt. If I want to create a certain object why should I have to state that twice?&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#666"&gt;// C# 2.0&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#339;font-weight:bold"&gt;Dictionary&lt;/span&gt;&amp;lt;Customer, &lt;span style="color:#339;font-weight:bold"&gt;List&lt;/span&gt;&amp;lt;PhoneNumber&amp;gt;&amp;gt; phonebook = &lt;span style="color:#080;font-weight:bold"&gt;new&lt;/span&gt; &lt;span style="color:#339;font-weight:bold"&gt;Dictionary&lt;/span&gt;&amp;lt;Customer, &lt;span style="color:#339;font-weight:bold"&gt;List&lt;/span&gt;&amp;lt;PhoneNumber&amp;gt;&amp;gt;(); &lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#666"&gt;// C# 3.0&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;var phonebook = &lt;span style="color:#080;font-weight:bold"&gt;new&lt;/span&gt; &lt;span style="color:#339;font-weight:bold"&gt;Dictionary&lt;/span&gt;&amp;lt;Customer, &lt;span style="color:#339;font-weight:bold"&gt;List&lt;/span&gt;&amp;lt;PhoneNumber&amp;gt;&amp;gt;();&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;Still you should use it with care. I've seen:&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;var i = &lt;span style="color:#00D;font-weight:bold"&gt;5&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;var s = &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;This stmt is unprovable!&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;And frankly, I do not agree.&lt;/p&gt;

&lt;p&gt;A couple of days ago I almost thought I found a bug or limitation in the C# compiler. Something like the following would not compile:&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#339;font-weight:bold"&gt;String&lt;/span&gt; html = &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;&amp;lt;a href='http://is.gd/Uoip'&amp;gt;Recursion&amp;lt;/a&amp;gt;,&lt;/span&gt;&lt;span style="color:#b0b"&gt;\r&lt;/span&gt;&lt;span style="color:#b0b"&gt;\n&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; +&lt;tt&gt;
&lt;/tt&gt;              &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;see &amp;lt;a href='http://is.gd/Uoip'&amp;gt;recursion&amp;lt;/a&amp;gt;.&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;&lt;span style="color:#339;font-weight:bold"&gt;String&lt;/span&gt; links=&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;var matches = Regex.Matches(html, &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;(a href=')(.*)('&amp;gt;)&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;);&lt;tt&gt;
&lt;/tt&gt;foreach (var match in matches) {&lt;tt&gt;
&lt;/tt&gt;    links+=match.Groups[&lt;span style="color:#00D;font-weight:bold"&gt;2&lt;/span&gt;]+&lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#b0b"&gt;\r&lt;/span&gt;&lt;span style="color:#b0b"&gt;\n&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;;&lt;tt&gt;
&lt;/tt&gt;}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;The compiler complained that Object had no Groups method. How come it could not see that Regex.Matches returned a MatchCollection and that that collection was populated with Match objects? Then it dawned on me. Back in the dark ages of C# 1.x we did not have generics. MatchCollection is an old class that implements ICollection and not ICollection(T) so the compiler could not infer the type. A quick change to:&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;foreach (Match match in matches) {&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;&lt;/p&gt;

&lt;p&gt;and we were good to go.&lt;/p&gt;</description>
      <pubDate>Tue, 09 Jun 2009 22:24:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:d0caed26-abe1-46b0-ad59-6058205ebeeb</guid>
      <author>Jonas Elfström</author>
      <link>http://alicebobandmallory.com/articles/2009/06/09/no-var-for-me-in-the-foreach</link>
      <category>C#</category>
    </item>
    <item>
      <title>Get the value of and with using the null-coalescing operator</title>
      <description>&lt;p&gt;I like the &lt;a href="http://msdn.microsoft.com/en-us/library/ms173224.aspx"&gt;?? operator&lt;/a&gt; that surfaced in C# 2.0. I'm often in environments where null values florish and ?? is a great way to handle them, especially for presentation. A while ago I found a use for the ?? operator in a way I'd never used it before.&lt;/p&gt;

&lt;p&gt;The problem at hand was to return an account number from data that could be described as somewhat inconsistent.&lt;/p&gt;

&lt;p&gt;The application had left room for the users to enter accounts in two by two ways. A customer could have multiple subsidiaries and in each subsidiary, by misconception, two different fields had been used as the account number. It was also possible to connect an account to all subsidairies of a customer at the same time as a specific subsidiary of that customer had an account defined.&lt;/p&gt;

&lt;p&gt;Only if a unique account could be found it should be returned. All other cases should return a cause of failure so that the users could use that information to clean up the mess.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;8&lt;tt&gt;
&lt;/tt&gt;9&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;10&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;11&lt;tt&gt;
&lt;/tt&gt;12&lt;tt&gt;
&lt;/tt&gt;13&lt;tt&gt;
&lt;/tt&gt;14&lt;tt&gt;
&lt;/tt&gt;15&lt;tt&gt;
&lt;/tt&gt;16&lt;tt&gt;
&lt;/tt&gt;17&lt;tt&gt;
&lt;/tt&gt;18&lt;tt&gt;
&lt;/tt&gt;19&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;20&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;21&lt;tt&gt;
&lt;/tt&gt;22&lt;tt&gt;
&lt;/tt&gt;23&lt;tt&gt;
&lt;/tt&gt;24&lt;tt&gt;
&lt;/tt&gt;25&lt;tt&gt;
&lt;/tt&gt;26&lt;tt&gt;
&lt;/tt&gt;27&lt;tt&gt;
&lt;/tt&gt;28&lt;tt&gt;
&lt;/tt&gt;29&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;30&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;31&lt;tt&gt;
&lt;/tt&gt;32&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#088;font-weight:bold"&gt;private&lt;/span&gt; &lt;span style="color:#088;font-weight:bold"&gt;static&lt;/span&gt; Account GetCustomerAccount(Customer customer)&lt;tt&gt;
&lt;/tt&gt;{&lt;tt&gt;
&lt;/tt&gt;  bool hasAccount = !&lt;span style="color:#339;font-weight:bold"&gt;String&lt;/span&gt;.IsNullOrEmpty(customer.AccountNo);&lt;tt&gt;
&lt;/tt&gt;  bool hasAccountNoExtra = !&lt;span style="color:#339;font-weight:bold"&gt;String&lt;/span&gt;.IsNullOrEmpty(customer.AccountNoExtra);&lt;tt&gt;
&lt;/tt&gt;  bool hasAllSubsAccount = !&lt;span style="color:#339;font-weight:bold"&gt;String&lt;/span&gt;.IsNullOrEmpty(customer.AccountNoAllSubs);&lt;tt&gt;
&lt;/tt&gt;  bool hasAllSubsAccountNoExtra = &lt;tt&gt;
&lt;/tt&gt;       !&lt;span style="color:#339;font-weight:bold"&gt;String&lt;/span&gt;.IsNullOrEmpty(customer.AccountNoExtraAllSubs);&lt;tt&gt;
&lt;/tt&gt;  var account = &lt;span style="color:#080;font-weight:bold"&gt;new&lt;/span&gt; Account();&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; ((hasAccount || hasAccountNoExtra) &amp;amp;&amp;amp;&lt;tt&gt;
&lt;/tt&gt;      (hasAllSubsAccount || hasAllSubsAccountNoExtra)) &lt;tt&gt;
&lt;/tt&gt;  {&lt;tt&gt;
&lt;/tt&gt;    account.Status = account.HasBothAllSubsAndSpecific;&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; account;&lt;tt&gt;
&lt;/tt&gt;  }&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; ((hasAccount &amp;amp;&amp;amp; hasAccountNoExtra) ||   &lt;tt&gt;
&lt;/tt&gt;      (hasAllSubsAccount &amp;amp;&amp;amp; hasAllSubsAccountNoExtra)) &lt;tt&gt;
&lt;/tt&gt;  {&lt;tt&gt;
&lt;/tt&gt;    account.Status = account.HasBothAccountAndAccountExtra;&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; account;&lt;tt&gt;
&lt;/tt&gt;  }&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;  account.AccountNo = customer.AccountNo ??&lt;tt&gt;
&lt;/tt&gt;                      customer.AccountNoExtra ??&lt;tt&gt;
&lt;/tt&gt;                      customer.AccountNoAllSubs ??&lt;tt&gt;
&lt;/tt&gt;                      customer.AccountNoExtraAllSubs;&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (account.AccountNo==&lt;span style="color:#080;font-weight:bold"&gt;null&lt;/span&gt;)&lt;tt&gt;
&lt;/tt&gt;    account.Status=account.HasNoAccountDefined;&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; account;&lt;tt&gt;
&lt;/tt&gt;}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;pre&gt;&lt;/pre&gt;

&lt;p&gt;Notice how AccountNo is set to the first non null value of the four. Imagine that with plain ifs. Here I believe the ??-operator both makes the code clear and saves us from a bunch of nested ifs.&lt;/p&gt;</description>
      <pubDate>Sat, 16 May 2009 00:38:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:41954150-6016-4a3f-a30e-c1324acfc4a4</guid>
      <author>Jonas Elfström</author>
      <link>http://alicebobandmallory.com/articles/2009/05/16/get-the-value-of-and-with-using-the-null-coalescing-operator</link>
      <category>C#</category>
    </item>
    <item>
      <title>Predicate&amp;lt;T&amp;gt; matches me</title>
      <description>&lt;p&gt;I really like the Predicate(T) delegates that were added to the generic collections and lists in .NET 2.0. With the later addition of lambda expressions came cleanliness and readability.&lt;/p&gt;

&lt;p&gt;Today we faced a quite simple problem that were made even simpler by the dear predicates. We had a kind of event log and wanted to filter it client side (Windows Forms) using a list of criterias. We began by implementing to filter by a number of categories. It ended up being only one row (in Visual Studio, for obvious reasons not here):&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#088;font-weight:bold"&gt;private&lt;/span&gt; &lt;span style="color:#339;font-weight:bold"&gt;List&lt;/span&gt;&amp;lt;Events&amp;gt; FilterEventsByCategory(&lt;span style="color:#339;font-weight:bold"&gt;List&lt;/span&gt;&amp;lt;Events&amp;gt; events,&lt;tt&gt;
&lt;/tt&gt;                                        &lt;span style="color:#339;font-weight:bold"&gt;List&lt;/span&gt;&amp;lt;Category&amp;gt; categories) &lt;tt&gt;
&lt;/tt&gt;{&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; events.FindAll(event =&amp;gt; &lt;tt&gt;
&lt;/tt&gt;      categories.Exists(category =&amp;gt; category.CategoryId==event.CategoryId)); &lt;tt&gt;
&lt;/tt&gt;}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;pre&gt;&lt;/pre&gt;

&lt;p&gt;Neat!&lt;/p&gt;</description>
      <pubDate>Thu, 29 Jan 2009 21:29:00 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:524b2718-70a9-41f1-b48f-5a3b37d6b8e4</guid>
      <author>Jonas Elfström</author>
      <link>http://alicebobandmallory.com/articles/2009/01/29/predicate-matches-me</link>
      <category>C#</category>
    </item>
    <item>
      <title>Unit testing strains</title>
      <description>&lt;p&gt;I've felt it and I've heard it from colleagues several times. Writing unit tests can be hard work. Especially adding unit test to an existing code base is, at best, cumbersome. Also it's one of those things with delayed gratification. Sometimes it's not even you that will benefit from them being there because the biggest win can be long down the road, when changes to the system has to be made. &lt;/p&gt;

&lt;p&gt;Tests may seem to be isolated and it's even considered a good thing to keep them that way. Even so the tests of your application has a correlation to what the system aims to do on a bigger scale.  This one of the things &lt;a href="http://en.wikipedia.org/wiki/Behavior_driven_development"&gt;BDD&lt;/a&gt;  focuses on. I think that one of the biggest advantages is that you in one process writes a specification and tests that ensures that the spec. is met. Testing becomes a natural part of the development process. This way it clearly shows that BDD and  &lt;a href="http://en.wikipedia.org/wiki/Test-driven_development"&gt;TDD&lt;/a&gt; are design processes and that it's certainly not all about adding unit tests.&lt;/p&gt;

&lt;p&gt;Find out more about BDD on: &lt;a href="BehaviourDrivenDevelopment"&gt;http://behaviour-driven.org&lt;/a&gt;&lt;br/&gt;
&lt;em&gt;It must be stressed that BDD is a rephrasing of existing good practice, it is not a radically new departure. Its aim is to bring together existing, well-established techniques under a common banner and with a consistent and unambiguous terminology.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;For Ruby &lt;a href="http://rspec.info/"&gt;RSpec&lt;/a&gt; has almost become the de facto standard for BDD. The concepts Story, Scenario, and Test feels natural and the syntax is short and easy to read.&lt;/p&gt;

&lt;p&gt;In languages like Java or C# the tests often becomes much more cluttered and some of that clutter is the extra code that comes with static typing. I believe that dynamically typed and overall dynamic languages like Ruby or Python could find a nice little niche here. They could become &lt;a href="http://en.wikipedia.org/wiki/Domain-specific_programming_language"&gt;DSL's&lt;/a&gt; for testing.&lt;/p&gt;

&lt;p&gt;RSpec is on it's way for .NET/C# via &lt;a href="http://en.wikipedia.org/wiki/IronRuby"&gt;IronRuby&lt;/a&gt; and for Java via &lt;a href="http://jruby.codehaus.org/"&gt;JRuby&lt;/a&gt; but don't hold your breath because they are still in alpha and beta.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.NET / C#&lt;/strong&gt;&lt;br/&gt;
&lt;a href="http://rubydoes.net/2008/02/21/testing-net-with-ironrubys-mini_rspecrb/"&gt;Testing .NET with IronRuby...&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://bgeek.net/2008/02/14/nspecify-rspec-well-closer-anyway/"&gt;NSpecify =&gt; RSpec… well closer anyway&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Java&lt;/strong&gt;&lt;br/&gt;
&lt;a href="http://pivots.pivotallabs.com/users/pzabelin/blog/articles/375-functional-tests-for-java-project-rspec-jruby"&gt;Java Functional Testing with JRuby and RSpec&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://jtestr.codehaus.org/"&gt;JtestR&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ruby&lt;/strong&gt;&lt;br/&gt;
&lt;a href="http://socialface.com/slapp/"&gt;Slapp&lt;/a&gt; - A simple chat wall Merb tutorial. With nice exampes of using RSpec.&lt;br/&gt;
&lt;a href="http://www.ibm.com/developerworks/web/library/wa-rspec/"&gt;Behavior-driven testing with RSpec&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ASP.NET MVC&lt;/strong&gt;&lt;br/&gt;
&lt;a href="http://blogs.msdn.com/webdevtools/archive/2008/03/06/asp-net-mvc-test-framework-integration-demo.aspx"&gt;ASP.NET MVC Test Framework Integration Walkthrough&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://www.asp.net/learn/3.5-extensions-videos/video-271.aspx"&gt;MVC Preview - Testing&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://www.hanselman.com/blog/ASPNETMVCSessionAtMix08TDDAndMvcMockHelpers.aspx"&gt;ASP.NET MVC Session at Mix08, TDD and MvcMockHelpers&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Mon, 05 May 2008 21:17:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:89da439e-a963-4880-9501-16902abf9b31</guid>
      <author>Jonas Elfström</author>
      <link>http://alicebobandmallory.com/articles/2008/05/05/unit-testing-strains</link>
      <category>Ruby</category>
      <category>C#</category>
      <category>Java</category>
    </item>
    <item>
      <title>What one-way hash function to use?</title>
      <description>&lt;p&gt;One-way hash functions takes a message of any length as input and outputs a very large but fixed length number, called message digest or fingerprint. They can be used for "storing" passwords or as a signature that makes it possible to verify that you got the correct message.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/MD5"&gt;MD5&lt;/a&gt; got into problems over 10 years ago and &lt;a href="http://en.wikipedia.org/wiki/SHA-1#Cryptanalysis_of_SHA-1"&gt;SHA-1&lt;/a&gt; could to be heading the same way. Until the &lt;a href="http://www.schneier.com/blog/archives/2007/02/a_new_secure_ha.html"&gt;new standard&lt;/a&gt; is published I would follow the crowd and recommend SHA-256.&lt;/p&gt;

&lt;h5&gt;Ruby&lt;/h5&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;require&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;digest/sha2&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;
&lt;span class="ident"&gt;quickfox&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;The quick brown fox jumps over the lazy dog&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="constant"&gt;Digest&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="constant"&gt;SHA256&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;hexdigest&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;quickfox&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;=&gt; "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592"&lt;/p&gt;

&lt;h5&gt;C#&lt;/h5&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_C# "&gt;using System.Security.Cryptography;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;...&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_C# "&gt; ASCIIEncoding byteConverter = new ASCIIEncoding();
 string quickfox=&amp;quot;The quick brown fox jumps over the lazy dog&amp;quot;;
 HashAlgorithm sha256 = new SHA256Managed();
 byte[] hash = sha256.ComputeHash(byteConverter.GetBytes(quickfox));
 crypt.Text = Convert.ToBase64String(hash);           &lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
      <pubDate>Tue, 27 Feb 2007 17:01:00 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:5159f23a-0740-45e0-b264-b6b6e6f94972</guid>
      <author>Jonas Elfström</author>
      <link>http://alicebobandmallory.com/articles/2007/02/27/what-one-way-hash-function-to-use</link>
      <category>Security</category>
      <category>Ruby</category>
      <category>C#</category>
    </item>
  </channel>
</rss>
