<?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; passing anonymous types functions</title>
	<atom:link href="http://www.richardbushnell.net/tag/passing-anonymous-types-functions/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>Can You Pass an Anonymous Type Across Functions?</title>
		<link>http://www.richardbushnell.net/2008/01/08/can-you-pass-an-anonymous-type-across-functions/</link>
		<comments>http://www.richardbushnell.net/2008/01/08/can-you-pass-an-anonymous-type-across-functions/#comments</comments>
		<pubDate>Tue, 08 Jan 2008 09:22:18 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Anonymous Types]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[passing anonymous types functions]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/01/08/can-you-pass-an-anonymous-type-across-functions/</guid>
		<description><![CDATA[One of the biggest questions with Anonymous Types is &#8220;can I pass them around?&#8221; If not, why not? Can you do something like this, for example: var GetAnonymousValue() { return new { Name = "Richard Bushnell" }; } void Main() { var value = GetAnonymousValue(); var name = value.Name;} The answer is simple: no, you [...]]]></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%2F01%2F08%2Fcan-you-pass-an-anonymous-type-across-functions%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>One of the biggest questions with Anonymous Types is &#8220;can I pass them around?&#8221; If not, why not? Can you do something like this, for example:</p>
<pre class="code"><font color="#0000ff">var</font> GetAnonymousValue() {
  <span style="color: blue">return new </span>{ Name = <span style="color: #a31515">"Richard Bushnell" </span>};
}

<span style="color: blue">void </span>Main() {
  <span style="color: blue">var </span>value = GetAnonymousValue();
  <span style="color: blue">var </span>name = value.Name;}</pre>
<p>The answer is simple: no, you can&#8217;t pass anonymous types across functions. var is not a dynamic variable, like in JavaScript. The CLR knows nothing about &#8220;var&#8221;, as the compiler just uses it to infer types when a variable is initialized.</p>
<p>At least, that was the answer until now. Using a simple extension method and generics, <a href="http://blogs.msdn.com/alexj/archive/2007/11/22/t-castbyexample-t-object-o-t-example.aspx" target="_blank">Alex James</a> just showed a nice way to pass them around on <a href="http://blogs.msdn.com/alexj/archive/2007/11/22/t-castbyexample-t-object-o-t-example.aspx" target="_blank">his blog</a>.</p>
<p>The trick is to use an example of the anonymous type you expect.</p>
<pre class="code"><span style="color: blue">object </span>GetAnonymousValue() {
  <span style="color: blue">return new </span>{ Name = <span style="color: #a31515">"Richard Bushnell" </span>};
}

<span style="color: blue">void </span>Main() {
  <span style="color: blue">var </span>value = GetAnonymousValue().CastByExample(<span style="color: blue">new </span>{ Name = <span style="color: #a31515">"" </span>});
  <span style="color: blue">var </span>name = value.Name;
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>While this might be a little dangerous, especially if you&#8217;re not testing your code regularly, this could be the solution to a few problems I can already think of. </p>
<p>Watch out for casting exceptions at runtime though! If you make a mistake anywhere, you&#8217;ll won&#8217;t get a compile-time exception, but a nasty runtime exception instead.</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%2F01%2F08%2Fcan-you-pass-an-anonymous-type-across-functions%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/01/08/can-you-pass-an-anonymous-type-across-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

