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

<channel>
	<title>.Net Smoothie &#187; C#</title>
	<atom:link href="http://www.richardbushnell.net/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.richardbushnell.net</link>
	<description></description>
	<lastBuildDate>Wed, 30 Dec 2009 11:42:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>C# Acrobatics : Lambdas and Expression Methods as a replacement for NVelocity</title>
		<link>http://www.richardbushnell.net/2008/06/02/c-acrobatics-lambdas-and-expression-methods-as-a-replacement-for-nvelocity/</link>
		<comments>http://www.richardbushnell.net/2008/06/02/c-acrobatics-lambdas-and-expression-methods-as-a-replacement-for-nvelocity/#comments</comments>
		<pubDate>Mon, 02 Jun 2008 10:09:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<category><![CDATA[Lambda Expressions]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[ListView]]></category>
		<category><![CDATA[NVelocity]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/06/02/c-acrobatics-lambdas-and-expression-methods-as-a-replacement-for-nvelocity/</guid>
		<description><![CDATA[I&#8217;ve been very quiet recently. (I&#8217;m trying to not be so loud, Scott. ) You see, I&#8217;ve been writing a lot of ASP.Net code for a site I&#8217;m working on. And, to be honest, I&#8217;ve been having a lot of trouble. The source code for .Net has been very helpful, and I&#8217;ve learnt a lot [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F06%2F02%2Fc-acrobatics-lambdas-and-expression-methods-as-a-replacement-for-nvelocity%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>I&#8217;ve been very quiet recently. (I&#8217;m trying to not be so <a href="http://www.hanselman.com/blog/ProfessionalismProgrammingAndPunditryAndSuccessAsAMetric.aspx" target="_blank">loud</a>, Scott. <img src='http://www.richardbushnell.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ) You see, I&#8217;ve been writing a lot of ASP.Net code for a site I&#8217;m working on. And, to be honest, I&#8217;ve been having a <em>lot</em> of trouble. The <a href="http://richardbushnell.net/index.php/2008/04/18/the-value-of-being-free-to-see-the-source/" target="_blank">source code for .Net</a> has been very helpful, and I&#8217;ve learnt a lot about what&#8217;s going on under the covers of ASP.Net because of it. </p>
<p>(Note: I won&#8217;t comment here on the quality of the code I&#8217;ve found &#8211; I&#8217;ll leave that up to you to judge. But in any case, I&#8217;ve been trying to build on top of it.)</p>
<p>One thing I&#8217;ve found to be important is the reliance on Web Controls. (It&#8217;s got something to do with javascript libraries, but that&#8217;s another story.) Getting away from the &quot;standard&quot; way to do ASP.Net isn&#8217;t easy though. Even the ninjas on the <a href="http://www.haacked.com/archive/2008/05/31/the-design-is-never-right-the-first-time.aspx" target="_blank">ASP.Net MVC</a> team <a href="http://www.haacked.com/archive/2008/05/03/code-based-repeater-for-asp.net-mvc.aspx" target="_blank">seem to be having trouble</a>. However, with the magic of lambdas and extension methods in C#, I think I might have just about managed to get something usable. I thought I&#8217;d publish my work here, and see what comments I got.</p>
<p>I think it&#8217;s best to start with what my ASP.Net code looks like once I&#8217;ve got everything working. (Notice I still have some Web Controls in there, but that&#8217;s because I&#8217;ve not worked out how to do sorting of data without web controls yet.)</p>
<p>The inspiration for this was taken from the <a href="r" target="_blank">improvements made to NVelocity</a> by the gurus on the Castle Project. I thought it looked great, and I&#8217;d like something similar, but I didn&#8217;t really want to learn a whole new scripting language and integrate it into my working environment just for rendering a bit of HTML. So I built some C# classes to do a similar thing for me instead. It&#8217;s not as nice as NVelocity, but it&#8217;s okay for now.</p>
<p><strong>Warning</strong>: The following code may contain statements of a disturbing nature to more sensitive readers. We cannot be held responsible for any confusion, delusion or mental illness caused by this code.</p>
<p>It starts by taking a collection of Task objects, and calling the extension method &quot;ForEach&quot; on them:</p>
<pre class="code"><span style="background: #ffee62">&lt;%</span> Tasks.ForEach(sections =&gt; {
   sections.NoData = tasks =&gt; {
<span style="background: #ffee62">%&gt;
</span><span style="color: blue">   &lt;</span><span style="color: #a31515">p</span><span style="color: blue">&gt;
</span>   Hey, you've got nothing to do.<span style="color: blue">&lt;/</span><span style="color: #a31515">p</span><span style="color: blue">&gt;
   &lt;</span><span style="color: #a31515">p</span><span style="color: blue">&gt;
</span><span style="background: #ffee62">&lt;%
</span>   };
   sections.BeforeAll = tasks =&gt; {
   <span style="background: #ffee62">%&gt;
</span>  <span style="color: blue">&lt;</span><span style="color: #a31515">table </span><span style="color: red">class</span><span style="color: blue">=&quot;task-list&quot;&gt;
    &lt;</span><span style="color: #a31515">tr </span><span style="color: red">class</span><span style="color: blue">=&quot;task-list-header&quot;&gt;
      &lt;</span><span style="color: #a31515">th</span><span style="color: blue">&gt;
        &lt;</span><span style="color: #a31515">asp</span><span style="color: blue">:</span><span style="color: #a31515">LinkButton </span><span style="color: red">runat</span><span style="color: blue">=&quot;server&quot; </span><span style="color: red">CommandName</span><span style="color: blue">=&quot;Sort&quot; </span><span style="color: red">CommandArgument</span><span style="color: blue">=&quot;StartDate&quot;
          </span><span style="color: red">Text</span><span style="color: blue">=&quot;Started&quot; /&gt;
      &lt;/</span><span style="color: #a31515">th</span><span style="color: blue">&gt;
      &lt;</span><span style="color: #a31515">th</span><span style="color: blue">&gt;
        &lt;</span><span style="color: #a31515">asp</span><span style="color: blue">:</span><span style="color: #a31515">LinkButton </span><span style="color: red">runat</span><span style="color: blue">=&quot;server&quot; </span><span style="color: red">CommandName</span><span style="color: blue">=&quot;Sort&quot; </span><span style="color: red">CommandArgument</span><span style="color: blue">=&quot;DueOn&quot;
          </span><span style="color: red">Text</span><span style="color: blue">=&quot;Due&quot; /&gt;
      &lt;/</span><span style="color: #a31515">th</span><span style="color: blue">&gt;
      &lt;</span><span style="color: #a31515">th</span><span style="color: blue">&gt;
        &lt;</span><span style="color: #a31515">asp</span><span style="color: blue">:</span><span style="color: #a31515">LinkButton </span><span style="color: red">runat</span><span style="color: blue">=&quot;server&quot; </span><span style="color: red">CommandName</span><span style="color: blue">=&quot;Sort&quot; </span><span style="color: red">CommandArgument</span><span style="color: blue">=&quot;Priority&quot;
          </span><span style="color: red">Text</span><span style="color: blue">=&quot;Priority&quot; /&gt;
      &lt;/</span><span style="color: #a31515">th</span><span style="color: blue">&gt;
      &lt;</span><span style="color: #a31515">th</span><span style="color: blue">&gt;
        &lt;</span><span style="color: #a31515">asp</span><span style="color: blue">:</span><span style="color: #a31515">LinkButton </span><span style="color: red">CssClass</span><span style="color: blue">=&quot;task-description&quot; </span><span style="color: red">CommandName</span><span style="color: blue">=&quot;Sort&quot; </span><span style="color: red">CommandArgument</span><span style="color: blue">=&quot;Title&quot;
          </span><span style="color: red">Text</span><span style="color: blue">=&quot;Description&quot; </span><span style="color: red">runat</span><span style="color: blue">=&quot;server&quot; /&gt;
      &lt;/</span><span style="color: #a31515">th</span><span style="color: blue">&gt;</span><span style="color: blue">      &lt;</span><span style="color: #a31515">th</span><span style="color: blue">&gt;
      &lt;/</span><span style="color: #a31515">th</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">tr</span><span style="color: blue">&gt;
   </span><span style="background: #ffee62">&lt;%
</span>   };
     sections.Before = task =&gt; {
    <span style="background: #ffee62">%&gt;
</span>    <span style="color: blue">&lt;</span><span style="color: #a31515">tr </span><span style="color: red">class</span><span style="color: blue">=&quot;</span><span style="background: #ffee62">&lt;%</span>= this.tableCssClasses.Next() <span style="background: #ffee62">%&gt;</span><span style="color: blue">&quot;&gt;
    </span><span style="background: #ffee62">&lt;%
</span>   };
   sections.Each = task =&gt; {
    <span style="background: #ffee62">%&gt;</span>
    <span style="color: blue">&lt;</span><span style="color: #a31515">td</span><span style="color: blue">&gt;
      &lt;</span><span style="color: #a31515">div </span><span style="color: red">class</span><span style="color: blue">='calendar calendar-icon-</span><span style="background: #ffee62">&lt;%</span>= task.StartMonth <span style="background: #ffee62">%&gt;</span><span style="color: blue">'&gt;
        &lt;</span><span style="color: #a31515">div </span><span style="color: red">class</span><span style="color: blue">=&quot;calendar-day&quot;&gt;
          </span><span style="background: #ffee62">&lt;%</span><span style="color: blue">= </span>task.StartDayOfMonth <span style="background: #ffee62">%&gt;</span><span style="color: blue">&lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;
      &lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">td</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">td</span><span style="color: blue">&gt;
      &lt;</span><span style="color: #a31515">div </span><span style="color: red">class</span><span style="color: blue">='calendar calendar-icon-</span><span style="background: #ffee62">&lt;%</span>= task.DueMonth <span style="background: #ffee62">%&gt;</span><span style="color: blue">'&gt;
        &lt;</span><span style="color: #a31515">div </span><span style="color: red">class</span><span style="color: blue">=&quot;calendar-day&quot;&gt;
          </span><span style="background: #ffee62">&lt;%</span><span style="color: blue">= </span>task.DueDayOfMonth <span style="background: #ffee62">%&gt;</span><span style="color: blue">&lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;
      &lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">td</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">td</span><span style="color: blue">&gt;
      </span><span style="background: #ffee62">&lt;%</span><span style="color: blue">= </span>task.Priority <span style="background: #ffee62">%&gt;
</span>    <span style="color: blue">&lt;/</span><span style="color: #a31515">td</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">td </span><span style="color: red">class</span><span style="color: blue">=&quot;task-title&quot;&gt;
      &lt;</span><span style="color: #a31515">a </span><span style="color: red">href</span><span style="color: blue">='</span><span style="background: #ffee62">&lt;%</span>= Href.For(&quot;~/Tasks/{0}/Show.aspx&quot;, task.ID) <span style="background: #ffee62">%&gt;</span><span style="color: blue">'&gt;</span><span style="background: #ffee62">&lt;%</span><span style="color: blue">= </span>task.Title <span style="background: #ffee62">%&gt;</span><span style="color: blue">&lt;/</span><span style="color: #a31515">a</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">td</span><span style="color: blue">&gt;
</span><span style="color: blue">    &lt;</span><span style="color: #a31515">td</span><span style="color: blue">&gt;
      &lt;</span><span style="color: #a31515">asp</span><span style="color: blue">:</span><span style="color: #a31515">Button </span><span style="color: red">ID</span><span style="color: blue">=&quot;Button1&quot; </span><span style="color: red">runat</span><span style="color: blue">=&quot;server&quot; </span><span style="color: red">CssClass</span><span style="color: blue">=&quot;button&quot; </span><span style="color: red">CommandName</span><span style="color: blue">=&quot;Delete&quot; </span><span style="color: red">Text</span><span style="color: blue">=&quot;Mark Done&quot; /&gt;
    &lt;/</span><span style="color: #a31515">td</span><span style="color: blue">&gt;
    </span><span style="background: #ffee62">&lt;%
</span>   };

   sections.After = task =&gt; {
    <span style="background: #ffee62">%&gt;
</span>    <span style="color: blue">&lt;/</span><span style="color: #a31515">tr</span><span style="color: blue">&gt;
    </span><span style="background: #ffee62">&lt;%
</span>   };

   sections.AfterAll = task =&gt; {
    <span style="background: #ffee62">%&gt;
</span>    <span style="color: blue">&lt;/</span><span style="color: #a31515">table</span><span style="color: blue">&gt;
    </span><span style="background: #ffee62">&lt;%
</span>   };
 });
<span style="background: #ffee62">%&gt;</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>It might take a while to grasp what&#8217;s going on here. The code actually starts using an Extension method to IEnumerable that looks like this:</p>
<pre class="code"><span style="color: gray">public static void </span>ForEach&lt;T&gt;(<span style="color: gray">this </span><span style="color: #2b91af">IEnumerable</span>&lt;T&gt; enumerable, <span style="color: #2b91af">ForeachSectionSetter</span>&lt;T&gt; sectionSetter) {
  <span style="color: gray">if </span>(enumerable != <span style="color: gray">null</span>) {

    <span style="color: gray">if </span>(sectionSetter != <span style="color: gray">null</span>) {
      <span style="color: #2b91af">ForeachSections</span>&lt;T&gt; sections = <span style="color: gray">new </span><span style="color: #2b91af">ForeachSections</span>&lt;T&gt;();
      sectionSetter(sections);

      <span style="color: gray">if </span>(enumerable.Count() == 0) {
        <span style="color: gray">if </span>(sections.NoData != <span style="color: gray">null</span>)
          sections.NoData(enumerable);
        <span style="color: gray">return</span>;
      }

      <span style="color: gray">if </span>(sections.BeforeAll != <span style="color: gray">null</span>)
        sections.BeforeAll(enumerable);

      <span style="color: gray">int </span>itemIndex = 0;
      T previousItem = <span style="color: gray">default</span>(T);

      <span style="color: gray">foreach </span>(T item <span style="color: gray">in </span>enumerable) {
        <span style="color: gray">if </span>(sections.Before != <span style="color: gray">null</span>)
          sections.Before(item);
        <span style="color: gray">if </span>(itemIndex % 2 == 1 &amp;&amp; sections.Odd != <span style="color: gray">null</span>)
          sections.Odd(item);
        <span style="color: gray">if </span>(itemIndex % 2 == 0 &amp;&amp; sections.Even != <span style="color: gray">null</span>)
          sections.Even(item);
        <span style="color: gray">if </span>(itemIndex &gt; 0 &amp;&amp; sections.Between != <span style="color: gray">null</span>)
          sections.Between(previousItem, item);
        <span style="color: gray">if </span>(sections.Each != <span style="color: gray">null</span>)
          sections.Each(item);
        <span style="color: gray">if </span>(sections.After != <span style="color: gray">null</span>)
          sections.After(item);
        itemIndex++;
        previousItem = item;
      }
      <span style="color: gray">if </span>(sections.AfterAll != <span style="color: gray">null</span>)
        sections.AfterAll(enumerable);
    }
  }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>The delegate ForEachSectionSetter is used by the calling method with a lambda expression. As a parameter it receives an ForeachSections object, which looks like this:</p>
<pre class="code"><span style="color: gray">public class </span><span style="color: #2b91af">ForeachSections</span>&lt;T&gt; {
  <span style="color: gray">public </span><span style="color: #2b91af">Action</span>&lt;T&gt; Each { <span style="color: gray">get</span>; <span style="color: gray">set</span>; }
  <span style="color: gray">public </span><span style="color: #2b91af">Action</span>&lt;<span style="color: #2b91af">IEnumerable</span>&lt;T&gt;&gt; BeforeAll { <span style="color: gray">get</span>; <span style="color: gray">set</span>; }
  <span style="color: gray">public </span><span style="color: #2b91af">Action</span>&lt;T&gt; Before { <span style="color: gray">get</span>; <span style="color: gray">set</span>; }
  <span style="color: gray">public </span><span style="color: #2b91af">Action</span>&lt;T,T&gt; Between { <span style="color: gray">get</span>; <span style="color: gray">set</span>; }
  <span style="color: gray">public </span><span style="color: #2b91af">Action</span>&lt;T&gt; Odd { <span style="color: gray">get</span>; <span style="color: gray">set</span>; }
  <span style="color: gray">public </span><span style="color: #2b91af">Action</span>&lt;T&gt; Even { <span style="color: gray">get</span>; <span style="color: gray">set</span>; }
  <span style="color: gray">public </span><span style="color: #2b91af">Action</span>&lt;T&gt; After { <span style="color: gray">get</span>; <span style="color: gray">set</span>; }
  <span style="color: gray">public </span><span style="color: #2b91af">Action</span>&lt;<span style="color: #2b91af">IEnumerable</span>&lt;T&gt;&gt; AfterAll { <span style="color: gray">get</span>; <span style="color: gray">set</span>; }
  <span style="color: gray">public </span><span style="color: #2b91af">Action</span>&lt;<span style="color: #2b91af">IEnumerable</span>&lt;T&gt;&gt; NoData { <span style="color: gray">get</span>; <span style="color: gray">set</span>; }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>The calling method gets the chance to set the properties of this class before it is returned to the constructor of the ForEach method for processing. And because each property is already preset to a default value (Null in this case), the constructor can use the ForeachSections object just like a set of default or optional parameters. The caller can simply set values to the properties it needs, and ignore the rest.</p>
<p>If I had tried this another way, using overloadable constructors, it would have led to multiple constructors with indistinguishable signatures. If I&#8217;d have used property initializers, I wouldn&#8217;t have been able to run the whole routine without requiring a second call to the object, which actually wasn&#8217;t possible. </p>
<p>Basically, I couldn&#8217;t think of another way to do it. </p>
<p>The properties of the ForeachSections object are all delegates too. That means that we can use them with lambdas, which gives us lambdas inside of a lambda. (Hmm, very confusing!)</p>
<p>So what do you think? Could <em>you</em> use something like this? Can you make it simpler? Leave me a comment if you can.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F06%2F02%2Fc-acrobatics-lambdas-and-expression-methods-as-a-replacement-for-nvelocity%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/06/02/c-acrobatics-lambdas-and-expression-methods-as-a-replacement-for-nvelocity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Writing Custom Exception Classes the Quick Way</title>
		<link>http://www.richardbushnell.net/2008/02/15/writing-custom-exception-classes-the-quick-way/</link>
		<comments>http://www.richardbushnell.net/2008/02/15/writing-custom-exception-classes-the-quick-way/#comments</comments>
		<pubDate>Fri, 15 Feb 2008 16:00:09 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C# 2.0]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/02/15/writing-custom-exception-classes-the-quick-way/</guid>
		<description><![CDATA[Until recently I thought this was a well-known feature. After demonstrating it a few times, I found out it wasn&#8217;t. A long time ago, in an cubicle far, far away, someone created the .Net Framework. To cut a long story short, they simultaneously produced guidelines for creating Exception classes, which you should always use or [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F02%2F15%2Fwriting-custom-exception-classes-the-quick-way%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>Until recently I thought this was a well-known feature. After demonstrating it a few times, I found out it wasn&#8217;t.</p>
<p>A long time ago, in an cubicle far, far away, someone created the .Net Framework. To cut a long story short, they simultaneously produced guidelines for creating Exception classes, which you should always use or face having your fingernails pulled out with a staple-gun.</p>
<p>The guidelines state:</p>
<blockquote>
<p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Arial','sans-serif'">&#8220;Use the common constructors shown in the following code example when creating exception classes. &#8220;<o:p></o:p></span></p>
<p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'">[C#]<o:p></o:p></span></p>
<p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal"><span style="font-size: 10pt; color: blue; font-family: 'Courier New'">public</span><span style="font-size: 10pt; font-family: 'Courier New'"> <span style="color: blue">class</span> XxxException : ApplicationException<br />
</span><span style="font-size: 10pt; font-family: 'Courier New'">{<br />
</span><span style="font-size: 10pt; font-family: 'Courier New'"><span>   </span><span style="color: blue">public</span> XxxException() {&#8230; }<br />
</span><span style="font-size: 10pt; font-family: 'Courier New'"><span>   </span><span style="color: blue">public</span> XxxException(<span style="color: blue">string</span> message) {&#8230; }<br />
</span><span style="font-size: 10pt; font-family: 'Courier New'"><span>   </span><span style="color: blue">public</span> XxxException(<span style="color: blue">string</span> message, Exception inner) {&#8230; }<br />
</span><span style="font-size: 10pt; font-family: 'Courier New'"><span>   </span><span style="color: blue">public</span> XxxException(SerializationInfo info, StreamingContext context) {&#8230;}<br />
</span><span style="font-size: 10pt; font-family: 'Courier New'">}<o:p></o:p></span></p></blockquote>
<p><span id="more-53"></span><br />
If you don&#8217;t believe me, you can look here:</p>
<p><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconerrorraisinghandlingguidelines.asp" title="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconerrorraisinghandlingguidelines.asp">http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconerrorraisinghandlingguidelines.asp</a></p>
<p>Now, I don&#8217;t know about you, but I can&#8217;t remember all that every time I create a custom exception class. Maybe that&#8217;s why the guidelines also state:</p>
<blockquote><p>In most cases, use the predefined exception types. Only define new exception types for programmatic scenarios, where you expect users of your class library to catch exceptions of this new type and perform a programmatic action based on the exception type itself.</p></blockquote>
<p>But let&#8217;s ignore that. It&#8217;s not relevant.</p>
<p>So, <em>did you know</em> that there is a Visual Studio code snippet that does the work for you? Here&#8217;s how you use it.</p>
<ol>
<li>Create a new class.</li>
<li>inside the namespace definition of the new class, type
<pre class="code">Exception</pre>
<p><a href="http://11011.net/software/vspaste"></a></li>
<li>Press the tab key twice.</li>
</ol>
<p>You then get the following code, free of charge:</p>
<pre class="code">[<span style="color: blue">global</span>::System.<span style="color: #2b91af">Serializable</span>]
<span style="color: blue">public class </span><span style="color: #2b91af">MyException </span>: <span style="color: #2b91af">Exception </span>{
<span style="color: green">
  </span><span style="color: blue">public </span>MyException() { }
  <span style="color: blue">public </span>MyException(<span style="color: blue">string </span>message) : <span style="color: blue">base</span>(message) { }
  <span style="color: blue">public </span>MyException(<span style="color: blue">string </span>message, <span style="color: #2b91af">Exception </span>inner) : <span style="color: blue">base</span>(message, inner) { }
  <span style="color: blue">protected </span>MyException(
  System.Runtime.Serialization.<span style="color: #2b91af">SerializationInfo </span>info,
  System.Runtime.Serialization.<span style="color: #2b91af">StreamingContext </span>context)
    : <span style="color: blue">base</span>(info, context) { }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>What&#8217;s more, you get code snippet functionality which automatically changes the names of all the constructors when you change the name of the class.</p>
<p>I wonder what other code snippets are available. Do you know of any cool ones? What are your favorites?</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F02%2F15%2Fwriting-custom-exception-classes-the-quick-way%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/02/15/writing-custom-exception-classes-the-quick-way/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

