<?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>Brian Grinstead &#187; tips</title>
	<atom:link href="http://www.briangrinstead.com/blog/tag/tips/feed" rel="self" type="application/rss+xml" />
	<link>http://www.briangrinstead.com/blog</link>
	<description></description>
	<lastBuildDate>Tue, 10 Aug 2010 18:26:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>C# Tips &#8211; Type Conversion with &#8220;as&#8221; and &#8220;is&#8221;</title>
		<link>http://www.briangrinstead.com/blog/csharp-tips-type-conversion-with-as-and-is</link>
		<comments>http://www.briangrinstead.com/blog/csharp-tips-type-conversion-with-as-and-is#comments</comments>
		<pubDate>Tue, 21 Jul 2009 19:00:39 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[snippet]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://www.briangrinstead.com/blog/?p=305</guid>
		<description><![CDATA[I had used C# for at least a year before I found out a couple of nice shorthand ways to convert types. These are the is and as expressions, and using them can help make your code more readable. The is Expression The is expression will return true if the provided expression is non-null and [...]]]></description>
			<content:encoded><![CDATA[<p>I had used C# for at least a year before I found out a couple of nice shorthand ways to convert types.  These are the <a href="http://msdn.microsoft.com/en-us/library/scekt9xw%28VS.80%29.aspx">is</a> and <a href="http://msdn.microsoft.com/en-us/library/cscsdfbt%28VS.71%29.aspx">as</a> expressions, and using them can help make your code more readable.</p>

<h3>The is Expression</h3>

<p>The <em>is</em> expression will return true if the provided expression is non-null and can be cast to the specific type.</p>


<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;color: #0000FF;">private</span> <span style="color: #FF0000;color: #0000FF;">string</span> GetDisplayField<span style="color: #000000;color: #000000;">&#40;</span><span style="color: #FF0000;color: #0000FF;">object</span> val<span style="color: #000000;color: #000000;">&#41;</span>
<span style="color: #000000;color: #000000;">&#123;</span>
	<span style="color: #0600FF;color: #0000FF;">if</span> <span style="color: #000000;color: #000000;">&#40;</span>val <span style="color: #008000;color: #0000FF;">is</span> <span style="color: #FF0000;color: #0000FF;">string</span><span style="color: #000000;color: #000000;">&#41;</span>
	<span style="color: #000000;color: #000000;">&#123;</span>
		<span style="color: #FF0000;color: #0000FF;">string</span> text <span style="color: #008000;color: #000000;">=</span> <span style="color: #000000;color: #000000;">&#40;</span><span style="color: #FF0000;color: #0000FF;">string</span><span style="color: #000000;color: #000000;">&#41;</span>val<span style="color: #008000;color: #000000;">;</span>
		<span style="color: #0600FF;color: #0000FF;">return</span> <span style="color: #666666;color: #808080;">&quot;Value was text: &quot;</span> <span style="color: #008000;color: #000000;">+</span> text<span style="color: #008000;color: #000000;">;</span>
	<span style="color: #000000;color: #000000;">&#125;</span>
	<span style="color: #0600FF;color: #0000FF;">else</span> <span style="color: #0600FF;color: #0000FF;">if</span> <span style="color: #000000;color: #000000;">&#40;</span>val <span style="color: #008000;color: #0000FF;">is</span> DateTime<span style="color: #000000;color: #000000;">&#41;</span>
	<span style="color: #000000;color: #000000;">&#123;</span>
		DateTime time <span style="color: #008000;color: #000000;">=</span> <span style="color: #000000;color: #000000;">&#40;</span>DateTime<span style="color: #000000;color: #000000;">&#41;</span>val<span style="color: #008000;color: #000000;">;</span>
		<span style="color: #0600FF;color: #0000FF;">return</span> <span style="color: #666666;color: #808080;">&quot;Value was date: &quot;</span> <span style="color: #008000;color: #000000;">+</span> time.<span style="color: #0000FF;">ToShortDateString</span><span style="color: #000000;color: #000000;">&#40;</span><span style="color: #000000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
	<span style="color: #000000;color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;color: #0000FF;">return</span> <span style="color: #666666;color: #808080;">&quot;Could not match type&quot;</span><span style="color: #008000;color: #000000;">;</span>
<span style="color: #000000;color: #000000;">&#125;</span></pre></div></div>


<h3>The as Expression</h3>

<p>The <em>as</em> expression will try to cast the object into the given type, and returns an object of that type if the cast was successful, or return null if unsuccessful.</p>

<p>This buys a little bit more functionality for you, as it assigns the variable with an already casted value if successful.  It is functionally equivalent to: <code>expression is type ? (type)expression : (type)null</code></p>


<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;color: #0000FF;">private</span> <span style="color: #FF0000;color: #0000FF;">string</span> GetDisplayField<span style="color: #000000;color: #000000;">&#40;</span><span style="color: #FF0000;color: #0000FF;">object</span> val<span style="color: #000000;color: #000000;">&#41;</span>
<span style="color: #000000;color: #000000;">&#123;</span>
	<span style="color: #FF0000;color: #0000FF;">string</span> text <span style="color: #008000;color: #000000;">=</span> val <span style="color: #0600FF;color: #0000FF;">as</span> string<span style="color: #008000;color: #000000;">;</span>
	DateTime<span style="color: #008000;color: #000000;">?</span> time <span style="color: #008000;color: #000000;">=</span> val <span style="color: #0600FF;color: #0000FF;">as</span> DateTime<span style="color: #008000;color: #000000;">?;</span>
&nbsp;
	<span style="color: #0600FF;color: #0000FF;">if</span> <span style="color: #000000;color: #000000;">&#40;</span>text <span style="color: #008000;color: #000000;">!=</span> <span style="color: #0600FF;color: #0000FF;">null</span><span style="color: #000000;color: #000000;">&#41;</span>
	<span style="color: #000000;color: #000000;">&#123;</span>
		<span style="color: #0600FF;color: #0000FF;">return</span> <span style="color: #666666;color: #808080;">&quot;Value was text: &quot;</span> <span style="color: #008000;color: #000000;">+</span> text<span style="color: #008000;color: #000000;">;</span>
	<span style="color: #000000;color: #000000;">&#125;</span>
	<span style="color: #0600FF;color: #0000FF;">if</span> <span style="color: #000000;color: #000000;">&#40;</span>time.<span style="color: #0000FF;">HasValue</span><span style="color: #000000;color: #000000;">&#41;</span>
	<span style="color: #000000;color: #000000;">&#123;</span>
		<span style="color: #0600FF;color: #0000FF;">return</span> <span style="color: #666666;color: #808080;">&quot;Value was date: &quot;</span> <span style="color: #008000;color: #000000;">+</span> time.<span style="color: #0000FF;">Value</span>.<span style="color: #0000FF;">ToShortDateString</span><span style="color: #000000;color: #000000;">&#40;</span><span style="color: #000000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
	<span style="color: #000000;color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;color: #0000FF;">return</span> <span style="color: #666666;color: #808080;">&quot;Could not match type&quot;</span><span style="color: #008000;color: #000000;">;</span>
<span style="color: #000000;color: #000000;">&#125;</span></pre></div></div>


<h3>Testing out the functions</h3>

<p><br /></p>


<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;color: #000000;">&#40;</span>GetDisplayField<span style="color: #000000;color: #000000;">&#40;</span><span style="color: #FF0000;color: #808080;">1</span><span style="color: #000000;color: #000000;">&#41;</span><span style="color: #000000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>              <span style="color: #008080; font-style: italic;color: #008000;">// Output: &quot;Could not match type&quot;</span>
Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;color: #000000;">&#40;</span>GetDisplayField<span style="color: #000000;color: #000000;">&#40;</span><span style="color: #666666;color: #808080;">&quot;Hello&quot;</span><span style="color: #000000;color: #000000;">&#41;</span><span style="color: #000000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>        <span style="color: #008080; font-style: italic;color: #008000;">// Output: &quot;Value was text: Hello&quot;</span>
Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;color: #000000;">&#40;</span>GetDisplayField<span style="color: #000000;color: #000000;">&#40;</span>DateTime.<span style="color: #0000FF;">Now</span><span style="color: #000000;color: #000000;">&#41;</span><span style="color: #000000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>   <span style="color: #008080; font-style: italic;color: #008000;">// Output: &quot;Value was date: 7/21/2009&quot;</span></pre></div></div>


<p>They are both readable ways to perform a type conversion &#8211; but pick one or the other!  Using both of them is redundant.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.briangrinstead.com/blog/csharp-tips-type-conversion-with-as-and-is/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C# Tips &#8211; Null Coalescing ??</title>
		<link>http://www.briangrinstead.com/blog/null-coalescing</link>
		<comments>http://www.briangrinstead.com/blog/null-coalescing#comments</comments>
		<pubDate>Tue, 05 May 2009 17:29:30 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://www.briangrinstead.com/?p=85</guid>
		<description><![CDATA[I have always enjoyed using the logical OR operator in JavaScript (&#124;&#124;) as an oppurtunity to check for a null-like value, specifically to provide default parameters for functions or to check for existence of a property on a collection. I think they are a great way to make code more concise, and if used well, [...]]]></description>
			<content:encoded><![CDATA[<p>I have always enjoyed using the logical OR operator in JavaScript (||) as an oppurtunity to check for a null-like value, specifically to provide default parameters for functions or to check for existence of a property on a collection.  I think they are a great way to make code more concise, and if used well, more readable.</p>

<p>These two functions do the same thing, but the first is shorter and more readable to people who may have to go back and modify it later.</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;color: #0000FF;">function</span> contentDocument<span style="color: #009900;color: #000000;">&#40;</span>frame<span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
    <span style="color: #006600; font-style: italic;color: #008000;">// If the frame.contentDocument either doesn't exist or is a null </span>
    <span style="color: #006600; font-style: italic;color: #008000;">// value, it will skip to the next value down the line </span>
    <span style="color: #000066; font-weight: bold;color: #0000FF;">return</span> frame.<span style="color: #660066;">contentDocument</span> <span style="color: #339933;color: #000000;">||</span> 
        frame.<span style="color: #660066;">contentWindow</span>.<span style="color: #660066;">document</span> <span style="color: #339933;color: #000000;">||</span> 
        frame.<span style="color: #660066;">document</span><span style="color: #339933;color: #000000;">;</span>
<span style="color: #009900;color: #000000;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;color: #0000FF;">function</span> contentDocumentVerbose<span style="color: #009900;color: #000000;">&#40;</span>frame<span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
    <span style="color: #006600; font-style: italic;color: #008000;">// This works, but is possibly too verbose for such a simple task</span>
    <span style="color: #000066; font-weight: bold;color: #0000FF;">if</span> <span style="color: #009900;color: #000000;">&#40;</span>frame.<span style="color: #660066;">contentDocument</span><span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
        <span style="color: #000066; font-weight: bold;color: #0000FF;">return</span> frame.<span style="color: #660066;">contentDocument</span><span style="color: #339933;color: #000000;">;</span>
    <span style="color: #009900;color: #000000;">&#125;</span>
    <span style="color: #000066; font-weight: bold;color: #0000FF;">else</span> <span style="color: #000066; font-weight: bold;color: #0000FF;">if</span> <span style="color: #009900;color: #000000;">&#40;</span>frame.<span style="color: #660066;">contentWindow</span><span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
        <span style="color: #000066; font-weight: bold;color: #0000FF;">return</span> frame.<span style="color: #660066;">contentWindow</span>.<span style="color: #660066;">document</span><span style="color: #339933;color: #000000;">;</span>
    <span style="color: #009900;color: #000000;">&#125;</span>
    <span style="color: #000066; font-weight: bold;color: #0000FF;">else</span> <span style="color: #000066; font-weight: bold;color: #0000FF;">if</span> <span style="color: #009900;color: #000000;">&#40;</span>frame.<span style="color: #660066;">document</span><span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
        <span style="color: #000066; font-weight: bold;color: #0000FF;">return</span> frame.<span style="color: #660066;">document</span><span style="color: #339933;color: #000000;">;</span>
    <span style="color: #009900;color: #000000;">&#125;</span>
<span style="color: #009900;color: #000000;">&#125;</span></pre></div></div>


<p>I was happy to see that C# has a similar feature, the <a href="http://en.wikipedia.org/wiki/%3F%3F_Operator">Null Coalescing Operator</a>.</p>

<p>This is nice for the same reasons, and is more readable than the normal ternary operator when simply checking null.  Of course, if I am not checking for a null value, but a more complex boolean expression, I will usually use the ternary operator or an if-else block it is more than just a variable assignment or return statement.  Anytime I use any extra syntactical feature, it is my hope to make the code more readable.</p>

<p>Anyway, here is an example of the &#8216;??&#8217; (coalescing) operator contrasted with the &#8216;?:&#8217; (ternary) operator.</p>


<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;color: #0000FF;">string</span> APP_DEFAULT <span style="color: #008000;color: #000000;">=</span> <span style="color: #666666;color: #808080;">&quot;application/octet-stream&quot;</span><span style="color: #008000;color: #000000;">;</span>
&nbsp;
<span style="color: #0600FF;color: #0000FF;">private</span> <span style="color: #FF0000;color: #0000FF;">string</span> GetContentType<span style="color: #000000;color: #000000;">&#40;</span><span style="color: #FF0000;color: #0000FF;">string</span> contentType<span style="color: #000000;color: #000000;">&#41;</span>
<span style="color: #000000;color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;color: #008000;">// Nice and readable.  The '??' operator can be very useful.</span>
    <span style="color: #FF0000;color: #0000FF;">string</span> FUNCTION_DEFAULT <span style="color: #008000;color: #000000;">=</span> null<span style="color: #008000;color: #000000;">;</span>
&nbsp;
    <span style="color: #0600FF;color: #0000FF;">return</span> contentType <span style="color: #008000;color: #000000;">??</span> FUNCTION_DEFAULT <span style="color: #008000;color: #000000;">??</span> APP_DEFAULT<span style="color: #008000;color: #000000;">;</span>
<span style="color: #000000;color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;color: #0000FF;">private</span> <span style="color: #FF0000;color: #0000FF;">string</span> GetContentTypeTernary<span style="color: #000000;color: #000000;">&#40;</span><span style="color: #FF0000;color: #0000FF;">string</span> contentType<span style="color: #000000;color: #000000;">&#41;</span> 
<span style="color: #000000;color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;color: #008000;">// This is NOT readable.  Please do not use nested ternary</span>
    <span style="color: #008080; font-style: italic;color: #008000;">// operators... They take too much energy to figure out.</span>
    <span style="color: #FF0000;color: #0000FF;">string</span> FUNCTION_DEFAULT <span style="color: #008000;color: #000000;">=</span> <span style="color: #666666;color: #808080;">&quot;text/plain&quot;</span><span style="color: #008000;color: #000000;">;</span>
&nbsp;
    <span style="color: #0600FF;color: #0000FF;">return</span> <span style="color: #000000;color: #000000;">&#40;</span>contentType <span style="color: #008000;color: #000000;">!=</span> <span style="color: #0600FF;color: #0000FF;">null</span><span style="color: #000000;color: #000000;">&#41;</span> <span style="color: #008000;color: #000000;">?</span> contentType <span style="color: #008000;color: #000000;">:</span>
        <span style="color: #000000;color: #000000;">&#40;</span><span style="color: #000000;color: #000000;">&#40;</span>FUNCTION_DEFAULT <span style="color: #008000;color: #000000;">!=</span> <span style="color: #0600FF;color: #0000FF;">null</span><span style="color: #000000;color: #000000;">&#41;</span> <span style="color: #008000;color: #000000;">?</span> FUNCTION_DEFAULT <span style="color: #008000;color: #000000;">:</span> APP_DEFAULT<span style="color: #000000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
<span style="color: #000000;color: #000000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.briangrinstead.com/blog/null-coalescing/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
