<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Fun with C# generics: down-casting to a generic type</title>
	<atom:link href="http://igoro.com/archive/fun-with-c-generics-down-casting-to-a-generic-type/feed/" rel="self" type="application/rss+xml" />
	<link>http://igoro.com/archive/fun-with-c-generics-down-casting-to-a-generic-type/</link>
	<description>On programming, technology, and random things of interest</description>
	<lastBuildDate>Thu, 02 Feb 2012 01:36:06 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
	<item>
		<title>By: vishaka opatha</title>
		<link>http://igoro.com/archive/fun-with-c-generics-down-casting-to-a-generic-type/comment-page-1/#comment-18361</link>
		<dc:creator>vishaka opatha</dc:creator>
		<pubDate>Wed, 14 Sep 2011 17:29:33 +0000</pubDate>
		<guid isPermaLink="false">http://igoro.com/?p=22#comment-18361</guid>
		<description>hi,
could you please explain me the simple way of casting a generic object to another different object.
Thanks in advance.</description>
		<content:encoded><![CDATA[<p>hi,<br />
could you please explain me the simple way of casting a generic object to another different object.<br />
Thanks in advance.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Igor Ostrovsky</title>
		<link>http://igoro.com/archive/fun-with-c-generics-down-casting-to-a-generic-type/comment-page-1/#comment-15096</link>
		<dc:creator>Igor Ostrovsky</dc:creator>
		<pubDate>Thu, 01 Sep 2011 16:20:42 +0000</pubDate>
		<guid isPermaLink="false">http://igoro.com/?p=22#comment-15096</guid>
		<description>Andrey: Great point! Yes, the solution via dynamic typing is a lot like reflection, but a lot cleaner. Also, dynamic typing should have better performance in typical cases than reflection.

Back when I wrote the article, dynamic typing wasn&#039;t yet available in C#.</description>
		<content:encoded><![CDATA[<p>Andrey: Great point! Yes, the solution via dynamic typing is a lot like reflection, but a lot cleaner. Also, dynamic typing should have better performance in typical cases than reflection.</p>
<p>Back when I wrote the article, dynamic typing wasn&#8217;t yet available in C#.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrey Tamelo</title>
		<link>http://igoro.com/archive/fun-with-c-generics-down-casting-to-a-generic-type/comment-page-1/#comment-15009</link>
		<dc:creator>Andrey Tamelo</dc:creator>
		<pubDate>Thu, 01 Sep 2011 09:53:58 +0000</pubDate>
		<guid isPermaLink="false">http://igoro.com/?p=22#comment-15009</guid>
		<description>A fix to my previous post (the content between the angle brackets &lt; &gt; has been skipped):

public static Pair FirstTwoValues(Node node)
{
  if( node == null ) throw new ArgumentException();
  if( node.Next == null ) throw new ArgumentException();

  dynamic first = node;
  dynamic next = node.Next;

  return MakePairImpl(first, next);
}

private static Pair MakePairImpl&lt;TFirst, TNext&gt;(Node&lt;TFirst&gt; first, Node&lt;TNext&gt; next)
{
  return new Pair&lt;Node&lt;TFirst&gt;, Node&lt;TNext&gt;&gt;(first, next);
}</description>
		<content:encoded><![CDATA[<p>A fix to my previous post (the content between the angle brackets &lt; &gt; has been skipped):</p>
<p>public static Pair FirstTwoValues(Node node)<br />
{<br />
  if( node == null ) throw new ArgumentException();<br />
  if( node.Next == null ) throw new ArgumentException();</p>
<p>  dynamic first = node;<br />
  dynamic next = node.Next;</p>
<p>  return MakePairImpl(first, next);<br />
}</p>
<p>private static Pair MakePairImpl&lt;TFirst, TNext&gt;(Node&lt;TFirst&gt; first, Node&lt;TNext&gt; next)<br />
{<br />
  return new Pair&lt;Node&lt;TFirst&gt;, Node&lt;TNext&gt;&gt;(first, next);<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrey Tamelo</title>
		<link>http://igoro.com/archive/fun-with-c-generics-down-casting-to-a-generic-type/comment-page-1/#comment-15008</link>
		<dc:creator>Andrey Tamelo</dc:creator>
		<pubDate>Thu, 01 Sep 2011 09:49:04 +0000</pubDate>
		<guid isPermaLink="false">http://igoro.com/?p=22#comment-15008</guid>
		<description>That&#039;s an interesting approach! Thanks for sharing, Igor.

But there is a fourth option ;)

Recently I was solving just about the same problem. Here is what I came up with:

public static Pair FirstTwoValues(Node node)
{
  if( node == null ) throw new ArgumentException();
  if( node.Next == null ) throw new ArgumentException();

  dynamic first = node;
  dynamic next = node.Next;

  return MakePair(first, next);
}

private static Pair MakePair(Node first, Node next)
{
  return new Pair&lt;Node, Node&gt;(first, next);
}

That trick relies on DLR being able to figure out (under the hood) the exact runtime types of the nodes.

This is somewhat similar to the reflection-based approach and should have the same performance penalties.. But looks much nicer from the C# code perspective and devoid of the downsides of the 3rd option you outlined.

Hope that will also be useful to the readers of your blog.

Andrey</description>
		<content:encoded><![CDATA[<p>That&#8217;s an interesting approach! Thanks for sharing, Igor.</p>
<p>But there is a fourth option <img src='http://igoro.com/wordpress/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Recently I was solving just about the same problem. Here is what I came up with:</p>
<p>public static Pair FirstTwoValues(Node node)<br />
{<br />
  if( node == null ) throw new ArgumentException();<br />
  if( node.Next == null ) throw new ArgumentException();</p>
<p>  dynamic first = node;<br />
  dynamic next = node.Next;</p>
<p>  return MakePair(first, next);<br />
}</p>
<p>private static Pair MakePair(Node first, Node next)<br />
{<br />
  return new Pair&lt;Node, Node&gt;(first, next);<br />
}</p>
<p>That trick relies on DLR being able to figure out (under the hood) the exact runtime types of the nodes.</p>
<p>This is somewhat similar to the reflection-based approach and should have the same performance penalties.. But looks much nicer from the C# code perspective and devoid of the downsides of the 3rd option you outlined.</p>
<p>Hope that will also be useful to the readers of your blog.</p>
<p>Andrey</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Olawn</title>
		<link>http://igoro.com/archive/fun-with-c-generics-down-casting-to-a-generic-type/comment-page-1/#comment-8549</link>
		<dc:creator>Olawn</dc:creator>
		<pubDate>Sun, 03 Jul 2011 17:27:38 +0000</pubDate>
		<guid isPermaLink="false">http://igoro.com/?p=22#comment-8549</guid>
		<description>hello admin!
Your article is very useful for who is learning c#.
have a Good time..</description>
		<content:encoded><![CDATA[<p>hello admin!<br />
Your article is very useful for who is learning c#.<br />
have a Good time..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: enrique</title>
		<link>http://igoro.com/archive/fun-with-c-generics-down-casting-to-a-generic-type/comment-page-1/#comment-8518</link>
		<dc:creator>enrique</dc:creator>
		<pubDate>Sun, 03 Jul 2011 05:52:57 +0000</pubDate>
		<guid isPermaLink="false">http://igoro.com/?p=22#comment-8518</guid>
		<description>thank you for your great article.........
i got what  search ....
thank you very much....but keep on updating</description>
		<content:encoded><![CDATA[<p>thank you for your great article&#8230;&#8230;&#8230;<br />
i got what  search &#8230;.<br />
thank you very much&#8230;.but keep on updating</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Igor Ostrovsky</title>
		<link>http://igoro.com/archive/fun-with-c-generics-down-casting-to-a-generic-type/comment-page-1/#comment-2515</link>
		<dc:creator>Igor Ostrovsky</dc:creator>
		<pubDate>Tue, 15 Mar 2011 21:37:02 +0000</pubDate>
		<guid isPermaLink="false">http://igoro.com/?p=22#comment-2515</guid>
		<description>Dave: Casting to object will not resolve the problem. The issue is that the U and V type variables do not exist in the current context. You only have the node objects, whose static type is the non-generic Node base class.

To get into a context where U and V type variables are available, you need to use one of the techniques described in the article (i.e., big switch statement, reflection, or virtual method call).</description>
		<content:encoded><![CDATA[<p>Dave: Casting to object will not resolve the problem. The issue is that the U and V type variables do not exist in the current context. You only have the node objects, whose static type is the non-generic Node base class.</p>
<p>To get into a context where U and V type variables are available, you need to use one of the techniques described in the article (i.e., big switch statement, reflection, or virtual method call).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave</title>
		<link>http://igoro.com/archive/fun-with-c-generics-down-casting-to-a-generic-type/comment-page-1/#comment-2514</link>
		<dc:creator>Dave</dc:creator>
		<pubDate>Tue, 15 Mar 2011 21:31:24 +0000</pubDate>
		<guid isPermaLink="false">http://igoro.com/?p=22#comment-2514</guid>
		<description>Why not do an intermediate cast to object?
&lt;code&gt;
return new Pair&lt;U,V&gt;((Node&lt;U&gt;)(object)node, (Node&lt;V&gt;)(object)node.Next);
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Why not do an intermediate cast to object?<br />
<code><br />
return new Pair&lt;U,V&gt;((Node&lt;U&gt;)(object)node, (Node&lt;V&gt;)(object)node.Next);<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ihar Bury</title>
		<link>http://igoro.com/archive/fun-with-c-generics-down-casting-to-a-generic-type/comment-page-1/#comment-2121</link>
		<dc:creator>Ihar Bury</dc:creator>
		<pubDate>Wed, 12 Jan 2011 16:38:34 +0000</pubDate>
		<guid isPermaLink="false">http://igoro.com/?p=22#comment-2121</guid>
		<description>Actually you probably don&#039;t need FirstTwoValues method to return Pair. Non-generic Pair (or even Pair) should be enough in most cases.</description>
		<content:encoded><![CDATA[<p>Actually you probably don&#8217;t need FirstTwoValues method to return Pair. Non-generic Pair (or even Pair) should be enough in most cases.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrew Borodin</title>
		<link>http://igoro.com/archive/fun-with-c-generics-down-casting-to-a-generic-type/comment-page-1/#comment-978</link>
		<dc:creator>Andrew Borodin</dc:creator>
		<pubDate>Sun, 30 May 2010 12:09:14 +0000</pubDate>
		<guid isPermaLink="false">http://igoro.com/?p=22#comment-978</guid>
		<description>Usefulll casting, btw. Thx (:</description>
		<content:encoded><![CDATA[<p>Usefulll casting, btw. Thx (:</p>
]]></content:encoded>
	</item>
</channel>
</rss>

