<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>C#</title>
        <link>http://ayende.com/Blog/category/500.aspx</link>
        <description>C#</description>
        <language>en-US</language>
        <copyright>Ayende Rahien</copyright>
        <managingEditor>Ayende@ayende.com</managingEditor>
        <generator>Subtext Version 1.9.3.51</generator>
        <item>
            <title>ReSharper is smarter than me</title>
            <link>http://ayende.com/Blog/archive/2008/03/27/ReSharper-is-smarter-than-me.aspx</link>
            <description>&lt;p&gt;Given the following block of code:&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (presenter.GetServerUrlFromRequest!=&lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
	GetServerUrlFromRequest.Checked = presenter.GetServerUrlFromRequest.Value;
&lt;span style="color: #0000ff"&gt;else&lt;/span&gt;
	GetServerUrlFromRequest.Checked = &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;Resharper gave me the following advice:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/ReSharperissmarterthanme_11B92/image_2.png"&gt;&lt;img height="128" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/ReSharperissmarterthanme_11B92/image_thumb.png" width="426" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;And turned the code to this piece:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre&gt;GetServerUrlFromRequest.Checked = !(presenter.GetServerUrlFromRequest!=&lt;span style="color: #0000ff"&gt;null&lt;/span&gt;) || &lt;br /&gt;                                  presenter.GetServerUrlFromRequest.Value;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;And while it has the same semantics, I actually had to dechiper the code to figure out what it was doing.&lt;/p&gt;

&lt;p&gt;I choose to keep the old version.&lt;/p&gt;&lt;img src="http://ayende.com/Blog/aggbug/10127.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2008/03/27/ReSharper-is-smarter-than-me.aspx</guid>
            <pubDate>Thu, 27 Mar 2008 18:10:15 GMT</pubDate>
            <wfw:comment>http://ayende.com/Blog/comments/10127.aspx</wfw:comment>
            <comments>http://ayende.com/Blog/archive/2008/03/27/ReSharper-is-smarter-than-me.aspx#feedback</comments>
            <slash:comments>22</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10127.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Csc.exe and delegate inference, or: Why C# has awkward syntax</title>
            <link>http://ayende.com/Blog/archive/2008/03/20/Csc.exe-and-delegate-inference-or-Why-C-has-awkward-syntax.aspx</link>
            <description>&lt;p&gt;I just tried to to do a major revamp of Rhino Mocks' interface. It was intended to make it easier to work with C# 3.0 and use the smarter compiler to get better syntax.&lt;/p&gt; &lt;p&gt;I shouldn't have bothered. Take a look at this.&lt;/p&gt;&lt;pre&gt;	&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; TestCsc
	{
		&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; TestMethod()
		{
			Execute(Bar); &lt;span style="color: #008000"&gt;// fail to compile&lt;/span&gt;
			Execute(&lt;span style="color: #0000ff"&gt;delegate&lt;/span&gt;(&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; ia, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; x) { }); &lt;span style="color: #008000"&gt;// compiles fine&lt;/span&gt;
			Execute((&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; i, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; x) =&amp;gt; { &lt;span style="color: #0000ff"&gt;return&lt;/span&gt;; }); &lt;span style="color: #008000"&gt;// Compiles fine&lt;/span&gt;
			Execute((&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; i, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; x) =&amp;gt; { &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;; }); &lt;span style="color: #008000"&gt;// fail to compile&lt;/span&gt;
			Execute(Foo);&lt;span style="color: #008000"&gt;// fail to compile&lt;/span&gt;
			Execute(&lt;span style="color: #0000ff"&gt;delegate&lt;/span&gt;(&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; ia, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; x) { &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;; }); &lt;span style="color: #008000"&gt;// fail to compile&lt;/span&gt;
		}

		&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; Foo(&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; ia, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; x)
		{
			&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;false&lt;/span&gt;;
		}

		&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Bar(&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; ia, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; x)
		{
		}

		&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Execute&amp;lt;T, K&amp;gt;(Action&amp;lt;T, K&amp;gt; e)
		{
			
		}

		&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Execute&amp;lt;T, K&amp;gt;(Func&amp;lt;&lt;span style="color: #0000ff"&gt;bool&lt;/span&gt;, T, K&amp;gt; e)
		{

		}
	}&lt;/pre&gt;&lt;pre&gt;Annoyed.&lt;/pre&gt;&lt;img src="http://ayende.com/Blog/aggbug/10105.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2008/03/20/Csc.exe-and-delegate-inference-or-Why-C-has-awkward-syntax.aspx</guid>
            <pubDate>Thu, 20 Mar 2008 06:18:43 GMT</pubDate>
            <wfw:comment>http://ayende.com/Blog/comments/10105.aspx</wfw:comment>
            <comments>http://ayende.com/Blog/archive/2008/03/20/Csc.exe-and-delegate-inference-or-Why-C-has-awkward-syntax.aspx#feedback</comments>
            <slash:comments>8</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10105.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Generic extension methods</title>
            <link>http://ayende.com/Blog/archive/2008/02/25/Generic-extension-methods.aspx</link>
            <description>&lt;p&gt;I was playing around with the compiler when I hit this interesting feature. I was very surprised to see that this has compiled successfully.&lt;/p&gt;  &lt;blockquote&gt;   &lt;div&gt;     &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;       &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Program&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   2:&lt;/span&gt; {&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   3:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Main(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;[] args)&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   4:&lt;/span&gt;     {&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   5:&lt;/span&gt;         IProcesser&amp;lt;GZipStream&amp;gt; p = &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   6:&lt;/span&gt;         p.HasTimeout();&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   7:&lt;/span&gt;     }&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   8:&lt;/span&gt; }&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   9:&lt;/span&gt;  &lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  10:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Extensions&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  11:&lt;/span&gt; {&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  12:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; HasTimeout&amp;lt;T&amp;gt;(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt; IProcesser&amp;lt;T&amp;gt; s)&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  13:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;where&lt;/span&gt; T : Stream&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  14:&lt;/span&gt;     {&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  15:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; s.Desitnation.CanTimeout;&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  16:&lt;/span&gt;     }&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  17:&lt;/span&gt; }&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  18:&lt;/span&gt;  &lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  19:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;interface&lt;/span&gt; IProcesser&amp;lt;TDestination&amp;gt;&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  20:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;where&lt;/span&gt; TDestination : Stream&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  21:&lt;/span&gt; {&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  22:&lt;/span&gt;     TDestination Desitnation { get; }&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  23:&lt;/span&gt; }&lt;/pre&gt;
    &lt;/div&gt;
  &lt;/div&gt;&lt;/blockquote&gt;&lt;img src="http://ayende.com/Blog/aggbug/10069.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2008/02/25/Generic-extension-methods.aspx</guid>
            <pubDate>Sun, 24 Feb 2008 22:27:05 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2008/02/25/Generic-extension-methods.aspx#feedback</comments>
            <slash:comments>18</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10069.aspx</wfw:commentRss>
        </item>
        <item>
            <title>On Syntactic Sugar and Weight</title>
            <link>http://ayende.com/Blog/archive/2008/02/09/On-Syntactic-Sugar-and-Weight.aspx</link>
            <description>&lt;p&gt;Greg Young posted about &lt;a href="http://codebetter.com/blogs/gregyoung/archive/2008/02/05/dbc-and-iterators.aspx"&gt;some issues&lt;/a&gt; that he run into with yield statements and compile time verifications in Spec#. He ends with:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;Given this information are iterators something that are THAT desirable? Iterators are syntactical sugar, I can duplicate what an iterator does rather simply in code that does not use an iterator … Are they worth me giving up the possibility of removing all of my runtime checks or introducing this complexity?&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;My answer, yes, they are 100% desirable. &lt;/p&gt; &lt;p&gt;OO is syntactic sugar, there is enough object oriented C code out there to prove &lt;em&gt;that&lt;/em&gt;, at least. But syntactic sugar &lt;em&gt;matters&lt;/em&gt;, it matters because it allows you to reduce the complexity and move it to the compiler. &lt;/p&gt; &lt;p&gt;Take Rhino ETL as an example, the whole thing revolves around the idea of iterators. Making it any more complex than it has to will cause it to be infeasible.&lt;/p&gt; &lt;p&gt;The weight of the pattern or approach has direct correlation to its applicability. Dependency Injection doesn't work without an IoC container around, and iterators are far less useful if you don't have yield to make the pain go away.&lt;/p&gt;&lt;img src="http://ayende.com/Blog/aggbug/10037.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2008/02/09/On-Syntactic-Sugar-and-Weight.aspx</guid>
            <pubDate>Sat, 09 Feb 2008 13:19:21 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2008/02/09/On-Syntactic-Sugar-and-Weight.aspx#feedback</comments>
            <slash:comments>5</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10037.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Performance, threading and double checked locks</title>
            <link>http://ayende.com/Blog/archive/2008/01/21/Performance-threading-and-double-checked-locks.aspx</link>
            <description>&lt;p&gt;A very common pattern for lazy initialization of expensive things is this:&lt;/p&gt; &lt;blockquote&gt;&lt;pre&gt;GetDescriptionsHelper.Delegate getDescription;
&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (GetDescriptionsHelper.Cache.TryGetValue(entityType, &lt;span style="color: #0000ff"&gt;out&lt;/span&gt; getDescription) == &lt;span style="color: #0000ff"&gt;false&lt;/span&gt;)
{
	MethodInfo getDescriptionInternalGeneric = getDescriptionInternalMethodInfo.MakeGenericMethod(entityType);
	getDescription = (GetDescriptionsHelper.Delegate)Delegate.CreateDelegate(
		&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(GetDescriptionsHelper.Delegate),
		getDescriptionInternalGeneric);

	GetDescriptionsHelper.Cache.Add(entityType, getDescription);
}&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;This tends to break down when we are talking about code that can run in multiply threads. So we start writing this:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;&lt;span style="color: #0000ff"&gt;lock&lt;/span&gt;(GetDescriptionsHelper.Cache)
{
	GetDescriptionsHelper.Delegate getDescription;
	&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (GetDescriptionsHelper.Cache.TryGetValue(entityType, &lt;span style="color: #0000ff"&gt;out&lt;/span&gt; getDescription) == &lt;span style="color: #0000ff"&gt;false&lt;/span&gt;)
	{
		MethodInfo getDescriptionInternalGeneric = getDescriptionInternalMethodInfo.MakeGenericMethod(entityType);
		getDescription = (GetDescriptionsHelper.Delegate)Delegate.CreateDelegate(
			&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(GetDescriptionsHelper.Delegate),
			getDescriptionInternalGeneric);
	
		GetDescriptionsHelper.Cache.Add(entityType, getDescription);
	}
}&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;Except, this is really bad for performance, because we always lock when we try to get the value out. So we start playing with it and get this:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;GetDescriptionsHelper.Delegate getDescription;
&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (GetDescriptionsHelper.Cache.TryGetValue(entityType, &lt;span style="color: #0000ff"&gt;out&lt;/span&gt; getDescription) == &lt;span style="color: #0000ff"&gt;false&lt;/span&gt;)
{
	&lt;span style="color: #0000ff"&gt;lock&lt;/span&gt;(GetDescriptionsHelper.Cache)
	{	
		MethodInfo getDescriptionInternalGeneric = getDescriptionInternalMethodInfo.MakeGenericMethod(entityType);
		getDescription = (GetDescriptionsHelper.Delegate)Delegate.CreateDelegate(
			&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(GetDescriptionsHelper.Delegate),
			getDescriptionInternalGeneric);
	
		GetDescriptionsHelper.Cache.Add(entityType, getDescription);
	}
}&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;This code has a serious error in it, in the right conditions, two threads will evaluate the if at the same time, and then try to enter the lock at the same time. The end result is an exception as the second of them will try to add the entity type again.&lt;/p&gt;
&lt;p&gt;So we come out with the doubly checked lock, like this:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;GetDescriptionsHelper.Delegate getDescription;
&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (GetDescriptionsHelper.Cache.TryGetValue(entityType, &lt;span style="color: #0000ff"&gt;out&lt;/span&gt; getDescription) == &lt;span style="color: #0000ff"&gt;false&lt;/span&gt;)
{
	&lt;span style="color: #0000ff"&gt;lock&lt;/span&gt;(GetDescriptionsHelper.Cache)
	{	
		&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (GetDescriptionsHelper.Cache.TryGetValue(entityType, &lt;span style="color: #0000ff"&gt;out&lt;/span&gt; getDescription) )
			&lt;span style="color: #0000ff"&gt;return&lt;/span&gt;;

		MethodInfo getDescriptionInternalGeneric = getDescriptionInternalMethodInfo.MakeGenericMethod(entityType);
		getDescription = (GetDescriptionsHelper.Delegate)Delegate.CreateDelegate(
			&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(GetDescriptionsHelper.Delegate),
			getDescriptionInternalGeneric);
	
		GetDescriptionsHelper.Cache.Add(entityType, getDescription);
	}
}&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;Now we are handling this condition as well. But my preference of late has been to use this code in multi threaded environments.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Update: &lt;/strong&gt;I should be wrong more often, I got some very good replies to this post. The code below happened to work by chance and luck alone, apparently. The solution above is the more correct one.&lt;/p&gt;
&lt;p&gt;Actually, it is more complex than that. It is still possible to have readers attempt to read the Cache variable (which is of type Dictionary&amp;lt;Type, Delegate&amp;gt;) while it is being written to inside the lock. There is a potential for serious  bit of mayhem in that case.&lt;/p&gt;
&lt;p&gt;The safe thing to do in this case would be to lock it always before access, but for things that are going to be used frequently (hot spots) this can be a problem. Reader/Writer lock is much worse in terms or performance than the usual lock statement, and ReaderWriterLockSlim is for .NET 3.5 only.&lt;/p&gt;
&lt;p&gt;An interesting dilemma, and I apologize for misleading you earlier.&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;&lt;strike&gt;GetDescriptionsHelper.Delegate getDescription;
&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (GetDescriptionsHelper.Cache.TryGetValue(entityType, &lt;span style="color: #0000ff"&gt;out&lt;/span&gt; getDescription) == &lt;span style="color: #0000ff"&gt;false&lt;/span&gt;)
{
	MethodInfo getDescriptionInternalGeneric = getDescriptionInternalMethodInfo.MakeGenericMethod(entityType);
	getDescription = (GetDescriptionsHelper.Delegate)Delegate.CreateDelegate(
		&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(GetDescriptionsHelper.Delegate),
		getDescriptionInternalGeneric);

	GetDescriptionsHelper.Cache[entityType] = getDescription;
}&lt;/strike&gt;&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;&lt;strike&gt;can you spot the main difference? We are not using locks, and we are using the indexer instead of the Add() method.&lt;/strike&gt;&lt;/p&gt;
&lt;p&gt;&lt;strike&gt;Empirical evidence suggest that using the indexer in a multi threaded environment is safe, in that it doesn't corrupt the dictionary and one of the values will remain there.&lt;/strike&gt;&lt;/p&gt;
&lt;p&gt;&lt;strike&gt;This means that assuming that the cost of creating the value isn't very high, it is just fine to have it created twice on those rare cases, the end result is after the initial flurry, we have the value cached, even if it was calculated more than once. In the long run, it doesn't matter.&lt;/strike&gt;&lt;/p&gt;&lt;img src="http://ayende.com/Blog/aggbug/10011.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2008/01/21/Performance-threading-and-double-checked-locks.aspx</guid>
            <pubDate>Mon, 21 Jan 2008 11:15:57 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2008/01/21/Performance-threading-and-double-checked-locks.aspx#feedback</comments>
            <slash:comments>15</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10011.aspx</wfw:commentRss>
        </item>
        <item>
            <title>System.Type.GUID stability</title>
            <link>http://ayende.com/Blog/archive/2008/01/12/System.Type.GUID-stability.aspx</link>
            <description>&lt;p&gt;Here is an interesting issue. Can you rely on System.Type.GUID to be stable?&lt;/p&gt; &lt;p&gt;By stable I mean that it will generate the same value for the same type across compilations. Empirical evidence suggest that this is the case, with the following factors determining the Guid of a type:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Type name (including the namespace)&lt;/li&gt; &lt;li&gt;Assembly name&lt;/li&gt; &lt;li&gt;Assembly public key&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Reflectoring into the system, it turns out that System.Type.GUID is eventually translated to a call to System.RuntimeType.GetGUID, this is one of the scary InternallCall method that are implemented directly in the runtime itself.&lt;/p&gt; &lt;p&gt;I wonder...&lt;/p&gt;&lt;img src="http://ayende.com/Blog/aggbug/10000.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2008/01/12/System.Type.GUID-stability.aspx</guid>
            <pubDate>Sat, 12 Jan 2008 10:54:23 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2008/01/12/System.Type.GUID-stability.aspx#feedback</comments>
            <slash:comments>5</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10000.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Multi threading challenge: can you spot a bug?</title>
            <link>http://ayende.com/Blog/archive/2007/12/26/Multi-threading-challenge-can-you-spot-a-bug.aspx</link>
            <description>&lt;p&gt;One of the problems of multi threading is that there are a lot of intricacies that you have to deal with. Recently I run into issues that dictated that I would have to write an AsyncBulkInserterAppender  for log4net.&lt;/p&gt; &lt;p&gt;One of the reasons that I want to do that is to avoid locking the application if the database is down or the logging table is locked.I just had a recent issue where this casued a problem. &lt;/p&gt; &lt;p&gt;When I implemented that, I started to worry about what would happen if the database is locked for a long duration. There is a chance that this async logging would block for a long time, and then another async batch would start, also blocking, etc. Eventually, it will fill the thread pool and halt the entire system.&lt;/p&gt; &lt;p&gt;This is the approach I ended up with, it should ensure that there is at most, only two threads that are writing to the database at a time. Since I wrote it, I already found at least two bugs in there. It looks fine now, but I can't think of any good way to really test that.&lt;/p&gt; &lt;p&gt;I am afraid that multi threading can't really be tested successfully. This is something where code review is required.&lt;/p&gt; &lt;p&gt;Here is the code:&lt;/p&gt; &lt;blockquote&gt;&lt;pre&gt;&lt;span style="color: #0000ff"&gt;protected&lt;/span&gt; &lt;span style="color: #0000ff"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; SendBuffer(LoggingEvent[] events)
{
	&lt;span style="color: #008000"&gt;// we accept some additional complexity here&lt;/span&gt;
	&lt;span style="color: #008000"&gt;// in favor of better concurrency. We don't want to&lt;/span&gt;
	&lt;span style="color: #008000"&gt;// block all threads in the pool (train) if we have an issue with&lt;/span&gt;
	&lt;span style="color: #008000"&gt;// the database. Therefor, we perform thread sync to ensure&lt;/span&gt;
	&lt;span style="color: #008000"&gt;// that only a single thread will write to the DB at any given point&lt;/span&gt;
	ThreadPool.QueueUserWorkItem(&lt;span style="color: #0000ff"&gt;delegate&lt;/span&gt;
	{
		&lt;span style="color: #0000ff"&gt;lock&lt;/span&gt; (syncLock)
		{
			eventsList.AddLast(events);
			&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (anotherThreadAlreadyHandlesLogging)
				&lt;span style="color: #0000ff"&gt;return&lt;/span&gt;;&lt;br /&gt;                        anotherThreadAlreadyHandlesLogging = &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;; 
		}
		&lt;span style="color: #0000ff"&gt;while&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;true&lt;/span&gt;)
		{
			LoggingEvent[] current;
			&lt;span style="color: #0000ff"&gt;lock&lt;/span&gt; (syncLock)
			{
				&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;(eventsList.Count == 0)
				{
					anotherThreadAlreadyHandlesLogging = &lt;span style="color: #0000ff"&gt;false&lt;/span&gt;;
					&lt;span style="color: #0000ff"&gt;return&lt;/span&gt;;
				}
				current = eventsList.First.Value;
				eventsList.RemoveFirst();
			}
			PerformWriteToDatabase(current);
		}
	});
}
&lt;font face="Trebuchet MS"&gt;&lt;/font&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;img src="http://ayende.com/Blog/aggbug/9976.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2007/12/26/Multi-threading-challenge-can-you-spot-a-bug.aspx</guid>
            <pubDate>Wed, 26 Dec 2007 10:30:36 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2007/12/26/Multi-threading-challenge-can-you-spot-a-bug.aspx#feedback</comments>
            <slash:comments>15</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/9976.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Statically typed? Compiler checked? Ha!</title>
            <link>http://ayende.com/Blog/archive/2007/12/20/Statically-typed-Compiler-checked-Ha.aspx</link>
            <description>&lt;p&gt;Just a nod toward the people that cling to static typing with both hands, their teeth and the tail:&lt;/p&gt;&lt;pre&gt;RouteTable.Routes.Add(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Route
{
 Url = “admin/[controller]/[action]“,
 Defaults = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt;
 {
  Controller = “Admin“,
  Acton = “Index”
 },
 Validation = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt;
 {
  Conrtoller = “Admin|Users|Categories”
 },
 RouteHandler = &lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(MvcRouteHandler)
});&lt;/pre&gt;
&lt;p&gt;Now, instead of abusing the language to get this, can we get first class support for this things?&lt;/p&gt;&lt;img src="http://ayende.com/Blog/aggbug/9967.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2007/12/20/Statically-typed-Compiler-checked-Ha.aspx</guid>
            <pubDate>Thu, 20 Dec 2007 20:34:52 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2007/12/20/Statically-typed-Compiler-checked-Ha.aspx#feedback</comments>
            <slash:comments>22</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/9967.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Do you trust your compiler? Really trust your compiler?</title>
            <link>http://ayende.com/Blog/archive/2007/12/18/Do-you-trust-your-compiler-Really-trust-your-compiler.aspx</link>
            <description>&lt;p&gt;There is a discussion in the alt.net mailing list right now about how far you can and should trust your compiler. I thinks that this is interesting, because this piece of code of mine is on its way to production:&lt;/p&gt; &lt;blockquote&gt;&lt;pre&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; Guid Create&amp;lt;T&amp;gt;(T newEntity)
{
	&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (CrmService svc = GetCrmService())
	{
		&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; cheatCompiler = newEntity;
		Guid guid = svc.Create((BusinessEntity) cheatCompiler);
		&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; guid;
	}
}
&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;This is part an implementation of an interface in an assembly that cannot reference BusinessEntity.&lt;/p&gt;
&lt;p&gt;I am feeling good with this.&lt;/p&gt;&lt;img src="http://ayende.com/Blog/aggbug/9961.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2007/12/18/Do-you-trust-your-compiler-Really-trust-your-compiler.aspx</guid>
            <pubDate>Tue, 18 Dec 2007 21:29:54 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2007/12/18/Do-you-trust-your-compiler-Really-trust-your-compiler.aspx#feedback</comments>
            <slash:comments>6</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/9961.aspx</wfw:commentRss>
        </item>
        <item>
            <title>The WPF mystery</title>
            <link>http://ayende.com/Blog/archive/2007/12/09/The-WPF-mystery.aspx</link>
            <description>&lt;p&gt;Okay, here are a few weird things that I wish I didn't know about WPF.&lt;/p&gt; &lt;ul&gt; &lt;li&gt;It does some low level cheating all over the place, for fun &amp;amp; profit, put the following like in the main window ctor:&lt;br /&gt;&lt;pre&gt;	Debug.Assert(Application.Current.MainWindow != &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;); &lt;/pre&gt;Remember that on the CLR, you don't often see other objects referencing before you even started your constructor. 
&lt;/li&gt;&lt;li&gt;Then there is this fun puzzle, when I am registering to the Application.Startup event, I am getting inconsistent results with regards to Application.MainWindow being null or not. After a short investigation, I pinned the blame on the async initialization of the windows. But it was a major PITA, to say the least.&lt;/li&gt;&lt;/ul&gt;&lt;img src="http://ayende.com/Blog/aggbug/9931.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2007/12/09/The-WPF-mystery.aspx</guid>
            <pubDate>Sun, 09 Dec 2007 01:53:31 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2007/12/09/The-WPF-mystery.aspx#feedback</comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/9931.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>