<?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>Alteridem Consulting &#187; .NET 2.0</title>
	<atom:link href="http://www.alteridem.net/category/net/net-20/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.alteridem.net</link>
	<description>Software by Design</description>
	<lastBuildDate>Fri, 02 Sep 2011 14:21:27 +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>Extending CopyHelper using Generics</title>
		<link>http://www.alteridem.net/2008/07/21/extending-copyhelper-using-generics/</link>
		<comments>http://www.alteridem.net/2008/07/21/extending-copyhelper-using-generics/#comments</comments>
		<pubDate>Mon, 21 Jul 2008 15:33:55 +0000</pubDate>
		<dc:creator>Robert Prouse</dc:creator>
				<category><![CDATA[.NET 2.0]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Libraries]]></category>
		<category><![CDATA[Tips'n'Tricks]]></category>

		<guid isPermaLink="false">http://www.alteridem.net/2008/07/21/extending-copyhelper-using-generics/</guid>
		<description><![CDATA[In my last post, I created a method that does the grunt work of copying data from an instance of one class to an instance of another type. I often find myself copying data between the properties of my data layer classes and those of my user interface like this. // Copy the data from [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://www.alteridem.net/2008/07/09/method-to-copy-data-between-objects-of-different-types/">last post</a>, I created a method that does the grunt work of copying data from an instance of one class to an instance of another type. I often find myself copying data between the properties of my data layer classes and those of my user interface like this.</p>
</p>
<div class="wlWriterSmartContent" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:d56ee549-681a-47e3-98f2-b8699e660de1" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre name="code" class="c#:nogutter">// Copy the data from the customer to the view
view.Address = customer.Address;
view.Country = customer.Country;
view.FirstName = customer.FirstName;
view.LastName = customer.LastName;
view.PostalCode = customer.PostalCode;
view.Province = customer.Province;</pre>
</div>
<p>The newly created <a href="http://www.alteridem.net/2008/07/09/method-to-copy-data-between-objects-of-different-types/"><strong>CopyHelper</strong> class</a> allows me to shorten that to this.</p>
<div class="wlWriterSmartContent" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:106cd999-9de2-4404-bfe0-d08445cb28ca" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre name="code" class="c#:nogutter">// Copy the data from the customer to the view (using reflection in .NET 1.x)
CopyHelper.Copy( typeof(ICustomer), customer, typeof(ICustomerView), view );</pre>
</div>
<p>Today, I want to extend that code using Generics and a <a href="http://en.wikipedia.org/wiki/Fluent_interface">fluent interface</a> so that I can write code like this.</p>
<div class="wlWriterSmartContent" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:21d2f05d-c92d-4fc4-a163-9687494dddde" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre name="code" class="c#:nogutter">// Copy the data from the customer to the view (using reflection and generics in .NET 2.0)
Copier&lt;ICustomer&gt;.Copy( customer ).To&lt;ICustomerView&gt;( view );</pre>
</div>
</p>
<p>Internally, I use my <a href="http://www.alteridem.net/2008/07/09/method-to-copy-data-between-objects-of-different-types/"><strong>CopyHelper</strong> class</a> from my last post. I extend that by creating a generic <strong>Copier</strong> class. I make the constructor private so that it can only be created as a part of the fluent interface, in this case the static copy method. Using the instance of the <strong>Copier</strong> class that was returned from that method, you can then copy <strong>To</strong> or <strong>From</strong> another class instance.</p>
<p>Here is the code.</p>
<div class="wlWriterSmartContent" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:e4d4e61e-e527-494e-b96d-0d4b4f9ba5ea" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre name="code" class="c#">public sealed class Copier&lt;T1&gt; where T1 : class
{
    #region Private Members

    private readonly T1 _subject;

    #endregion

    #region Public Interface

    /// &lt;summary&gt;
    /// Begin the copying process.
    /// &lt;/summary&gt;
    /// &lt;param name="interface1"&gt;The object you are copying from or to&lt;/param&gt;
    /// &lt;returns&gt;An instance of the Copier class so that you can
    /// continue with the copy to/from in a fluent interface.&lt;/returns&gt;
    public static Copier&lt;T1&gt; Copy( T1 interface1 )
    {
        return new Copier&lt;T1&gt;( interface1 );
    }

    #endregion

    #region Construction

    /// &lt;summary&gt;
    /// Private constructor so that it can only be created as a part of a fluent interface.
    /// &lt;/summary&gt;
    /// &lt;param name="subject"&gt;&lt;/param&gt;
    private Copier( T1 subject )
    {
        _subject = subject;
    }

    #endregion

    #region Copier Methods

    /// &lt;summary&gt;
    /// Copies properties from the subject to the passed in object.
    /// &lt;/summary&gt;
    /// &lt;typeparam name="T2"&gt;The type of object you are copying into.&lt;/typeparam&gt;
    /// &lt;param name="to"&gt;The object to copy into.&lt;/param&gt;
    /// &lt;returns&gt;The modified object that you passed in.&lt;/returns&gt;
    public T2 To&lt;T2&gt;( T2 to ) where T2 : class
    {
        CopyHelper.Copy( typeof( T1 ), _subject, typeof( T2 ), to );
        return to;
    }

    /// &lt;summary&gt;
    /// Copies properties from the passed in object into the subject.
    /// &lt;/summary&gt;
    /// &lt;typeparam name="T2"&gt;The type of object you are copying from.&lt;/typeparam&gt;
    /// &lt;param name="from"&gt;The object to copy from.&lt;/param&gt;
    /// &lt;returns&gt;The modified subject that you originally passed in the Copy method.&lt;/returns&gt;
    public T1 From&lt;T2&gt;( T2 from ) where T2 : class
    {
        CopyHelper.Copy( typeof( T2 ), from, typeof( T1 ), _subject );
        return _subject;
    }

    #endregion
}</pre>
</div>
</p>
<p>I would like to constrain <strong>T1</strong> and <strong>T2</strong> to interfaces at compile time, but I am not sure if that can be done. If you have ideas, please post them in the comments. I thought of using reflection to check if <strong>T1</strong> and <strong>T2</strong> are interfaces at run time, but I am a big believer in favouring compile time errors over run time errors.</p>
<p>In my <a href="http://www.alteridem.net/2008/07/22/extending-copyhelper-using-extension-methods/">next post</a>, I am going to use C# 3.0 extension methods to further simplify copying allowing you to write code like this.</p>
<div class="wlWriterSmartContent" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:acf0083b-b819-41ac-801c-41c0f977934e" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre name="code" class="c#:nogutter">// Copy the data from the customer to the view (using extension methods in C# 3.0)
customer.CopyTo&lt;ICustomerView&gt;( view );</pre>
</div>
<p>I am very interested in hearing your feedback on this, so be sure to post to the comments. Do you think this is a good idea? Do you have suggestions for improvements? Let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alteridem.net/2008/07/21/extending-copyhelper-using-generics/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Read Properties from an MSI File</title>
		<link>http://www.alteridem.net/2008/05/20/read-properties-from-an-msi-file/</link>
		<comments>http://www.alteridem.net/2008/05/20/read-properties-from-an-msi-file/#comments</comments>
		<pubDate>Tue, 20 May 2008 18:52:10 +0000</pubDate>
		<dc:creator>Robert Prouse</dc:creator>
				<category><![CDATA[.NET 2.0]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[MSI]]></category>

		<guid isPermaLink="false">http://www.alteridem.net/?p=47</guid>
		<description><![CDATA[Today I was working writing auto-updating for some software. I wanted to base it on the Product Version property in the installer MSI file, so I needed some code to read that from the file. It took a fair amount of searching and code tweaking, but I finally worked it all out. Add a reference [...]]]></description>
			<content:encoded><![CDATA[<p>Today I was working writing auto-updating for some software. I wanted to base it on the Product Version property in the installer MSI file, so I needed some code to read that from the file.</p>
<p>It took a fair amount of searching and code tweaking, but I finally worked it all out.</p>
<ol>
<li>Add a reference to the COM <strong>Microsoft Windows Installer Object Library</strong>.</li>
<li>Add a <span style="font-family: 'Courier New';"><span style="color: #0000ff;">using</span> WindowsInstaller;</span></li>
<li>Add the following static method to your code (error checking removed for brevity.)</li>
</ol>
<pre class="c-sharp:nogutter">public static string GetMsiProperty( string msiFile, string property )
{
   string retVal = string.Empty;

   // Create an Installer instance
   Type classType = Type.GetTypeFromProgID( “WindowsInstaller.Installer” );
   Object installerObj = Activator.CreateInstance( classType );
   Installer installer = installerObj as Installer;

   // Open the msi file for reading
   // 0 - Read, 1 - Read/Write
   Database database = installer.OpenDatabase( msiFile, 0 );

   // Fetch the requested property
   string sql = String.Format(
      “SELECT Value FROM Property WHERE Property=’{0}’”, property );
   View view = database.OpenView( sql );
   view.Execute( null );

   // Read in the fetched record
   Record record = view.Fetch();
   if ( record != null )
      retVal = record.get_StringData( 1 );

   return retVal;
}</pre>
<p>If you want to look up the version, just pass in the name of the MSI file you want to inspect and &#8220;ProductVersion&#8221; for the property you want to return. For example;</p>
<pre class="c-sharp:nogutter">string version = GetMsiProperty( msiFile, “ProductVersion” );</pre>
<p>You can use this method to look up other properties in the installer. Some common ones you might want are <strong>ProductName, ProductCode, UpgradeCode, Manufacturer, ARPHELPLINK, ARPCOMMENTS, ARPCONTACT, ARPURLINFOABOUT</strong> and <strong>ARPURLUDATEINFO</strong>. For a full list of properties, see the <a href="http://msdn.microsoft.com/en-us/library/aa370905(VS.85).aspx">MSDN Reference</a>, but remember that most of the properties listed on that page are only for already installed applications and won&#8217;t be included in the installer.</p>
<p>If you find this code useful or the code is not self-explanatory, please leave a comment.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alteridem.net/2008/05/20/read-properties-from-an-msi-file/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>The Stopwatch Class in .NET</title>
		<link>http://www.alteridem.net/2008/01/14/the-stopwatch-class-in-net/</link>
		<comments>http://www.alteridem.net/2008/01/14/the-stopwatch-class-in-net/#comments</comments>
		<pubDate>Mon, 14 Jan 2008 20:13:21 +0000</pubDate>
		<dc:creator>Robert Prouse</dc:creator>
				<category><![CDATA[.NET 2.0]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Tips'n'Tricks]]></category>

		<guid isPermaLink="false">http://www.alteridem.net/2008/01/14/the-stopwatch-class-in-net/</guid>
		<description><![CDATA[Do you ever find yourself using DateTime to time a section of code?&#160; Do you have code like the following? DateTime start = DateTime.Now; // Perform a long process Thread.Sleep( 1968 ); DateTime end = DateTime.Now; TimeSpan duration = end.Subtract( start ); Console.WriteLine( "This process took {0} ms", duration.TotalMilliseconds ); If you do, you should [...]]]></description>
			<content:encoded><![CDATA[<p>Do you ever find yourself using <strong>DateTime</strong> to time a section of code?&nbsp; Do you have code like the following?</p>
<pre class="code">  <span style="color: rgb(128,0,128)">DateTime</span> start <span style="color: rgb(255,0,0)">=</span> <span style="color: rgb(128,0,128)">DateTime</span><span style="color: rgb(255,0,0)">.</span>Now;

  <span style="color: rgb(0,128,0)">// Perform a long process
</span>   <span style="color: rgb(43,145,175)">Thread</span><span style="color: rgb(255,0,0)">.</span>Sleep( <span style="color: rgb(128,0,128)">1968</span> );

  <span style="color: rgb(128,0,128)">DateTime</span> end <span style="color: rgb(255,0,0)">=</span> <span style="color: rgb(128,0,128)">DateTime</span><span style="color: rgb(255,0,0)">.</span>Now;
  <span style="color: rgb(128,0,128)">TimeSpan</span> duration <span style="color: rgb(255,0,0)">=</span> end<span style="color: rgb(255,0,0)">.</span>Subtract( start );
  <span style="color: rgb(43,145,175)">Console</span><span style="color: rgb(255,0,0)">.</span>WriteLine( <span style="color: rgb(163,21,21)">"This process took {0} ms"</span>,
    duration<span style="color: rgb(255,0,0)">.</span>TotalMilliseconds );</pre>
<p>If you do, you should look at the <strong><a href="http://msdn2.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx">System.Diagnostics.Stopwatch</a></strong> class that was introduced in the 2.0 framework.&nbsp; You can convert the code above to the much more readable</p>
<pre class="code">  <span style="color: rgb(43,145,175)">Stopwatch</span> stopwatch <span style="color: rgb(255,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(43,145,175)">Stopwatch</span>();
  stopwatch<span style="color: rgb(255,0,0)">.</span>Start();

  <span style="color: rgb(0,128,0)">// Perform a long process
</span>  <span style="color: rgb(43,145,175)">Thread</span><span style="color: rgb(255,0,0)">.</span>Sleep( <span style="color: rgb(128,0,128)">1968</span> );

  stopwatch<span style="color: rgb(255,0,0)">.</span>Stop();
  <span style="color: rgb(43,145,175)">Console</span><span style="color: rgb(255,0,0)">.</span>WriteLine( <span style="color: rgb(163,21,21)">"This process took {0} ms"</span>,
    stopwatch<span style="color: rgb(255,0,0)">.</span>ElapsedMilliseconds );</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.alteridem.net/2008/01/14/the-stopwatch-class-in-net/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>The Yield Statement in C#</title>
		<link>http://www.alteridem.net/2007/08/22/the-yield-statement-in-c/</link>
		<comments>http://www.alteridem.net/2007/08/22/the-yield-statement-in-c/#comments</comments>
		<pubDate>Wed, 22 Aug 2007 20:50:50 +0000</pubDate>
		<dc:creator>Robert Prouse</dc:creator>
				<category><![CDATA[.NET 2.0]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.alteridem.net/2007/08/22/the-yield-statement-in-c/</guid>
		<description><![CDATA[Another often overlooked C# statement that was introduced in .NET 2.0 is yield. This keyword is used to return items from a loop within a method and retain the state of the method through multiple calls. That is a bit hard to wrap your head around, so as always, an example will help; public static [...]]]></description>
			<content:encoded><![CDATA[<p>Another often overlooked C# statement that was introduced in .NET 2.0 is <strong>yield</strong>. This keyword is used to return items from a loop within a method and retain the state of the method through multiple calls. That is a bit hard to wrap your head around, so as always, an example will help;</p>
<pre class="code"><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)">IEnumerable</span><span style="color: rgb(255,0,0)">&lt;</span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(255,0,0)">&gt;</span> Range( <span style="color: rgb(0,0,255)">int</span> min, <span style="color: rgb(0,0,255)">int</span> max )
{
   <span style="color: rgb(0,0,255)">for</span> ( <span style="color: rgb(0,0,255)">int</span> i <span style="color: rgb(255,0,0)">=</span> min; i <span style="color: rgb(255,0,0)">&lt;</span> max; i<span style="color: rgb(255,0,0)">++</span> )
   {
      <span style="color: rgb(0,0,255)">yield</span> <span style="color: rgb(0,0,255)">return</span> i;
   }
}</pre>
<p>Each time <strong>Range</strong> is called, it will return one value in the given range. Of interest is that it maintains the state between calls. The best way to think of it is that for the first call to the method, execution starts at the first line and continues until it hits a yield statement at which time it returns the value. The subsequent runs through the method continue execution at the statement after the yield, continuing to yield values or exiting at the end of the method.</p>
<p>Using this, you can write the following code;</p>
<pre class="code"><span style="color: rgb(0,0,255)">foreach</span> ( <span style="color: rgb(0,0,255)">int</span> i <span style="color: rgb(0,0,255)">in</span> Range( <span style="color: rgb(128,0,128)">10</span>, <span style="color: rgb(128,0,128)">20</span> ) )
{
   <span style="color: rgb(43,145,175)">Console</span><span style="color: rgb(255,0,0)">.</span>Write( i<span style="color: rgb(255,0,0)">.</span>ToString() <span style="color: rgb(255,0,0)">+</span> <span style="color: rgb(163,21,21)">" "</span> );
}</pre>
<p>Which will return the following;</p>
<p>10 11 12 13 14 15 16 17 18 19</p>
<p>Why bother you say? Why not just iterate through the numbers yourself? The answer&nbsp;lies in the fact that each call maintains state, so you don&#8217;t have to. The above example doesn&#8217;t really show you the power, so let&#8217;s try a more complex example, calculating prime numbers.</p>
<pre class="code"><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)">IEnumerable</span><span style="color: rgb(255,0,0)">&lt;</span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(255,0,0)">&gt;</span> Primes( <span style="color: rgb(0,0,255)">int</span> max )
{
   <span style="color: rgb(0,0,255)">yield</span> <span style="color: rgb(0,0,255)">return</span> <span style="color: rgb(128,0,128)">2</span>;
   <span style="color: rgb(43,145,175)">List</span><span style="color: rgb(255,0,0)">&lt;</span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(255,0,0)">&gt;</span> found <span style="color: rgb(255,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(43,145,175)">List</span><span style="color: rgb(255,0,0)">&lt;</span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(255,0,0)">&gt;</span>();
   found<span style="color: rgb(255,0,0)">.</span>Add( <span style="color: rgb(128,0,128)">3</span> );
   <span style="color: rgb(0,0,255)">int</span> candidate <span style="color: rgb(255,0,0)">=</span> <span style="color: rgb(128,0,128)">3</span>;
   <span style="color: rgb(0,0,255)">while</span> ( candidate <span style="color: rgb(255,0,0)">&lt;=</span> max )
   {
      <span style="color: rgb(0,0,255)">bool</span> isPrime <span style="color: rgb(255,0,0)">=</span> <span style="color: rgb(0,0,255)">true</span>;
      <span style="color: rgb(0,0,255)">foreach</span> ( <span style="color: rgb(0,0,255)">int</span> prime <span style="color: rgb(0,0,255)">in</span> found )
      {
         <span style="color: rgb(0,0,255)">if</span> ( prime <span style="color: rgb(255,0,0)">*</span> prime <span style="color: rgb(255,0,0)">&gt;</span> candidate )
         {
            <span style="color: rgb(0,0,255)">break</span>;
         }
         <span style="color: rgb(0,0,255)">if</span> ( candidate <span style="color: rgb(255,0,0)">%</span> prime <span style="color: rgb(255,0,0)">==</span> <span style="color: rgb(128,0,128)">0</span> )
         {
            isPrime <span style="color: rgb(255,0,0)">=</span> <span style="color: rgb(0,0,255)">false</span>;
            <span style="color: rgb(0,0,255)">break</span>;
         }
      }
      <span style="color: rgb(0,0,255)">if</span> ( isPrime )
      {
         found<span style="color: rgb(255,0,0)">.</span>Add( candidate );
         <span style="color: rgb(0,0,255)">yield</span> <span style="color: rgb(0,0,255)">return</span> candidate;
      }
      candidate <span style="color: rgb(255,0,0)">+=</span> <span style="color: rgb(128,0,128)">2</span>;
   }
}</pre>
<p>Notice that there are multiple yields within this method and that the state is maintained through each call. You can now print out all of the prime numbers below 50 with;</p>
<pre class="code"><span style="color: rgb(0,0,255)">foreach</span> ( <span style="color: rgb(0,0,255)">int</span> prime <span style="color: rgb(0,0,255)">in</span> Primes( <span style="color: rgb(128,0,128)">50</span> ) )
{
   <span style="color: rgb(43,145,175)">Console</span><span style="color: rgb(255,0,0)">.</span>Write( prime<span style="color: rgb(255,0,0)">.</span>ToString() <span style="color: rgb(255,0,0)">+</span> <span style="color: rgb(163,21,21)">" "</span> );
}</pre>
<p>There is also the <strong>yield break</strong> statement. If a <strong>yield break</strong> statement is hit within a method, execution of that method stops with no return. Using this, the first method could be rewritten like this;</p>
<pre class="code"><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)">IEnumerable</span><span style="color: rgb(255,0,0)">&lt;</span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(255,0,0)">&gt;</span> Range( <span style="color: rgb(0,0,255)">int</span> min, <span style="color: rgb(0,0,255)">int</span> max )
{
   <span style="color: rgb(0,0,255)">while</span> ( <span style="color: rgb(0,0,255)">true</span> )
   {
      <span style="color: rgb(0,0,255)">if</span> ( min <span style="color: rgb(255,0,0)">&gt;=</span> max )
      {
         <span style="color: rgb(0,0,255)">yield</span> <span style="color: rgb(0,0,255)">break</span>;
      }
      <span style="color: rgb(0,0,255)">yield</span> <span style="color: rgb(0,0,255)">return</span> min<span style="color: rgb(255,0,0)">++</span>;
   }
}</pre>
<p>It is not as useful in this case, but I&#8217;ll leave it to you to find more interesting reasons to break out of a loop. Also, even though I used the generic <strong>IEnumerable&lt;T&gt;</strong> for all of my examples, you can also use <strong>IEnumerable</strong>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alteridem.net/2007/08/22/the-yield-statement-in-c/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Null Coalescing Operator</title>
		<link>http://www.alteridem.net/2007/08/17/null-coalescing-operator/</link>
		<comments>http://www.alteridem.net/2007/08/17/null-coalescing-operator/#comments</comments>
		<pubDate>Fri, 17 Aug 2007 18:11:38 +0000</pubDate>
		<dc:creator>Robert Prouse</dc:creator>
				<category><![CDATA[.NET 2.0]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.alteridem.net/2007/08/17/null-coalescing-operator/</guid>
		<description><![CDATA[The first time I saw the ?? operator in C#, I did a double take and had to look it up. The operator, called the null coalescing operator was added in C# 2.0 and is pretty useful, but still fairly unknown. Order order = GetOrder( id ) ?? new Order(); In the above code, if [...]]]></description>
			<content:encoded><![CDATA[<p>The first time I saw the ?? operator in C#, I did a double take and had to look it up. The operator, called the null coalescing operator was added in C# 2.0 and is pretty useful, but still fairly unknown.</p>
<pre class="code"><span style="color: rgb(43,145,175)"></span></pre>
<pre class="code">Order order <span style="color: rgb(255,0,0)">=</span> GetOrder( id ) <span style="color: rgb(255,0,0)">??</span> <span style="color: rgb(0,0,255)">new</span> Order();</pre>
<p>In the above code, if the return value of&nbsp;GetOrder()&nbsp;is not null, it is assigned to order. If it is null,&nbsp;a new Order is created and assigned. Before the ?? operator, you would have to write something like this;</p>
<pre class="code">Order order <span style="color: rgb(255,0,0)">=</span> GetOrder( id );
<span style="color: rgb(0,0,255)">if</span> ( order <span style="color: rgb(255,0,0)">==</span> <span style="color: rgb(0,0,255)">null</span> )
    order <span style="color: rgb(255,0,0)">=</span> <span style="color: rgb(0,0,255)">new</span> Order();</pre>
<p>It is also really useful for output. For example, instead of using the conditional operator,</p>
<pre class="code"><span style="color: rgb(43,145,175)">Console</span><span style="color: rgb(255,0,0)">.</span>WriteLine( filename <span style="color: rgb(255,0,0)">!=</span> <span style="color: rgb(0,0,255)">null</span> <span style="color: rgb(255,0,0)">?</span> filename : <span style="color: rgb(163,21,21)">"Filename is undefined"</span> );</pre>
<p>You can use the null coalescing operator,</p>
<pre class="code"><span style="color: rgb(43,145,175)">Console</span><span style="color: rgb(255,0,0)">.</span>WriteLine( filename <span style="color: rgb(255,0,0)">??</span> <span style="color: rgb(163,21,21)">"Filename is undefined"</span> );</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.alteridem.net/2007/08/17/null-coalescing-operator/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

