<?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>Ruchit Surati&#039;s blog &#187; generics</title>
	<atom:link href="http://www.ruchitsurati.net/index.php/tag/generics/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ruchitsurati.net</link>
	<description>thoughts and ideas of a .net developer</description>
	<lastBuildDate>Wed, 23 Feb 2011 19:48:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>Sealed classes as Generic type-constraints</title>
		<link>http://www.ruchitsurati.net/index.php/2010/07/07/sealed-classes-as-generic-constraints/</link>
		<comments>http://www.ruchitsurati.net/index.php/2010/07/07/sealed-classes-as-generic-constraints/#comments</comments>
		<pubDate>Wed, 07 Jul 2010 14:23:08 +0000</pubDate>
		<dc:creator>Ruchit</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[generics]]></category>

		<guid isPermaLink="false">http://www.ruchitsurati.net/?p=348</guid>
		<description><![CDATA[We make heavy use of generics in our projects. Generics are really great deal when it comes to run-time polymorphism. Recently, one of my colleague came up with an idea to use a sealed class as generic type constraint. He wrote a sample code that looks like this. public sealed class MySealedClass { public MySealedClass() [...]]]></description>
			<content:encoded><![CDATA[<p>We make heavy use of generics in our projects. Generics are really great deal when it comes to run-time polymorphism. Recently, one of my colleague came up with an idea to use a sealed class as generic type constraint. He wrote a sample code that looks like this.</p>
<pre class="brush: csharp; highlight: [1,7]; title: ;">
public sealed class MySealedClass
{
    public MySealedClass() { }
}

public class MyGeneric&lt;T&gt;
        where T : MySealedClass
{
    public MyGeneric() { }
}
</pre>
<p style="text-align: justify;">Surprisingly, Above code gives following compile error.</p>
<p class="message notice" style="text-align: justify;">&#8216;MySealedClass&#8217; is not a valid constraint. A type used as a constraint must be an interface, a non-sealed class or a type parameter.</p>
<p style="text-align: justify;">That was strange. We never observed such behavior before. Code was absolutely correct and it seemed like language designers had intentionally put this constraint to not allow sealed classed as generic constraints. But what must be the reason behind this ? Finally we had our thoughts in place and found the fact why it is so.</p>
<p style="text-align: justify;">To understand, why sealed classes are not allowed to be generic type constraints, let us assume that sealed classes are allowed to be type constraints to generics. With this assumption the code above is valid and compiles successfully. Now lets write some client-code that would use this generic.</p>
<pre class="brush: csharp; title: ;">
//create and instantiate an object of type MyGeneric
MyGeneric&lt;MySealedClass&gt; objGeneric = new MyGeneric&lt;MySealedClass&gt;();
</pre>
<p style="text-align: justify;">Fair enough, this looks ok.</p>
<p style="text-align: justify;">Now lets check what other types we can pass to MyGeneric type-argument at runtime. By defination, it has to be MySealedClass itself or any type that inherits from MySealedClass.</p>
<p style="text-align: justify;">But won&#8217;t MySealedClass be the only valid type-argument for MyGeneric since MySealedClass cannot be inherited further and there will be no class that inherits from MySealedClass ? Doesn&#8217;t this defeat the whole idea of putting MySealedClass in type constraint to MyGeneric, since there&#8217;ll be no other valid-type argument except MySealedClass itself ? If MySealedClass is going to be the only valid type for MyGeneric, then  there&#8217;s no point in  defining MyGeneric as generic at all. Can&#8217;t we simply code against the type MySealedClass ?</p>
<p style="text-align: justify;">We could finally realize the reason why language designers of  C# didn&#8217;t allow sealed classes as generic type-argument. quite enlightening.</p>
<p style="text-align: justify;">While doing this we also came to realize why static classes are also not allowed to be generic type-constraints, though it&#8217;s very much obvious that static classes can never ever be generic type constraints. We found our answer in the compiled IL, where the <strong>static classes are marked as both abstract and sealed. Abstract since they cannot be instantiated and sealed since they cannot be inherited, hence cannot be generic type-constraints. </strong>In general only those types can be generic type-constraints which can have inheritance hierarchy below them, this includes interfaces, any non-sealed abstract or concrete classes.</p>
<p>
<script type="text/javascript"><!--
google_ad_client = "pub-3020293614675538";
/* 300x250, created 14-July-2010 */
google_ad_slot = "4146580937";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ruchitsurati.net/index.php/2010/07/07/sealed-classes-as-generic-constraints/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

