<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Jacob Carpenter's Weblog</title>
	<atom:link href="http://jacobcarpenter.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://jacobcarpenter.wordpress.com</link>
	<description>Just another WordPress.com weblog</description>
	<lastBuildDate>Thu, 08 Dec 2011 00:07:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='jacobcarpenter.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Jacob Carpenter's Weblog</title>
		<link>http://jacobcarpenter.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://jacobcarpenter.wordpress.com/osd.xml" title="Jacob Carpenter&#039;s Weblog" />
	<atom:link rel='hub' href='http://jacobcarpenter.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Hello Roslyn</title>
		<link>http://jacobcarpenter.wordpress.com/2011/10/20/hello-roslyn/</link>
		<comments>http://jacobcarpenter.wordpress.com/2011/10/20/hello-roslyn/#comments</comments>
		<pubDate>Thu, 20 Oct 2011 14:55:35 +0000</pubDate>
		<dc:creator>Jacob</dc:creator>
				<category><![CDATA[csharp]]></category>
		<category><![CDATA[Roslyn]]></category>

		<guid isPermaLink="false">https://jacobcarpenter.wordpress.com/2011/10/20/hello-roslyn/</guid>
		<description><![CDATA[using System; using Roslyn.Compilers.CSharp; namespace HelloRoslyn { class Program { static void Main() { string program = Syntax.CompilationUnit( usings: Syntax.List(Syntax.UsingDirective(name: Syntax.ParseName(&#34;System&#34;))), members: Syntax.List&#60;MemberDeclarationSyntax&#62;( Syntax.NamespaceDeclaration( name: Syntax.ParseName(&#34;HelloRoslyn&#34;), members: Syntax.List&#60;MemberDeclarationSyntax&#62;( Syntax.ClassDeclaration( identifier: Syntax.Identifier(&#34;Program&#34;), members: Syntax.List&#60;MemberDeclarationSyntax&#62;( Syntax.MethodDeclaration( returnType: Syntax.PredefinedType(Syntax.Token(SyntaxKind.VoidKeyword)), modifiers: Syntax.TokenList(Syntax.Token(SyntaxKind.StaticKeyword)), identifier: Syntax.ParseToken(&#34;Main&#34;), parameterList: Syntax.ParameterList(), bodyOpt: Syntax.Block( statements: Syntax.List&#60;StatementSyntax&#62;( Syntax.ExpressionStatement( Syntax.InvocationExpression( Syntax.MemberAccessExpression( kind: SyntaxKind.MemberAccessExpression, expression: Syntax.IdentifierName(&#34;Console&#34;), name: Syntax.IdentifierName(&#34;WriteLine&#34;), [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jacobcarpenter.wordpress.com&amp;blog=2133403&amp;post=93&amp;subd=jacobcarpenter&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<pre class="code"><span style="color:blue;">using </span>System;
<span style="color:blue;">using </span>Roslyn.Compilers.CSharp;

<span style="color:blue;">namespace </span>HelloRoslyn
{
  <span style="color:blue;">class </span><span style="color:#2b91af;">Program
  </span>{
    <span style="color:blue;">static void </span>Main()
    {
      <span style="color:blue;">string </span>program = <span style="color:#2b91af;">Syntax</span>.CompilationUnit(
        usings: <span style="color:#2b91af;">Syntax</span>.List(<span style="color:#2b91af;">Syntax</span>.UsingDirective(name: <span style="color:#2b91af;">Syntax</span>.ParseName(<span style="color:#a31515;">&quot;System&quot;</span>))),
        members: <span style="color:#2b91af;">Syntax</span>.List&lt;<span style="color:#2b91af;">MemberDeclarationSyntax</span>&gt;(
          <span style="color:#2b91af;">Syntax</span>.NamespaceDeclaration(
            name: <span style="color:#2b91af;">Syntax</span>.ParseName(<span style="color:#a31515;">&quot;HelloRoslyn&quot;</span>),
            members: <span style="color:#2b91af;">Syntax</span>.List&lt;<span style="color:#2b91af;">MemberDeclarationSyntax</span>&gt;(
              <span style="color:#2b91af;">Syntax</span>.ClassDeclaration(
                identifier: <span style="color:#2b91af;">Syntax</span>.Identifier(<span style="color:#a31515;">&quot;Program&quot;</span>),
                members: <span style="color:#2b91af;">Syntax</span>.List&lt;<span style="color:#2b91af;">MemberDeclarationSyntax</span>&gt;(
                  <span style="color:#2b91af;">Syntax</span>.MethodDeclaration(
                    returnType: <span style="color:#2b91af;">Syntax</span>.PredefinedType(<span style="color:#2b91af;">Syntax</span>.Token(<span style="color:#2b91af;">SyntaxKind</span>.VoidKeyword)),
                    modifiers: <span style="color:#2b91af;">Syntax</span>.TokenList(<span style="color:#2b91af;">Syntax</span>.Token(<span style="color:#2b91af;">SyntaxKind</span>.StaticKeyword)),
                    identifier: <span style="color:#2b91af;">Syntax</span>.ParseToken(<span style="color:#a31515;">&quot;Main&quot;</span>),
                    parameterList: <span style="color:#2b91af;">Syntax</span>.ParameterList(),
                    bodyOpt: <span style="color:#2b91af;">Syntax</span>.Block(
                      statements: <span style="color:#2b91af;">Syntax</span>.List&lt;<span style="color:#2b91af;">StatementSyntax</span>&gt;(
                        <span style="color:#2b91af;">Syntax</span>.ExpressionStatement(
                          <span style="color:#2b91af;">Syntax</span>.InvocationExpression(
                            <span style="color:#2b91af;">Syntax</span>.MemberAccessExpression(
                              kind: <span style="color:#2b91af;">SyntaxKind</span>.MemberAccessExpression,
                              expression: <span style="color:#2b91af;">Syntax</span>.IdentifierName(<span style="color:#a31515;">&quot;Console&quot;</span>),
                              name: <span style="color:#2b91af;">Syntax</span>.IdentifierName(<span style="color:#a31515;">&quot;WriteLine&quot;</span>),
                              operatorToken: <span style="color:#2b91af;">Syntax</span>.Token(<span style="color:#2b91af;">SyntaxKind</span>.DotToken)),
                            <span style="color:#2b91af;">Syntax</span>.ArgumentList(
                              arguments: <span style="color:#2b91af;">Syntax</span>.SeparatedList(
                                <span style="color:#2b91af;">Syntax</span>.Argument(
                                  expression: <span style="color:#2b91af;">Syntax</span>.LiteralExpression(
                                    kind: <span style="color:#2b91af;">SyntaxKind</span>.StringLiteralExpression,
                                    token: <span style="color:#2b91af;">Syntax</span>.Literal(<span style="color:#a31515;">&quot;\&quot;Hello world\&quot;&quot;</span>, <span style="color:#a31515;">&quot;Hello world&quot;</span>)
                                  )
                                )
                              )
                            )
                          )
                        )
                      )
                    )
                  )
                )
              )
            )
          )
        )).Format().GetFullText();

      <span style="color:#2b91af;">Console</span>.WriteLine(program);
    }
  }
}</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jacobcarpenter.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jacobcarpenter.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jacobcarpenter.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jacobcarpenter.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jacobcarpenter.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jacobcarpenter.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jacobcarpenter.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jacobcarpenter.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jacobcarpenter.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jacobcarpenter.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jacobcarpenter.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jacobcarpenter.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jacobcarpenter.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jacobcarpenter.wordpress.com/93/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jacobcarpenter.wordpress.com&amp;blog=2133403&amp;post=93&amp;subd=jacobcarpenter&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jacobcarpenter.wordpress.com/2011/10/20/hello-roslyn/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4c3c432cf8dcc12d98a6edf4d9f76936?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Jacob</media:title>
		</media:content>
	</item>
		<item>
		<title>Reading large xml files</title>
		<link>http://jacobcarpenter.wordpress.com/2010/01/07/reading-large-xml-files/</link>
		<comments>http://jacobcarpenter.wordpress.com/2010/01/07/reading-large-xml-files/#comments</comments>
		<pubDate>Thu, 07 Jan 2010 08:16:06 +0000</pubDate>
		<dc:creator>Jacob</dc:creator>
				<category><![CDATA[csharp]]></category>
		<category><![CDATA[extension methods]]></category>

		<guid isPermaLink="false">http://jacobcarpenter.wordpress.com/2010/01/07/reading-large-xml-files/</guid>
		<description><![CDATA[I’m a huge fan of System.Xml.Linq or “LINQ to XML”. However, some documents really are just too large to efficiently process with an in-memory representation like XDocument. For such documents, we need to consume the xml with a streaming XmlReader instead. As much as I love System.Xml.Linq, that’s how much I hate XmlReader. I don’t [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jacobcarpenter.wordpress.com&amp;blog=2133403&amp;post=68&amp;subd=jacobcarpenter&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I’m a huge fan of <code><a href="http://msdn.microsoft.com/en-us/library/system.xml.linq.aspx">System.Xml.Linq</a></code> or “LINQ to XML”. However, some documents really are just too large to efficiently process with an in-memory representation like <code><a href="http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.aspx">XDocument</a></code>. For such documents, we need to consume the xml with a streaming <code><a href="http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx">XmlReader</a></code> instead.</p>
<p>As much as I love <code>System.Xml.Linq</code>, that’s how much I hate <code>XmlReader</code>. I don’t know why it is, but every time I have to use an <code>XmlReader</code>, I have to go back to the documentation. And working with an <code>XmlReader</code> rarely feels fun.</p>
<p>At <a href="http://code.logos.com/blog/">work</a> (by the way, <a href="http://www.logos.com/jobs">we’re hiring all kinds of developers</a>), we’ve written some really nice code to make reading xml easier. But I’m not at work, and I wanted to process a large set of xml data—namely, the <a href="http://www.gutenberg.org/wiki/Gutenberg:Feeds#The_Project_Gutenberg_Catalog_in_RDF.2FXML_Format">Project Gutenberg catalog in RDF/XML format</a>. So I came up with a simple, efficient solution that I want to share.</p>
<p>The Project Gutenberg catalog data looks something like this:</p>
<pre class="code"><span style="color:blue;">&lt;</span><span style="color:#a31515;">rdf:RDF </span><span style="color:red;">xmlns:rdf</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">http://www.w3.org/1999/02/22-rdf-syntax-ns#</span>&quot;
         <span style="color:red;">xmlns:dc</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">http://purl.org/dc/elements/1.1/</span>&quot;
         <span style="color:red;">xmlns:dcterms</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">http://purl.org/dc/terms/</span>&quot;
         <span style="color:red;">xmlns:cc</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">http://web.resource.org/cc/</span>&quot;
         <span style="color:red;">xmlns:pgterms</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">http://www.gutenberg.org/rdfterms/</span>&quot;<span style="color:blue;">&gt;

    &lt;</span><span style="color:#a31515;">cc:Work </span><span style="color:red;">rdf:about</span><span style="color:blue;">=</span>&quot;&quot;<span style="color:blue;">&gt;
        &lt;</span><span style="color:#a31515;">cc:license </span><span style="color:red;">rdf:resource</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">http://creativecommons.org/licenses/GPL/2.0/</span>&quot; <span style="color:blue;">/&gt;
    &lt;/</span><span style="color:#a31515;">cc:Work</span><span style="color:blue;">&gt;

    &lt;</span><span style="color:#a31515;">cc:License </span><span style="color:red;">rdf:about</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">http://creativecommons.org/licenses/GPL/2.0/</span>&quot;<span style="color:blue;">&gt;
        &lt;!-- </span><span style="color:green;">cc:license children omitted </span><span style="color:blue;">--&gt;
    &lt;/</span><span style="color:#a31515;">cc:License</span><span style="color:blue;">&gt;

    &lt;</span><span style="color:#a31515;">rdf:Description </span><span style="color:red;">rdf:about</span><span style="color:blue;">=</span>&quot;&quot;<span style="color:blue;">&gt;
        &lt;</span><span style="color:#a31515;">dc:created</span><span style="color:blue;">&gt;
            &lt;</span><span style="color:#a31515;">dcterms:W3CDTF</span><span style="color:blue;">&gt;
                &lt;</span><span style="color:#a31515;">rdf:value</span><span style="color:blue;">&gt;</span>2010-01-05<span style="color:blue;">&lt;/</span><span style="color:#a31515;">rdf:value</span><span style="color:blue;">&gt;
            &lt;/</span><span style="color:#a31515;">dcterms:W3CDTF</span><span style="color:blue;">&gt;
        &lt;/</span><span style="color:#a31515;">dc:created</span><span style="color:blue;">&gt;
    &lt;/</span><span style="color:#a31515;">rdf:Description</span><span style="color:blue;">&gt;

    &lt;</span><span style="color:#a31515;">pgterms:etext </span><span style="color:red;">rdf:ID</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">etext14624</span>&quot;<span style="color:blue;">&gt;
        &lt;</span><span style="color:#a31515;">dc:publisher</span><span style="color:blue;">&gt;</span><span style="color:red;">&amp;pg;</span><span style="color:blue;">&lt;/</span><span style="color:#a31515;">dc:publisher</span><span style="color:blue;">&gt;
        &lt;</span><span style="color:#a31515;">dc:title </span><span style="color:red;">rdf:parseType</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">Literal</span>&quot;<span style="color:blue;">&gt;</span>Santa Claus's Partner<span style="color:blue;">&lt;/</span><span style="color:#a31515;">dc:title</span><span style="color:blue;">&gt;
        &lt;</span><span style="color:#a31515;">dc:creator </span><span style="color:red;">rdf:parseType</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">Literal</span>&quot;<span style="color:blue;">&gt;</span>Page, Thomas Nelson, 1853-1922<span style="color:blue;">&lt;/</span><span style="color:#a31515;">dc:creator</span><span style="color:blue;">&gt;
        &lt;</span><span style="color:#a31515;">pgterms:friendlytitle </span><span style="color:red;">rdf:parseType</span><span style="color:blue;">=</span>&quot;<span style="color:blue;">Literal</span>&quot;<span style="color:blue;">&gt;</span>Santa Claus's Partner by Thomas Nelson Page<span style="color:blue;">&lt;/</span><span style="color:#a31515;">pgterms:friendlytitle</span><span style="color:blue;">&gt;
        &lt;</span><span style="color:#a31515;">dc:language</span><span style="color:blue;">&gt;&lt;</span><span style="color:#a31515;">dcterms:ISO639-2</span><span style="color:blue;">&gt;&lt;</span><span style="color:#a31515;">rdf:value</span><span style="color:blue;">&gt;</span>en<span style="color:blue;">&lt;/</span><span style="color:#a31515;">rdf:value</span><span style="color:blue;">&gt;&lt;/</span><span style="color:#a31515;">dcterms:ISO639-2</span><span style="color:blue;">&gt;&lt;/</span><span style="color:#a31515;">dc:language</span><span style="color:blue;">&gt;
        &lt;</span><span style="color:#a31515;">dc:subject</span><span style="color:blue;">&gt;&lt;</span><span style="color:#a31515;">dcterms:LCSH</span><span style="color:blue;">&gt;&lt;</span><span style="color:#a31515;">rdf:value</span><span style="color:blue;">&gt;</span>Christmas stories<span style="color:blue;">&lt;/</span><span style="color:#a31515;">rdf:value</span><span style="color:blue;">&gt;&lt;/</span><span style="color:#a31515;">dcterms:LCSH</span><span style="color:blue;">&gt;&lt;/</span><span style="color:#a31515;">dc:subject</span><span style="color:blue;">&gt;
        &lt;</span><span style="color:#a31515;">dc:subject</span><span style="color:blue;">&gt;&lt;</span><span style="color:#a31515;">dcterms:LCC</span><span style="color:blue;">&gt;&lt;</span><span style="color:#a31515;">rdf:value</span><span style="color:blue;">&gt;</span>PZ<span style="color:blue;">&lt;/</span><span style="color:#a31515;">rdf:value</span><span style="color:blue;">&gt;&lt;/</span><span style="color:#a31515;">dcterms:LCC</span><span style="color:blue;">&gt;&lt;/</span><span style="color:#a31515;">dc:subject</span><span style="color:blue;">&gt;
        &lt;</span><span style="color:#a31515;">dc:created</span><span style="color:blue;">&gt;&lt;</span><span style="color:#a31515;">dcterms:W3CDTF</span><span style="color:blue;">&gt;&lt;</span><span style="color:#a31515;">rdf:value</span><span style="color:blue;">&gt;</span>2005-01-06<span style="color:blue;">&lt;/</span><span style="color:#a31515;">rdf:value</span><span style="color:blue;">&gt;&lt;/</span><span style="color:#a31515;">dcterms:W3CDTF</span><span style="color:blue;">&gt;&lt;/</span><span style="color:#a31515;">dc:created</span><span style="color:blue;">&gt;
        &lt;</span><span style="color:#a31515;">dc:rights </span><span style="color:red;">rdf:resource</span><span style="color:blue;">=</span>&quot;<span style="color:red;">&amp;lic;</span>&quot; <span style="color:blue;">/&gt;
    &lt;/</span><span style="color:#a31515;">pgterms:etext</span><span style="color:blue;">&gt;

    &lt;!-- </span><span style="color:green;">etc. </span><span style="color:blue;">--&gt;

&lt;/</span><span style="color:#a31515;">rdf:RDF</span><span style="color:blue;">&gt;</span></pre>
<p>Let’s first look at the <em>wrong way</em> to read this data:</p>
<pre class="code"><span style="color:blue;">static void </span>Main()
{
    <span style="color:#2b91af;">XNamespace </span>nsGutenbergTerms = <span style="color:#a31515;">&quot;http://www.gutenberg.org/rdfterms/&quot;</span>;
    <span style="color:#2b91af;">XNamespace </span>nsRdf = <span style="color:#a31515;">&quot;http://www.w3.org/1999/02/22-rdf-syntax-ns#&quot;</span>;

    <span style="color:#2b91af;">XDocument </span>doc = <span style="color:#2b91af;">XDocument</span>.Load(<span style="color:#a31515;">&quot;catalog.rdf&quot;</span>);
    <span style="color:blue;">foreach </span>(<span style="color:#2b91af;">XElement </span>etext <span style="color:blue;">in </span>doc.Root.Elements(nsGutenbergTerms + <span style="color:#a31515;">&quot;etext&quot;</span>))
    {
        <span style="color:blue;">string </span>id = (<span style="color:blue;">string</span>) etext.Attribute(nsRdf + <span style="color:#a31515;">&quot;ID&quot;</span>);
        <span style="color:blue;">string </span>title = (<span style="color:blue;">string</span>) etext.Element(nsGutenbergTerms + <span style="color:#a31515;">&quot;friendlytitle&quot;</span>);

        <span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&quot;{0}: {1}&quot;</span>, id, title);
    }
}</pre>
<p>A couple of problems:</p>
<ol>
<li><strong>speed</strong>—the program sits around for 5 seconds or so before outputting anything, while it loads the 128MB xml file into memory. </li>
<li><strong>memory usage</strong>—loading the 128MB file pushes the memory usage from 10,328K to 731,832K (as reported in task manager). I don’t want to read too much into that value, but we can certainly agree that loading the whole file into memory at once isn’t optimal. </li>
</ol>
<p>This is the worst of both worlds: the program is slower than it needs to be, <em>and</em> it uses more memory than it should.</p>
<p>… but did I mention that I love LINQ to XML? Processing each <code>etext</code> element as an <code>XElement</code> instance is <em>really</em> convenient.</p>
<p>Ideally, we would want to combine the efficiency of reading the large xml file with an <code>XmlReader</code> with the convenience of handling each <code>etext</code> element as an <code>XElement</code> instance.</p>
<p>Cue Patrick Stewart saying, “Make it so”:</p>
<pre class="code"><span style="color:blue;">static void </span>Main()
{
    <span style="color:#2b91af;">XNamespace </span>nsGutenbergTerms = <span style="color:#a31515;">&quot;http://www.gutenberg.org/rdfterms/&quot;</span>;
    <span style="color:#2b91af;">XNamespace </span>nsRdf = <span style="color:#a31515;">&quot;http://www.w3.org/1999/02/22-rdf-syntax-ns#&quot;</span>;

    <strong><span style="color:blue;">using </span>(<span style="color:#2b91af;">XmlReader </span>reader = <span style="color:#2b91af;">XmlReader</span>.Create(<span style="color:#a31515;">&quot;catalog.rdf&quot;</span>,
        <span style="color:blue;">new </span><span style="color:#2b91af;">XmlReaderSettings </span>{ ProhibitDtd = <span style="color:blue;">false </span>}))</strong>
    {
        <strong><span style="color:green;">// move the reader to the start of the content and read the root element's start tag
        //   that is, the reader is positioned at the first child of the root element
        </span>reader.MoveToContent();
        reader.ReadStartElement(<span style="color:#a31515;">&quot;RDF&quot;</span>, nsRdf.NamespaceName);

        </strong><span style="color:blue;">foreach </span>(<span style="color:#2b91af;">XElement </span>etext <span style="color:blue;">in </span><strong>reader.ReadElements</strong>(nsGutenbergTerms + <span style="color:#a31515;">&quot;etext&quot;</span>))
        {
            <span style="color:blue;">string </span>id = (<span style="color:blue;">string</span>) etext.Attribute(nsRdf + <span style="color:#a31515;">&quot;ID&quot;</span>);
            <span style="color:blue;">string </span>title = (<span style="color:blue;">string</span>) etext.Element(nsGutenbergTerms + <span style="color:#a31515;">&quot;friendlytitle&quot;</span>);

            <span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&quot;{0}: {1}&quot;</span>, id, title);
        }
    }
}</pre>
<p>Apart from noticing the similarity between this and the previous code block, the most interesting part of this code is the <code>ReadElements</code> extension method.</p>
<p>Before calling <code>ReadElements</code>, the code positions the reader on the first child of the root element. Then, <code>ReadElements</code> is called with an <code><a href="http://msdn.microsoft.com/en-us/library/system.xml.linq.xname.aspx">XName</a></code> referring to the <code>etext</code> element. All of the <code>etext</code> elements are returned as a sequence.</p>
<p>This is <em>exactly</em> what I want: the program starts processing <code>etext</code> elements nearly instantly, and the memory utilization is barely noticeable.</p>
<p>Let’s look at the implementation of <code>ReadElements</code>:</p>
<pre class="code"><span style="color:gray;">/// &lt;summary&gt;
/// </span><span style="color:green;">Returns a sequence of </span><span style="color:gray;">&lt;see cref=&quot;XElement&quot;&gt;</span><span style="color:green;">XElements</span><span style="color:gray;">&lt;/see&gt; </span><span style="color:green;">corresponding to the currently
</span><span style="color:gray;">/// </span><span style="color:green;">positioned element and all following sibling elements which match the specified name.
</span><span style="color:gray;">/// &lt;/summary&gt;
/// &lt;param name=&quot;reader&quot;&gt;</span><span style="color:green;">The xml reader positioned at the desired hierarchy level.</span><span style="color:gray;">&lt;/param&gt;
/// &lt;param name=&quot;elementName&quot;&gt;</span><span style="color:green;">An </span><span style="color:gray;">&lt;see cref=&quot;XName&quot;/&gt; </span><span style="color:green;">representing the name of the desired element.</span><span style="color:gray;">&lt;/param&gt;
/// &lt;returns&gt;</span><span style="color:green;">A sequence of </span><span style="color:gray;">&lt;see cref=&quot;XElement&quot;&gt;</span><span style="color:green;">XElements</span><span style="color:gray;">&lt;/see&gt;</span><span style="color:green;">.</span><span style="color:gray;">&lt;/returns&gt;
/// &lt;remarks&gt;</span><span style="color:green;">At the end of the sequence, the reader will be positioned on the end tag of the parent element.</span><span style="color:gray;">&lt;/remarks&gt;
</span><span style="color:blue;">public static </span><span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">XElement</span>&gt; ReadElements(<span style="color:blue;">this </span><span style="color:#2b91af;">XmlReader </span>reader, <span style="color:#2b91af;">XName </span>elementName)
{
    <span style="color:blue;">if </span>(reader.Name == elementName.LocalName &amp;&amp; reader.NamespaceURI == elementName.NamespaceName)
        <span style="color:blue;">yield return </span>(<span style="color:#2b91af;">XElement</span>) <span style="color:#2b91af;">XElement</span>.ReadFrom(reader);

    <span style="color:blue;">while </span>(reader.ReadToNextSibling(elementName.LocalName, elementName.NamespaceName))
        <span style="color:blue;">yield return </span>(<span style="color:#2b91af;">XElement</span>) <span style="color:#2b91af;">XElement</span>.ReadFrom(reader);
}</pre>
<p>The documentation comments should be pretty self-explanatory, but it’s probably important to call attention to the side effects: <code>ReadElements</code> expects an intentionally positioned xml reader. Once <code>ReadElements</code> is done returning <code>XElements</code>, the reader will be positioned at the end element of the initially positioned element’s parent.</p>
<p>I should also point out it would be trivial to add an overload of <code>ReadElements</code> that didn’t take an <code>XName</code> and simply returned a sequence of the initially positioned element and all of its following siblings. But I don’t need that method yet, so I didn’t write it.</p>
<p><code>ReadElements</code> will certainly allow me to process this large xml file more efficiently and easily than exclusively using either an <code>XDocument</code> or an <code>XmlReader</code>. Hopefully this method will be helpful to some of you, too.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jacobcarpenter.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jacobcarpenter.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jacobcarpenter.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jacobcarpenter.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jacobcarpenter.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jacobcarpenter.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jacobcarpenter.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jacobcarpenter.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jacobcarpenter.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jacobcarpenter.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jacobcarpenter.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jacobcarpenter.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jacobcarpenter.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jacobcarpenter.wordpress.com/68/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jacobcarpenter.wordpress.com&amp;blog=2133403&amp;post=68&amp;subd=jacobcarpenter&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jacobcarpenter.wordpress.com/2010/01/07/reading-large-xml-files/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4c3c432cf8dcc12d98a6edf4d9f76936?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Jacob</media:title>
		</media:content>
	</item>
		<item>
		<title>Google Reader Tip</title>
		<link>http://jacobcarpenter.wordpress.com/2009/03/12/google-reader-tip/</link>
		<comments>http://jacobcarpenter.wordpress.com/2009/03/12/google-reader-tip/#comments</comments>
		<pubDate>Thu, 12 Mar 2009 20:10:25 +0000</pubDate>
		<dc:creator>Jacob</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jacobcarpenter.wordpress.com/2009/03/12/google-reader-tip/</guid>
		<description><![CDATA[Sorry to break a long hiatus with such a lame, non-programming post. But, here’s a tip for Google Reader users: A coworker of mine appears in my google talk list, but was not able to see my shared items in his Reader. We checked the settings a number of times, but to no avail. Another [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jacobcarpenter.wordpress.com&amp;blog=2133403&amp;post=67&amp;subd=jacobcarpenter&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Sorry to break a long hiatus with such a lame, non-programming post. But, here’s a tip for Google Reader users:</p>
<p>A coworker of mine appears in my google talk list, but was not able to see <a href="http://www.google.com/reader/shared/18308957632653099172">my shared items</a> in his Reader. We checked the settings a number of times, but to no avail.</p>
<p>Another coworker recommended the “try changing this, and then changing it back” strategy. I was skeptical, but reluctantly tried it…</p>
<p>And it worked!</p>
<p>So, if you use Google Reader and share items, check to make sure you’re sharing with everyone you intend to:</p>
<p>1. Click sharing settings on the left-hand side of Reader:</p>
<p><a href="http://jacobcarpenter.files.wordpress.com/2009/03/reader001.jpg"><img title="Reader-001" style="display:block;float:none;margin-left:auto;margin-right:auto;border-width:0;" height="244" alt="Reader-001" src="http://jacobcarpenter.files.wordpress.com/2009/03/reader001-thumb.jpg?w=186&#038;h=244" width="186" border="0" /></a></p>
<p>2. Click “Change” on the right-hand side of the newly displayed page:</p>
<p><a href="http://jacobcarpenter.files.wordpress.com/2009/03/reader002.jpg"><img title="Reader-002" style="display:block;float:none;margin-left:auto;margin-right:auto;border-width:0;" height="186" alt="Reader-002" src="http://jacobcarpenter.files.wordpress.com/2009/03/reader002-thumb.jpg?w=244&#038;h=186" width="244" border="0" /></a></p>
<p>3. Toggle the radio button to “Share with ‘Friends’” and click Save:</p>
<p><a href="http://jacobcarpenter.files.wordpress.com/2009/03/reader003.jpg"><img title="Reader-003" style="display:block;float:none;margin-left:auto;margin-right:auto;border-width:0;" height="92" alt="Reader-003" src="http://jacobcarpenter.files.wordpress.com/2009/03/reader003-thumb.jpg?w=244&#038;h=92" width="244" border="0" /></a></p>
<p>Repeat steps 2 and 3, but select “Share with all of my chat contacts,” the second time.</p>
<p>Hope this helps.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jacobcarpenter.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jacobcarpenter.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jacobcarpenter.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jacobcarpenter.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jacobcarpenter.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jacobcarpenter.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jacobcarpenter.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jacobcarpenter.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jacobcarpenter.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jacobcarpenter.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jacobcarpenter.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jacobcarpenter.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jacobcarpenter.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jacobcarpenter.wordpress.com/67/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jacobcarpenter.wordpress.com&amp;blog=2133403&amp;post=67&amp;subd=jacobcarpenter&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jacobcarpenter.wordpress.com/2009/03/12/google-reader-tip/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4c3c432cf8dcc12d98a6edf4d9f76936?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Jacob</media:title>
		</media:content>

		<media:content url="http://jacobcarpenter.files.wordpress.com/2009/03/reader001-thumb.jpg" medium="image">
			<media:title type="html">Reader-001</media:title>
		</media:content>

		<media:content url="http://jacobcarpenter.files.wordpress.com/2009/03/reader002-thumb.jpg" medium="image">
			<media:title type="html">Reader-002</media:title>
		</media:content>

		<media:content url="http://jacobcarpenter.files.wordpress.com/2009/03/reader003-thumb.jpg" medium="image">
			<media:title type="html">Reader-003</media:title>
		</media:content>
	</item>
		<item>
		<title>C# compiler eccentricity of the day: throwing lambda</title>
		<link>http://jacobcarpenter.wordpress.com/2008/10/06/c-compiler-eccentricity-of-the-day-throwing-lambda/</link>
		<comments>http://jacobcarpenter.wordpress.com/2008/10/06/c-compiler-eccentricity-of-the-day-throwing-lambda/#comments</comments>
		<pubDate>Tue, 07 Oct 2008 00:21:27 +0000</pubDate>
		<dc:creator>Jacob</dc:creator>
				<category><![CDATA[csharp]]></category>

		<guid isPermaLink="false">http://jacobcarpenter.wordpress.com/2008/10/06/c-compiler-eccentricity-of-the-day-throwing-lambda/</guid>
		<description><![CDATA[Here at work (gratuitous link; oh yeah, and we’re hiring), we have a Verify helper class. Verify lets you succinctly validate (or verify, if you will) various method invariants. For instance, non-nullness: Verify.IsNotNull(name); The problem with these helper methods is that when an invariant is violated, an exception is thrown far from the actual affected [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jacobcarpenter.wordpress.com&amp;blog=2133403&amp;post=57&amp;subd=jacobcarpenter&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Here at work (<a href="http://code.logos.com/blog/">gratuitous link</a>; oh yeah, and <a href="http://code.logos.com/blog/2008/09/always_looking_for_good_programmers.html">we’re hiring</a>), we have a <code>Verify</code> helper class. <code>Verify</code> lets you succinctly validate (or verify, if you will) various method invariants. For instance, non-nullness:</p>
<pre class="code"><span style="color:rgb(43,145,175);">Verify</span>.IsNotNull(name);</pre>
<p>The problem with these helper methods is that when an invariant is violated, an exception is thrown far from the actual affected bit of code. Always moving one frame up the call stack to see the <em>real</em> code that’s failing quickly gets annoying.</p>
<p>I started thinking about how to mitigate this problem, and realized that with Visual Studio’s excellent debugger support for delegates, the call site could include an <code>Action&lt;Exception&gt;</code> that just threw (<code>throw</code>-ed?):</p>
<pre class="code"><span style="color:rgb(43,145,175);">Verify</span>.IsNotNull(name, ex =&gt; <span style="color:rgb(0,0,255);">throw</span> ex);</pre>
<p>Except that doesn’t compile.</p>
<p>Wrap the lambda body in curly braces (don’t forget the extra semi-colon) and everything works as expected (including the debugger breaking within the lambda!):</p>
<pre class="code"><span style="color:rgb(43,145,175);">Verify</span>.IsNotNull(name, ex =&gt; { <span style="color:rgb(0,0,255);">throw</span> ex; });</pre>
<p>But unfortunately that adds so much syntax that the “solution” is more annoying than the problem I was initially trying to solve.</p>
<p>Has anyone run into anything like this before? Does it make any sense why that statement wouldn’t be valid as a lambda expression?</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jacobcarpenter.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jacobcarpenter.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jacobcarpenter.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jacobcarpenter.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jacobcarpenter.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jacobcarpenter.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jacobcarpenter.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jacobcarpenter.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jacobcarpenter.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jacobcarpenter.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jacobcarpenter.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jacobcarpenter.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jacobcarpenter.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jacobcarpenter.wordpress.com/57/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jacobcarpenter.wordpress.com&amp;blog=2133403&amp;post=57&amp;subd=jacobcarpenter&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jacobcarpenter.wordpress.com/2008/10/06/c-compiler-eccentricity-of-the-day-throwing-lambda/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4c3c432cf8dcc12d98a6edf4d9f76936?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Jacob</media:title>
		</media:content>
	</item>
		<item>
		<title>C# reminder of the day</title>
		<link>http://jacobcarpenter.wordpress.com/2008/07/21/c-reminder-of-the-day/</link>
		<comments>http://jacobcarpenter.wordpress.com/2008/07/21/c-reminder-of-the-day/#comments</comments>
		<pubDate>Tue, 22 Jul 2008 00:24:32 +0000</pubDate>
		<dc:creator>Jacob</dc:creator>
				<category><![CDATA[csharp]]></category>

		<guid isPermaLink="false">http://jacobcarpenter.wordpress.com/2008/07/21/c-reminder-of-the-day/</guid>
		<description><![CDATA[Static data is not shared among constructed generic types. That is, the final line of output from the following program: using System; class Program { static void Main() { NonGeneric.PrintCount(); // &#34;Called 1 time.&#34; NonGeneric.PrintCount(); // &#34;Called 2 times.&#34; Generic&#60;int&#62;.PrintCount(); // &#34;Called 1 time.&#34; Generic&#60;string&#62;.PrintCount(); // ? } public static void DoPrintCount(int count) { Console.WriteLine(&#34;Called [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jacobcarpenter.wordpress.com&amp;blog=2133403&amp;post=54&amp;subd=jacobcarpenter&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>Static data is not shared among constructed generic types.</strong></p>
<p>That is, the final line of output from the following program:</p>
<pre class="code"><span style="color:rgb(0,0,255);">using</span> System;

<span style="color:rgb(0,0,255);">class</span> <span style="color:rgb(43,145,175);">Program
</span>{
    <span style="color:rgb(0,0,255);">static</span> <span style="color:rgb(0,0,255);">void</span> Main()
    {
        <span style="color:rgb(43,145,175);">NonGeneric</span>.PrintCount(); <span style="color:rgb(0,128,0);">// &quot;Called 1 time.&quot;
</span>        <span style="color:rgb(43,145,175);">NonGeneric</span>.PrintCount(); <span style="color:rgb(0,128,0);">// &quot;Called 2 times.&quot;

</span>        <span style="color:rgb(43,145,175);">Generic</span>&lt;<span style="color:rgb(0,0,255);">int</span>&gt;.PrintCount(); <span style="color:rgb(0,128,0);">// &quot;Called 1 time.&quot;
</span>        <span style="color:rgb(43,145,175);">Generic</span>&lt;<span style="color:rgb(0,0,255);">string</span>&gt;.PrintCount(); <span style="color:rgb(0,128,0);">// ?
</span>    }

    <span style="color:rgb(0,0,255);">public</span> <span style="color:rgb(0,0,255);">static</span> <span style="color:rgb(0,0,255);">void</span> DoPrintCount(<span style="color:rgb(0,0,255);">int</span> count)
    {
        <span style="color:rgb(43,145,175);">Console</span>.WriteLine(<span style="color:rgb(163,21,21);">&quot;Called {0} time{1}.&quot;</span>,
            count, count &gt; 1 ? <span style="color:rgb(163,21,21);">&quot;s&quot;</span> : <span style="color:rgb(163,21,21);">&quot;&quot;</span>);
    }
}

<span style="color:rgb(0,0,255);">class</span> <span style="color:rgb(43,145,175);">NonGeneric
</span>{
    <span style="color:rgb(0,0,255);">public</span> <span style="color:rgb(0,0,255);">static</span> <span style="color:rgb(0,0,255);">void</span> PrintCount() { <span style="color:rgb(43,145,175);">Program</span>.DoPrintCount(++count); }
    <span style="color:rgb(0,0,255);">static</span> <span style="color:rgb(0,0,255);">int</span> count;
}

<span style="color:rgb(0,0,255);">class</span> <span style="color:rgb(43,145,175);">Generic</span>&lt;T&gt;
{
    <span style="color:rgb(0,0,255);">public</span> <span style="color:rgb(0,0,255);">static</span> <span style="color:rgb(0,0,255);">void</span> PrintCount() { <span style="color:rgb(43,145,175);">Program</span>.DoPrintCount(++count); }
    <span style="color:rgb(0,0,255);">static</span> <span style="color:rgb(0,0,255);">int</span> count;
}</pre>
<p>Is “Called 1 time.”</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jacobcarpenter.wordpress.com/54/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jacobcarpenter.wordpress.com/54/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jacobcarpenter.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jacobcarpenter.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jacobcarpenter.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jacobcarpenter.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jacobcarpenter.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jacobcarpenter.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jacobcarpenter.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jacobcarpenter.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jacobcarpenter.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jacobcarpenter.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jacobcarpenter.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jacobcarpenter.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jacobcarpenter.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jacobcarpenter.wordpress.com/54/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jacobcarpenter.wordpress.com&amp;blog=2133403&amp;post=54&amp;subd=jacobcarpenter&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jacobcarpenter.wordpress.com/2008/07/21/c-reminder-of-the-day/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4c3c432cf8dcc12d98a6edf4d9f76936?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Jacob</media:title>
		</media:content>
	</item>
		<item>
		<title>Strange Framework design decision of the day&#8230;</title>
		<link>http://jacobcarpenter.wordpress.com/2008/07/16/strange-framework-design-decision-of-the-day/</link>
		<comments>http://jacobcarpenter.wordpress.com/2008/07/16/strange-framework-design-decision-of-the-day/#comments</comments>
		<pubDate>Wed, 16 Jul 2008 23:19:24 +0000</pubDate>
		<dc:creator>Jacob</dc:creator>
				<category><![CDATA[csharp]]></category>

		<guid isPermaLink="false">http://jacobcarpenter.wordpress.com/2008/07/16/strange-framework-design-decision-of-the-day/</guid>
		<description><![CDATA[Today I encountered the strangest .NET Framework design decision I’ve seen in recent times: HashSet&#60;T&#62;’s GetEnumerator method returns a public struct HashSet&#60;T&#62;.Enumerator. Let’s count how many Framework Design Guidelines this violates: 1. Avoid publicly exposed nested types. violation: duh. Do not define a structure [instead of a class] unless the type has all of the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jacobcarpenter.wordpress.com&amp;blog=2133403&amp;post=46&amp;subd=jacobcarpenter&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Today I encountered the strangest .NET Framework design decision I’ve seen in recent times:</p>
<p><code>HashSet&lt;T&gt;</code>’s <code>GetEnumerator</code> method returns a <code>public struct HashSet&lt;T&gt;.Enumerator</code>.</p>
<p>Let’s count how many <a href="http://www.amazon.com/gp/product/0321246756?ie=UTF8&amp;tag=jacocarpswebl-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0321246756">Framework Design Guidelines</a> this violates:</p>
<p>1. <a href="http://msdn.microsoft.com/en-us/library/ms229027(VS.80).aspx">Avoid publicly exposed nested types</a>.</p>
<ul>
<li><strong>violation</strong>: duh. </li>
</ul>
<p><a href="http://msdn.microsoft.com/en-us/library/ms229017(VS.80).aspx">Do not define a structure [instead of a class] unless the type has all of the following characteristics [including]</a>:</p>
<p>2. It is immutable.</p>
<ul>
<li><strong>violation</strong>: calling <code>MoveNext</code> mutates the enumerator object. </li>
</ul>
<p>3. It will not have to be boxed frequently.</p>
<ul>
<li><strong>violation</strong>: passing a <code>HashSet&lt;T&gt;</code> as a parameter to a method that accepts <code>IEnumerable&lt;T&gt;</code> (Linq, anyone?) will hide the class’ <code>GetEnumerator</code> method. Therefore, any calls to <code>GetEnumerator</code> call the interface method which requires boxing the <code>HashSet&lt;T&gt;.Enumerator</code> to return an <code>IEnumerator&lt;T&gt;</code>. </li>
</ul>
<p>4. [Any others you see? <a href="http://jacobcarpenter.wordpress.com/2008/07/16/strange-framework-design-decision-of-the-day/#respond">Leave a comment</a>.]</p>
<p>&#160;</p>
<p>I <em>really</em> want to hear the arguments in favor of the shipping design.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jacobcarpenter.wordpress.com/46/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jacobcarpenter.wordpress.com/46/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jacobcarpenter.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jacobcarpenter.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jacobcarpenter.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jacobcarpenter.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jacobcarpenter.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jacobcarpenter.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jacobcarpenter.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jacobcarpenter.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jacobcarpenter.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jacobcarpenter.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jacobcarpenter.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jacobcarpenter.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jacobcarpenter.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jacobcarpenter.wordpress.com/46/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jacobcarpenter.wordpress.com&amp;blog=2133403&amp;post=46&amp;subd=jacobcarpenter&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jacobcarpenter.wordpress.com/2008/07/16/strange-framework-design-decision-of-the-day/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4c3c432cf8dcc12d98a6edf4d9f76936?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Jacob</media:title>
		</media:content>
	</item>
		<item>
		<title>.Any overload</title>
		<link>http://jacobcarpenter.wordpress.com/2008/06/18/any-overload/</link>
		<comments>http://jacobcarpenter.wordpress.com/2008/06/18/any-overload/#comments</comments>
		<pubDate>Thu, 19 Jun 2008 00:12:05 +0000</pubDate>
		<dc:creator>Jacob</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jacobcarpenter.wordpress.com/2008/06/18/any-overload/</guid>
		<description><![CDATA[Nice: if (folderPaths.Any(path =&#62; !Directory.Exists(path))) throw new DirectoryNotFoundException(); Nicer: string pathNotFound; if (folderPaths.Any(path =&#62; !Directory.Exists(path), out pathNotFound)) throw new DirectoryNotFoundException(&#34;Could not find path: &#34; + pathNotFound); The implementation of public static bool Any&#60;T&#62;(this IEnumerable&#60;T&#62; source, Func&#60;T, bool&#62; predicate, out T found) left as a trivial exercise to the reader.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jacobcarpenter.wordpress.com&amp;blog=2133403&amp;post=45&amp;subd=jacobcarpenter&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Nice:</p>
<pre><span style="color:rgb(0,0,255);">if</span> (folderPaths.Any(path =&gt; !<span style="color:rgb(43,145,175);">Directory</span>.Exists(path)))
    <span style="color:rgb(0,0,255);">throw</span> <span style="color:rgb(0,0,255);">new</span> <span style="color:rgb(43,145,175);">DirectoryNotFoundException</span>();</pre>
<p>Nicer:</p>
<pre><span style="color:rgb(0,0,255);">string</span> pathNotFound;
<span style="color:rgb(0,0,255);">if</span> (folderPaths.Any(path =&gt; !<span style="color:rgb(43,145,175);">Directory</span>.Exists(path), <span style="color:rgb(0,0,255);">out</span> pathNotFound))
    <span style="color:rgb(0,0,255);">throw</span> <span style="color:rgb(0,0,255);">new</span> <span style="color:rgb(43,145,175);">DirectoryNotFoundException</span>(<span style="color:rgb(163,21,21);">&quot;Could not find path: &quot;</span> + pathNotFound);</pre>
<p>The implementation of</p>
<pre>    <span style="color:rgb(0,0,255);">public</span> <span style="color:rgb(0,0,255);">static</span> <span style="color:rgb(0,0,255);">bool</span> Any&lt;T&gt;(<span style="color:rgb(0,0,255);">this</span> <span style="color:rgb(43,145,175);">IEnumerable</span>&lt;T&gt; source,
        <span style="color:rgb(43,145,175);">Func</span>&lt;T, <span style="color:rgb(0,0,255);">bool</span>&gt; predicate, <span style="color:rgb(0,0,255);">out</span> T found)</pre>
<p>left as a trivial exercise to the reader.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jacobcarpenter.wordpress.com/45/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jacobcarpenter.wordpress.com/45/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jacobcarpenter.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jacobcarpenter.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jacobcarpenter.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jacobcarpenter.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jacobcarpenter.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jacobcarpenter.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jacobcarpenter.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jacobcarpenter.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jacobcarpenter.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jacobcarpenter.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jacobcarpenter.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jacobcarpenter.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jacobcarpenter.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jacobcarpenter.wordpress.com/45/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jacobcarpenter.wordpress.com&amp;blog=2133403&amp;post=45&amp;subd=jacobcarpenter&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jacobcarpenter.wordpress.com/2008/06/18/any-overload/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4c3c432cf8dcc12d98a6edf4d9f76936?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Jacob</media:title>
		</media:content>
	</item>
		<item>
		<title>Hello, world!</title>
		<link>http://jacobcarpenter.wordpress.com/2008/06/16/hello-world/</link>
		<comments>http://jacobcarpenter.wordpress.com/2008/06/16/hello-world/#comments</comments>
		<pubDate>Mon, 16 Jun 2008 17:12:42 +0000</pubDate>
		<dc:creator>Jacob</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jacobcarpenter.wordpress.com/2008/06/16/hello-world/</guid>
		<description><![CDATA[Jonas Adam Carpenter; born June 4th @ 6 lbs. 3 oz., 19 in. (Sorry for the entirely non-technical post. I’ll be blogging more C# again, soon.)<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jacobcarpenter.wordpress.com&amp;blog=2133403&amp;post=44&amp;subd=jacobcarpenter&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p align="center"><img style="border-width:0;" height="427" alt="Jonas Adam Carpenter" src="http://jacobcarpenter.files.wordpress.com/2008/06/img-0383.jpg?w=640&#038;h=427" width="640" border="0" /></p>
<p>Jonas Adam Carpenter; born June 4th @ 6 lbs. 3 oz., 19 in.</p>
<p>(Sorry for the entirely non-technical post. I’ll be blogging more C# again, soon.)</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jacobcarpenter.wordpress.com/44/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jacobcarpenter.wordpress.com/44/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jacobcarpenter.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jacobcarpenter.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jacobcarpenter.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jacobcarpenter.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jacobcarpenter.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jacobcarpenter.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jacobcarpenter.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jacobcarpenter.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jacobcarpenter.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jacobcarpenter.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jacobcarpenter.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jacobcarpenter.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jacobcarpenter.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jacobcarpenter.wordpress.com/44/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jacobcarpenter.wordpress.com&amp;blog=2133403&amp;post=44&amp;subd=jacobcarpenter&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jacobcarpenter.wordpress.com/2008/06/16/hello-world/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4c3c432cf8dcc12d98a6edf4d9f76936?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Jacob</media:title>
		</media:content>

		<media:content url="http://jacobcarpenter.files.wordpress.com/2008/06/img-0383.jpg" medium="image">
			<media:title type="html">Jonas Adam Carpenter</media:title>
		</media:content>
	</item>
		<item>
		<title>C# abuse of the day: SwitchOnType</title>
		<link>http://jacobcarpenter.wordpress.com/2008/04/23/c-abuse-of-the-day-switchontype/</link>
		<comments>http://jacobcarpenter.wordpress.com/2008/04/23/c-abuse-of-the-day-switchontype/#comments</comments>
		<pubDate>Thu, 24 Apr 2008 01:30:52 +0000</pubDate>
		<dc:creator>Jacob</dc:creator>
				<category><![CDATA[csharp]]></category>
		<category><![CDATA[extension methods]]></category>

		<guid isPermaLink="false">http://jacobcarpenter.wordpress.com/2008/04/23/c-abuse-of-the-day-switchontype/</guid>
		<description><![CDATA[Today I encountered a situation where I wanted to switch based on a type. Maybe I stayed up a little too late reading Foundations of F#, last night. While this is certainly no pattern matching, it didn&#8217;t seem like terrible C#: DefinitionBase definitionBase = /*...*/; var targetProperty = definitionBase.SwitchOnType( (ColumnDefinition col) =&#62; ColumnDefinition.WidthProperty, (RowDefinition row) [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jacobcarpenter.wordpress.com&amp;blog=2133403&amp;post=42&amp;subd=jacobcarpenter&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Today I encountered a situation where I wanted to switch based on a type. Maybe I stayed up a little too late reading <a href="http://www.amazon.com/gp/product/1590597575?ie=UTF8&amp;tag=jacocarpswebl-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=1590597575">Foundations of F#</a>, last night.</p>
<p>While this is certainly no <a href="http://diditwith.net/2008/02/19/WhyILoveFPatternMatching.aspx">pattern matching</a>, it didn&#8217;t seem like <a href="http://jacobcarpenter.wordpress.com/2008/02/20/generic-abuse-pattern-matching-in-c/">terrible C#</a>:</p>
<pre><span style="color:#2b91af;">DefinitionBase</span> definitionBase = <span style="color:#008000;">/*...*/</span>;

<span style="color:#0000ff;">var</span> targetProperty = definitionBase.SwitchOnType(
        (<span style="color:#2b91af;">ColumnDefinition</span> col) =&gt; <span style="color:#2b91af;">ColumnDefinition</span>.WidthProperty,
        (<span style="color:#2b91af;">RowDefinition</span> row) =&gt; <span style="color:#2b91af;">RowDefinition</span>.HeightProperty);</pre>
<p><a href="http://11011.net/software/vspaste"></a>Note that the lambdas require type decoration (you <em>really</em> don&#8217;t want to explicitly declare the generic parameters on this method).</p>
<p>Here&#8217;s the implementation (taking two <code>Func</code> projections—feel free to overload to your heart&#8217;s content):</p>
<pre><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> TResult SwitchOnType&lt;T, T1, T2, TResult&gt;(<span style="color:#0000ff;">this</span> T source,
    <span style="color:#2b91af;">Func</span>&lt;T1, TResult&gt; act1, <span style="color:#2b91af;">Func</span>&lt;T2, TResult&gt; act2)
{
    <span style="color:#0000ff;">if</span> (source <span style="color:#0000ff;">is</span> T1)
        <span style="color:#0000ff;">return</span> act1((T1) source);

    <span style="color:#0000ff;">if</span> (source <span style="color:#0000ff;">is</span> T2)
        <span style="color:#0000ff;">return</span> act2((T2) source);

    <span style="color:#0000ff;">throw</span> <span style="color:#0000ff;">new</span> <span style="color:#2b91af;">InvalidOperationException</span>(<span style="color:#a31515;">"No matching delegate found"</span>);
}
</pre>
<p>As you can see from the implementation, the method returns the result of the first delegate for which <code>source</code> can be converted into a parameter.</p>
<p>For a <code>default</code> case, add a final delegate that takes <code>object</code>.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jacobcarpenter.wordpress.com/42/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jacobcarpenter.wordpress.com/42/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jacobcarpenter.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jacobcarpenter.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jacobcarpenter.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jacobcarpenter.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jacobcarpenter.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jacobcarpenter.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jacobcarpenter.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jacobcarpenter.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jacobcarpenter.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jacobcarpenter.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jacobcarpenter.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jacobcarpenter.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jacobcarpenter.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jacobcarpenter.wordpress.com/42/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jacobcarpenter.wordpress.com&amp;blog=2133403&amp;post=42&amp;subd=jacobcarpenter&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jacobcarpenter.wordpress.com/2008/04/23/c-abuse-of-the-day-switchontype/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4c3c432cf8dcc12d98a6edf4d9f76936?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Jacob</media:title>
		</media:content>
	</item>
		<item>
		<title>PC#1: A solution</title>
		<link>http://jacobcarpenter.wordpress.com/2008/04/16/pc1-a-solution/</link>
		<comments>http://jacobcarpenter.wordpress.com/2008/04/16/pc1-a-solution/#comments</comments>
		<pubDate>Wed, 16 Apr 2008 20:21:48 +0000</pubDate>
		<dc:creator>Jacob</dc:creator>
				<category><![CDATA[challenge]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[extension methods]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://jacobcarpenter.wordpress.com/2008/04/16/pc1-a-solution/</guid>
		<description><![CDATA[So, when I initially posed the programming challenge #1 I stated: &#8230; since I intended to output HTML, ASP.NET seemed a logical choice. But I was amazed at the amount of code required for such a seemingly simple task (not to mention how ugly code containing &#60;% and %&#62; is!). Well, it turns out, using [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jacobcarpenter.wordpress.com&amp;blog=2133403&amp;post=41&amp;subd=jacobcarpenter&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>So, when I initially posed the <a href="http://jacobcarpenter.wordpress.com/2008/04/15/programming-challenge-1-html-calendar/">programming challenge #1</a> I stated:</p>
<blockquote><p>&#8230; since I intended to output HTML, ASP.NET seemed a logical choice. But I was amazed at the amount of code required for such a seemingly simple task (not to mention how ugly code containing <code>&lt;%</code> and <code>%&gt;</code> is!).</p>
</blockquote>
<p>Well, it turns out, using plain old C# with a little <a href="http://msdn2.microsoft.com/en-us/library/bb308960.aspx#xlinqoverview_topic2e">LINQ to XML functional construction</a> made my solution a lot nicer.</p>
<h4>Prerequisites</h4>
<p>I created a few <code>DateTimeExtensions</code> to enhance readability, though I could have easily inlined the implementation of each of those methods without any <a href="http://en.wikipedia.org/wiki/Lines_of_code">LOC</a> impact.</p>
<pre><span style="color:rgb(0,0,255);">public</span> <span style="color:rgb(0,0,255);">static</span> <span style="color:rgb(0,0,255);">class</span> <span style="color:rgb(43,145,175);">DateTimeExtensions
</span>{
    <span style="color:rgb(0,0,255);">public</span> <span style="color:rgb(0,0,255);">static</span> <span style="color:rgb(43,145,175);">DateTime</span> ToFirstDayOfMonth(<span style="color:rgb(0,0,255);">this</span> <span style="color:rgb(43,145,175);">DateTime</span> dt)
    {
        <span style="color:rgb(0,0,255);">return</span> <span style="color:rgb(0,0,255);">new</span> <span style="color:rgb(43,145,175);">DateTime</span>(dt.Year, dt.Month, 1);
    }
    <span style="color:rgb(0,0,255);">public</span> <span style="color:rgb(0,0,255);">static</span> <span style="color:rgb(43,145,175);">DateTime</span> ToLastDayOfMonth(<span style="color:rgb(0,0,255);">this</span> <span style="color:rgb(43,145,175);">DateTime</span> dt)
    {
        <span style="color:rgb(0,0,255);">return</span> <span style="color:rgb(0,0,255);">new</span> <span style="color:rgb(43,145,175);">DateTime</span>(dt.Year, dt.Month, <span style="color:rgb(43,145,175);">DateTime</span>.DaysInMonth(dt.Year, dt.Month));
    }
    <span style="color:rgb(0,0,255);">public</span> <span style="color:rgb(0,0,255);">static</span> <span style="color:rgb(43,145,175);">DateTime</span> ToFirstDayOfWeek(<span style="color:rgb(0,0,255);">this</span> <span style="color:rgb(43,145,175);">DateTime</span> dt)
    {
        <span style="color:rgb(0,0,255);">return</span> dt.AddDays(-((<span style="color:rgb(0,0,255);">int</span>) dt.DayOfWeek));
    }
    <span style="color:rgb(0,0,255);">public</span> <span style="color:rgb(0,0,255);">static</span> <span style="color:rgb(43,145,175);">DateTime</span> ToLastDayOfWeek(<span style="color:rgb(0,0,255);">this</span> <span style="color:rgb(43,145,175);">DateTime</span> dt)
    {
        <span style="color:rgb(0,0,255);">return</span> dt.AddDays(6 - ((<span style="color:rgb(0,0,255);">int</span>) dt.DayOfWeek));
    }
}</pre>
<p>I also relied on the <a href="http://jacobcarpenter.wordpress.com/2007/11/16/ruby-inspired-extension-method/"><code>Slice</code> extension method I&#8217;ve previously blogged about</a>.</p>
<h4>Solution</h4>
<pre><span style="color:rgb(0,0,255);">static</span> <span style="color:rgb(0,0,255);">void</span> Main(<span style="color:rgb(0,0,255);">string</span>[] args)
{
    <span style="color:rgb(43,145,175);">DateTime</span> today = <span style="color:rgb(43,145,175);">DateTime</span>.Today;
    <span style="color:rgb(43,145,175);">DateTime</span> firstDayOfMonth = today.ToFirstDayOfMonth();
    <span style="color:rgb(43,145,175);">DateTime</span> startCalendar = firstDayOfMonth.ToFirstDayOfWeek();
    <span style="color:rgb(43,145,175);">DateTime</span> lastDayOfMonth = today.ToLastDayOfMonth();
    <span style="color:rgb(43,145,175);">DateTime</span> endCalendar = lastDayOfMonth.ToLastDayOfWeek();

    <span style="color:rgb(0,0,255);">var</span> calendarPrefix =
        <span style="color:rgb(0,0,255);">from</span> day <span style="color:rgb(0,0,255);">in</span> <span style="color:rgb(43,145,175);">Enumerable</span>.Range(startCalendar.Day, (firstDayOfMonth - startCalendar).Days)
        <span style="color:rgb(0,0,255);">select</span> <span style="color:rgb(0,0,255);">new</span> <span style="color:rgb(43,145,175);">XElement</span>(<span style="color:rgb(163,21,21);">"td"</span>, <span style="color:rgb(0,0,255);">new</span> <span style="color:rgb(43,145,175);">XAttribute</span>(<span style="color:rgb(163,21,21);">"class"</span>, <span style="color:rgb(163,21,21);">"prevMonth"</span>), day);
    <span style="color:rgb(0,0,255);">var</span> calendarMonth =
        <span style="color:rgb(0,0,255);">from</span> day <span style="color:rgb(0,0,255);">in</span> <span style="color:rgb(43,145,175);">Enumerable</span>.Range(1, lastDayOfMonth.Day)
        <span style="color:rgb(0,0,255);">select</span> <span style="color:rgb(0,0,255);">new</span> <span style="color:rgb(43,145,175);">XElement</span>(<span style="color:rgb(163,21,21);">"td"</span>, day == today.Day ? <span style="color:rgb(0,0,255);">new</span> <span style="color:rgb(43,145,175);">XAttribute</span>(<span style="color:rgb(163,21,21);">"class"</span>, <span style="color:rgb(163,21,21);">"today"</span>) : <span style="color:rgb(0,0,255);">null</span>, day);
    <span style="color:rgb(0,0,255);">var</span> calendarSuffix =
        <span style="color:rgb(0,0,255);">from</span> day <span style="color:rgb(0,0,255);">in</span> <span style="color:rgb(43,145,175);">Enumerable</span>.Range(1, (endCalendar - lastDayOfMonth).Days)
        <span style="color:rgb(0,0,255);">select</span> <span style="color:rgb(0,0,255);">new</span> <span style="color:rgb(43,145,175);">XElement</span>(<span style="color:rgb(163,21,21);">"td"</span>, <span style="color:rgb(0,0,255);">new</span> <span style="color:rgb(43,145,175);">XAttribute</span>(<span style="color:rgb(163,21,21);">"class"</span>, <span style="color:rgb(163,21,21);">"nextMonth"</span>), day);

    <span style="color:rgb(0,0,255);">var</span> calendar = calendarPrefix.Concat(calendarMonth).Concat(calendarSuffix);

    <span style="color:rgb(0,0,255);">var</span> table = <span style="color:rgb(0,0,255);">new</span> <span style="color:rgb(43,145,175);">XElement</span>(<span style="color:rgb(163,21,21);">"table"</span>,
        <span style="color:rgb(0,0,255);">new</span> <span style="color:rgb(43,145,175);">XElement</span>(<span style="color:rgb(163,21,21);">"thead"</span>,
            <span style="color:rgb(0,0,255);">new</span> <span style="color:rgb(43,145,175);">XElement</span>(<span style="color:rgb(163,21,21);">"tr"</span>,
                <span style="color:rgb(0,0,255);">from</span> offset <span style="color:rgb(0,0,255);">in</span> <span style="color:rgb(43,145,175);">Enumerable</span>.Range(0, 7)
                <span style="color:rgb(0,0,255);">select</span> <span style="color:rgb(0,0,255);">new</span> <span style="color:rgb(43,145,175);">XElement</span>(<span style="color:rgb(163,21,21);">"th"</span>, startCalendar.AddDays(offset).ToString(<span style="color:rgb(163,21,21);">"ddd"</span>)))),
        <span style="color:rgb(0,0,255);">new</span> <span style="color:rgb(43,145,175);">XElement</span>(<span style="color:rgb(163,21,21);">"tbody"</span>,
            <span style="color:rgb(0,0,255);">from</span> week <span style="color:rgb(0,0,255);">in</span> calendar.Slice(7)
            <span style="color:rgb(0,0,255);">select</span> <span style="color:rgb(0,0,255);">new</span> <span style="color:rgb(43,145,175);">XElement</span>(<span style="color:rgb(163,21,21);">"tr"</span>, week)));

    <span style="color:rgb(43,145,175);">Console</span>.WriteLine(table);
}</pre>
<p>I&#8217;d love to see more ways to solve this. If you&#8217;ve got a simpler or more beautiful implementation in your favorite programming langauge/web application framework, <a href="http://jacobcarpenter.wordpress.com/2008/04/15/programming-challenge-1-html-calendar/#postcomment">let me know in the comments</a> of the <a href="http://jacobcarpenter.wordpress.com/2008/04/15/programming-challenge-1-html-calendar/">original post</a>.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jacobcarpenter.wordpress.com/41/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jacobcarpenter.wordpress.com/41/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jacobcarpenter.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jacobcarpenter.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jacobcarpenter.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jacobcarpenter.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jacobcarpenter.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jacobcarpenter.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jacobcarpenter.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jacobcarpenter.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jacobcarpenter.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jacobcarpenter.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jacobcarpenter.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jacobcarpenter.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jacobcarpenter.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jacobcarpenter.wordpress.com/41/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jacobcarpenter.wordpress.com&amp;blog=2133403&amp;post=41&amp;subd=jacobcarpenter&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jacobcarpenter.wordpress.com/2008/04/16/pc1-a-solution/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4c3c432cf8dcc12d98a6edf4d9f76936?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Jacob</media:title>
		</media:content>
	</item>
	</channel>
</rss>
