<?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>Ken Kopczyk &#187; C#</title>
	<atom:link href="http://kenkopczyk.com/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://kenkopczyk.com</link>
	<description>Hurdles in .NET WinForms Development</description>
	<lastBuildDate>Sat, 13 Mar 2010 03:50:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='kenkopczyk.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/60348bde5d188332d5268692846b25c7?s=96&#038;d=http://s2.wp.com/i/buttonw-com.png</url>
		<title>Ken Kopczyk &#187; C#</title>
		<link>http://kenkopczyk.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://kenkopczyk.com/osd.xml" title="Ken Kopczyk" />
	<atom:link rel='hub' href='http://kenkopczyk.com/?pushpress=hub'/>
		<item>
		<title>Fun With the &#8220;ref&#8221; Keyword</title>
		<link>http://kenkopczyk.com/2009/02/03/fun-with-the-ref-keyword/</link>
		<comments>http://kenkopczyk.com/2009/02/03/fun-with-the-ref-keyword/#comments</comments>
		<pubDate>Wed, 04 Feb 2009 02:28:05 +0000</pubDate>
		<dc:creator>Ken</dc:creator>
				<category><![CDATA[Programming Languages]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://kkopczyk.wordpress.com/?p=147</guid>
		<description><![CDATA[By default, parameters are passed by value in C#.  Passing by reference requires the &#8220;ref&#8221; or &#8220;out&#8221; keywords to precede the parameter in both the method declaration and the call to the method.  Using &#8220;ref&#8221; or &#8220;out&#8221; is fairly straight forward&#8230;..or so I thought. I recently came across two cases where parameters are valid if [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kenkopczyk.com&amp;blog=6344943&amp;post=147&amp;subd=kkopczyk&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>By default, parameters are passed by value in C#.  Passing by reference requires the &#8220;ref&#8221; or &#8220;out&#8221; keywords to precede the parameter in both the method declaration and the call to the method.  Using &#8220;ref&#8221; or &#8220;out&#8221; is fairly straight forward&#8230;..or so I thought.</p>
<p>I recently came across two cases where parameters are valid if passing by value, but result in compilation errors after adding &#8220;ref&#8221;.  Consider the following member and method:</p>
<pre class="brush: csharp;">
// SuperDataTable inherits from DataTable
// Indexing a SuperDataTable yields a SuperDataRow
// which inherits from DataRow.
private SuperDataTable mySuperDataTable = new SuperDataTable();

public void DoSomethingSuper(ref DataRow myDataRow)
{
	return;
}
</pre>
<p>Now examine the following three scenarios:</p>
<pre class="brush: csharp;">
// #1 You receive a compiler error &quot;A property or indexer
// may not be passed as an out or ref parameter&quot;
DoSomethingSuper(ref mySuperDataTable[0]);

// #2 You receive a compiler error &quot;Cannot convert from
// 'ref SuperDataRow' to 'ref DataRow'
SuperDataRow mySuperDataRow = mySuperDataTable[0];
DoSomethingSuper(ref mySuperDataRow);

// #3 Only does it work in the following manner, when you
// explicitly &quot;upcast&quot; the row to the exact type in the
// signature of the method
DataRow myDataRow = mySuperDataTable[0];
DoSomethingSuper(ref myDataRow);
</pre>
<p>Try it again, this time passing by value.  No compiler errors!  I probably am just lacking an intimate understanding of &#8220;ref&#8221;, but this came out of left field for me and I haven&#8217;t seen it documented online.  <b>Can anybody fill me in as to why scenarios #1 and #2 don&#8217;t work?</b></p>
<div><a title="Bookmark and Share" href="http://www.addthis.com/bookmark.php?pub=kkopczyk&amp;url=http://kkopczyk.wordpress.com/2009/02/03/fun-with-the-ref-keyword/" target="_blank"><img style="border:0;padding:0;" src="http://s7.addthis.com/static/btn/lg-share-en.gif" alt="Bookmark and Share" width="125" height="16" /></a></div>
<p><!-- AddThis Button END --></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kkopczyk.wordpress.com/147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kkopczyk.wordpress.com/147/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kkopczyk.wordpress.com/147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kkopczyk.wordpress.com/147/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kkopczyk.wordpress.com/147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kkopczyk.wordpress.com/147/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kkopczyk.wordpress.com/147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kkopczyk.wordpress.com/147/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kkopczyk.wordpress.com/147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kkopczyk.wordpress.com/147/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kkopczyk.wordpress.com/147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kkopczyk.wordpress.com/147/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kkopczyk.wordpress.com/147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kkopczyk.wordpress.com/147/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kenkopczyk.com&amp;blog=6344943&amp;post=147&amp;subd=kkopczyk&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kenkopczyk.com/2009/02/03/fun-with-the-ref-keyword/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/df43ecaeb5e162064d67f882431983c6?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">Ken</media:title>
		</media:content>

		<media:content url="http://s7.addthis.com/static/btn/lg-share-en.gif" medium="image">
			<media:title type="html">Bookmark and Share</media:title>
		</media:content>
	</item>
	</channel>
</rss>