<?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>Meatball&#039;s Guide to .NET &#187; C#</title>
	<atom:link href="http://rogerpence.com/blog/index.php/archives/tag/c/feed" rel="self" type="application/rss+xml" />
	<link>http://rogerpence.com/blog</link>
	<description>A dogma-free guide to making real-world sense of .NET</description>
	<lastBuildDate>Wed, 23 Jun 2010 16:15:35 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Getting to know Resharper: Generate a constructor</title>
		<link>http://rogerpence.com/blog/index.php/archives/230</link>
		<comments>http://rogerpence.com/blog/index.php/archives/230#comments</comments>
		<pubDate>Tue, 04 Aug 2009 20:03:43 +0000</pubDate>
		<dc:creator>rp</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Resharper]]></category>

		<guid isPermaLink="false">http://rogerpence.com/blog/?p=230</guid>
		<description><![CDATA[Resharper, the terrific Visual Studio productivity plugin, reduces adding a constructor for class, with as many arguments as you need, to just a few mouse clicks.Here’s how to use this Resharper feature.
Figure 1a. Class without a constructor.

  1public class Artist
  2{
  3    public int ArtistId { get; set; }
 [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.jetbrains.com/resharper/index.html">Resharper</a>, the terrific Visual Studio productivity plugin, reduces adding a constructor for class, with as many arguments as you need, to just a few mouse clicks.Here’s how to use this Resharper feature.</p>
<div class="sourceCodeHeading">Figure 1a. Class without a constructor.</div>
<div id="Id1046440468SourceCode" class="sourceCode">
<pre class="even"><span class="ln">  1</span><span class="code">public class Artist</span></pre>
<pre class="odd"><span class="ln">  2</span><span class="code">{</span></pre>
<pre class="even"><span class="ln">  3</span><span class="code">    public int ArtistId { get; set; }</span></pre>
<pre class="odd"><span class="ln">  4</span><span class="code">    public string Name { get; set; }</span></pre>
<pre class="even"><span class="ln">  5</span><span class="code">    public int Rank { get; set; }</span></pre>
<pre class="odd"><span class="ln">  6</span><span class="code">}</span></pre>
</div>
<div class="sourceCodeFooter"><a id="Id1046440468SourceCodeToClipboard" class="copyLink" href="javascript:toggleLineNumbers( 'Id1046440468SourceCode' );">Show copy-friendly code</a></div>
<p>Put the cursor somewhere in the body of a class that needs a constructor and press Alt/Ins (or use the Resharper&gt;Code&gt;Generate menu option). This displays Resharper’s <em>Generate</em> context menu (as shown below). Click “<em>Constructor</em>.”</p>
<p><a href="http://rogerpence.com/blog/wp-content/uploads/2009/10/ContextMenu1.jpg"><img style="border-right-width: 0px; margin: 0px 20px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="ContextMenu" border="0" alt="ContextMenu" src="http://rogerpence.com/blog/wp-content/uploads/2009/10/ContextMenu_thumb1.jpg" width="212" height="211" /></a> </p>
<h3>Coding with Resharper is almost like pair programming but minus the gum-chewing, Jonas Brother-listening, obnoxious coder next to you.</h3>
<p>This shows the <em>Generate constructors</em> dialog below. Select the members you want as arguments to the constructor with the checkboxes (selecting the class name selects all of the fields for use as constructor parameters). You can also select the access rights (public, private, et al) as well as whether you want an XML comment block added. Resharper is context sensitive&#8211;when using it to inject a constructor in an abstract class it’s smart enough to mark that constructor<em> protected</em>. Why? <a href="http://msdn.microsoft.com/en-us/library/ms229047.aspx">Read this.</a> For my money, providing this level of awareness is how Resharper elevates itself from being a simple code paster to being a true ally in helping you write better code. It’s almost like pair programming but minus the gum-chewing, Jonas Brother listening, obnoxious coder next to you.</p>
<p>The “Advanced” button lets you specify what arguments are passed on to a base constructor (if any are available).</p>
<p><a href="http://rogerpence.com/blog/wp-content/uploads/2009/10/generate.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; margin-left: 0px; border-left-width: 0px; margin-right: 0px" title="generate" border="0" alt="generate" src="http://rogerpence.com/blog/wp-content/uploads/2009/10/generate_thumb.jpg" width="335" height="335" /></a> </p>
<p>Assuming that the ArtistId and Name fields were selected, when you click “Finish” the constructor shown below is immediately added to the class.</p>
<div class="sourceCodeHeading">Figure 1b. Class with constructor automatically added.</div>
<div id="Id1059101875SourceCode" class="sourceCode">
<pre class="even"><span class="ln">  1</span><span class="code">public class Artist</span></pre>
<pre class="odd"><span class="ln">  2</span><span class="code">{</span></pre>
<pre class="even"><span class="ln">  3</span><span class="code">    public Artist( string name, int artistId )</span></pre>
<pre class="odd"><span class="ln">  4</span><span class="code">    {</span></pre>
<pre class="even"><span class="ln">  5</span><span class="code">        Name = name;</span></pre>
<pre class="odd"><span class="ln">  6</span><span class="code">        ArtistId = artistId;</span></pre>
<pre class="even"><span class="ln">  7</span><span class="code">    }</span></pre>
<pre class="odd"><span class="ln">  8</span><span class="code"></span></pre>
<pre class="even"><span class="ln">  9</span><span class="code">    public int ArtistId { get; set; }</span></pre>
<pre class="odd"><span class="ln"> 10</span><span class="code">    public string Name { get; set; }</span></pre>
<pre class="even"><span class="ln"> 11</span><span class="code">    private int Rank { get; set; }</span></pre>
<pre class="odd"><span class="ln"> 12</span><span class="code">}</span></pre>
</div>
<div class="sourceCodeFooter"><a id="Id1059101875SourceCodeToClipboard" class="copyLink" href="javascript:toggleLineNumbers( 'Id1059101875SourceCode' );">Show copy-friendly code</a></div>
<p>As you can see, Resharper makes it ridiculously easy to automate constructor creation, with optional parameters and value assignments. Although my examples show Resharper working with C#, there is a VB.NET version of Resharper available.</p>
]]></content:encoded>
			<wfw:commentRss>http://rogerpence.com/blog/index.php/archives/230/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Linq to create an ADO.NET connection string</title>
		<link>http://rogerpence.com/blog/index.php/archives/197</link>
		<comments>http://rogerpence.com/blog/index.php/archives/197#comments</comments>
		<pubDate>Wed, 22 Jul 2009 16:57:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[LinqPad]]></category>

		<guid isPermaLink="false">http://rogerpence.com/blog/index.php/archives/197</guid>
		<description><![CDATA[I know I’m a little late to the party, but I am quickly falling in love with Linq. Once you start adopting its functional mindset, it is so expressive and direct. 
I once heard someone way that once you really get Linq, you’ll won’t ever need to write a For/Each loop again. Probably an overstatement, [...]]]></description>
			<content:encoded><![CDATA[<p>I know I’m a little late to the party, but I am quickly falling in love with Linq. Once you start adopting its functional mindset, it is so expressive and direct. </p>
<p>I once heard someone way that once you really get Linq, you’ll won’t ever need to write a For/Each loop again. Probably an overstatement, but still I was intrigued. I set out last night to see how Linq could negate the need for For/Each. </p>
<p>I set out to find a way to create ADO.NET connection strings. In the past, I would have probably used foreach to iterate over a collection using StringBuilder and String.Format to build up the connection string. I dispensed with all that old-school thought and created the connection string as shown below in Figure 1. As a bonus, I also learned (thanks to a little prompting from <a href="http://www.jetbrains.com/resharper/">Resharper</a>) a shortcut for initializing a collection.</p>
<p>The Linq code in lines 10 and 11 is where the cool stuff happens. Each element from the dictionary is selected to create a value pair and the Linq Aggregate() operator is used to concatenate all of the value pairs. All very sweet! </p>
<div class="sourceCodeHeading">Figure 1. Using Linq to create an ADO.NET connection string</div>
<div id="Id1106513593SourceCode" class="sourceCode">
<pre class="even"><span class="ln">  1</span><span class="code">var o = new Dictionary&lt; string, string &gt;()</span></pre>
<pre class="odd"><span class="ln">  2</span><span class="code">	{</span></pre>
<pre class="even"><span class="ln">  3</span><span class="code">		{ &quot;Data Source&quot;,           &quot;DUFFSQLSERVER2008&quot; },</span></pre>
<pre class="odd"><span class="ln">  4</span><span class="code">		{ &quot;Initial Catalog&quot;,       &quot;Chinook&quot; },</span></pre>
<pre class="even"><span class="ln">  5</span><span class="code">		{ &quot;Persist Security Info&quot;, &quot;True&quot; },</span></pre>
<pre class="odd"><span class="ln">  6</span><span class="code">		{ &quot;User ID&quot;,               &quot;xxxxx&quot; },</span></pre>
<pre class="even"><span class="ln">  7</span><span class="code">		{ &quot;Password&quot;,              &quot;xxxxx&quot; }</span></pre>
<pre class="odd"><span class="ln">  8</span><span class="code">	};    </span></pre>
<pre class="even"><span class="ln">  9</span><span class="code"></span></pre>
<pre class="odd"><span class="ln"> 10</span><span class="code">var ConnectionString = o.Select( i =&gt; i.Key + &quot;=&quot; + i.Value ).</span></pre>
<pre class="even"><span class="ln"> 11</span><span class="code">                         Aggregate( ( a, b ) =&gt; a + &quot;;&quot; + b );</span></pre>
<pre class="odd"><span class="ln"> 12</span><span class="code"></span></pre>
<pre class="even"><span class="ln"> 13</span><span class="code">Console.WriteLine( ConnectionString );</span></pre>
</div>
<div class="sourceCodeFooter"><a id="Id1106513593SourceCodeToClipboard" class="copyLink" href="javascript:toggleLineNumbers( 'Id1106513593SourceCode' );">Show copy-friendly code</a></div>
<p>I used <a href="http://www.linqpad.net/">LinqPad</a> to create and test this code. I just started using LinqPad and can’t believe I ever coded without it. Beyond being a great test bed for Linq, it is a superb snippet compiler. LinqPad is a free download and should be in every coder’s toolbox. </p>
<p><a href="http://rogerpence.com/blog/wp-content/uploads/2009/07/linqpad1.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="linqpad" border="0" alt="linqpad" src="http://rogerpence.com/blog/wp-content/uploads/2009/07/linqpad-thumb1.jpg" width="775" height="489" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://rogerpence.com/blog/index.php/archives/197/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
