<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Why is deriving a public class from an internal class illegal?</title><link>http://blogs.msdn.com/b/ericlippert/archive/2012/11/13/why-is-deriving-a-public-class-from-an-internal-class-illegal.aspx</link><description>In C# it is illegal to declare a class D whose base class B is in any way less accessible than D. I'm occasionally asked why that is. There are a number of reasons; today I'll start with a very specific scenario and then talk about a general philosophy</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Why is deriving a public class from an internal class illegal?</title><link>http://blogs.msdn.com/b/ericlippert/archive/2012/11/13/why-is-deriving-a-public-class-from-an-internal-class-illegal.aspx#10371735</link><pubDate>Mon, 26 Nov 2012 20:57:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10371735</guid><dc:creator>Christian Amado</dc:creator><description>&lt;p&gt;Excellent article. Thank you! :)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10371735" width="1" height="1"&gt;</description></item><item><title>re: Why is deriving a public class from an internal class illegal?</title><link>http://blogs.msdn.com/b/ericlippert/archive/2012/11/13/why-is-deriving-a-public-class-from-an-internal-class-illegal.aspx#10370264</link><pubDate>Tue, 20 Nov 2012 16:49:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10370264</guid><dc:creator>Louis.fr</dc:creator><description>&lt;p&gt;@Walt Smith&lt;/p&gt;
&lt;p&gt;&amp;quot;There is no such thing as a physical &amp;quot;animal&amp;quot; as a base class for any species, nor is there some genetic code mixture that could be used to create a generic animal.&amp;quot;&lt;/p&gt;
&lt;p&gt;That&amp;#39;s why that class is abstract.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10370264" width="1" height="1"&gt;</description></item><item><title>re: Why is deriving a public class from an internal class illegal?</title><link>http://blogs.msdn.com/b/ericlippert/archive/2012/11/13/why-is-deriving-a-public-class-from-an-internal-class-illegal.aspx#10369130</link><pubDate>Fri, 16 Nov 2012 06:32:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10369130</guid><dc:creator>Gabe</dc:creator><description>&lt;p&gt;Walt Smith: While the analogy breaks down eventually (as all do), it&amp;#39;s hard to imagine a better way to understand inheritance than relating it to different species (even if the word &amp;quot;inheritance&amp;quot; has different meanings).&lt;/p&gt;
&lt;p&gt;Even my 3-year-old understands that giraffes and zebras are both animals, yet different species. She also understands that trees have a &amp;quot;bark&amp;quot; and dogs have a &amp;quot;bark&amp;quot;, yet they&amp;#39;re two totally unrelated things that just happen to have the same name.&lt;/p&gt;
&lt;p&gt;I&amp;#39;m curious what sort of conceptual software model you use to explain things, because I have a hard time imagining something could be easier to understand than a simple animal hierarchy.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10369130" width="1" height="1"&gt;</description></item><item><title>re: Why is deriving a public class from an internal class illegal?</title><link>http://blogs.msdn.com/b/ericlippert/archive/2012/11/13/why-is-deriving-a-public-class-from-an-internal-class-illegal.aspx#10369072</link><pubDate>Thu, 15 Nov 2012 23:53:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10369072</guid><dc:creator>Daniel Brückner</dc:creator><description>&lt;p&gt;Are internal class C { public void M() { [...] } } and &amp;nbsp;internal class C { internal void M() { [...] } } always equivalent or are there some corner cases?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10369072" width="1" height="1"&gt;</description></item><item><title>re: Why is deriving a public class from an internal class illegal?</title><link>http://blogs.msdn.com/b/ericlippert/archive/2012/11/13/why-is-deriving-a-public-class-from-an-internal-class-illegal.aspx#10368484</link><pubDate>Wed, 14 Nov 2012 13:20:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10368484</guid><dc:creator>Walt Smith</dc:creator><description>&lt;p&gt;Ah, great minds..... &amp;nbsp; ;)&lt;/p&gt;
&lt;div class="yellowbox"&gt;
&lt;p&gt;And fools never differ. :-) -- Eric&lt;/p&gt;
&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10368484" width="1" height="1"&gt;</description></item><item><title>re: Why is deriving a public class from an internal class illegal?</title><link>http://blogs.msdn.com/b/ericlippert/archive/2012/11/13/why-is-deriving-a-public-class-from-an-internal-class-illegal.aspx#10368261</link><pubDate>Tue, 13 Nov 2012 21:50:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10368261</guid><dc:creator>Walt Smith</dc:creator><description>&lt;p&gt;I think that one of the most confusing parts of thinking about inheritance is that the term itself is overloaded. &amp;nbsp;There is in fact no analog between software inheritance and the real-world inheritance models that are often used to explain it. &amp;nbsp;For instance, a giraffe does not inherit from some more generic animal class. &amp;nbsp;A giraffe is a giraffe, and when a new giraffe is &amp;quot;compiled&amp;quot;, it is a run-time instance that inherits from two run-time instances of its own class, namely its mother and father, who are themselves giraffes. &amp;nbsp;In point of fact, the notion of a &amp;quot;giraffe&amp;quot; or even of an &amp;quot;animal&amp;quot; is merely a convenient abstraction invented by the human mind to extrapolate similarities between different instances of species as a classification mechanism. &amp;nbsp;This extrapolation is performed ex post facto, not at design time. &amp;nbsp;There is no such thing as a physical &amp;quot;animal&amp;quot; as a base class for any species, nor is there some genetic code mixture that could be used to create a generic animal. &amp;nbsp;So trying to think about inheritance using real-world models necessarily leads to mis-interpretation of the concept and its proper use in software development. &amp;nbsp;This is all a long winded and pedantic way of saying that I think software inheritance is easier to understand if we stick to conceptual software models to explain it, rather than trying to extend the concept into the type of run-time inheritance that exists in the real, physical world, which seems to me to be conceptually closer to dependency injection than inheritance, although that is a really imperfect analog itself.&lt;/p&gt;
&lt;div class="yellowbox"&gt;
&lt;p&gt;I agree. See &lt;a href="http://blogs.msdn.com/b/ericlippert/archive/2012/02/13/bad-metaphors.aspx"&gt;my earlier blog post on this subject&lt;/a&gt;, in which I make several of your points. -- Eric&lt;/p&gt;
&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10368261" width="1" height="1"&gt;</description></item><item><title>re: Why is deriving a public class from an internal class illegal?</title><link>http://blogs.msdn.com/b/ericlippert/archive/2012/11/13/why-is-deriving-a-public-class-from-an-internal-class-illegal.aspx#10368234</link><pubDate>Tue, 13 Nov 2012 20:50:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10368234</guid><dc:creator>Carl</dc:creator><description>&lt;p&gt;Gabe - inheritance is not the only mechanism for code re-use. You have generics / extension methods these are quite capable of enforcing DRY in a none inherited way.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10368234" width="1" height="1"&gt;</description></item><item><title>re: Why is deriving a public class from an internal class illegal?</title><link>http://blogs.msdn.com/b/ericlippert/archive/2012/11/13/why-is-deriving-a-public-class-from-an-internal-class-illegal.aspx#10368223</link><pubDate>Tue, 13 Nov 2012 20:31:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10368223</guid><dc:creator>Gabe</dc:creator><description>&lt;p&gt;It&amp;#39;s unfortunate that inheritance is the only real mechanism for code reuse that C# provides. I dare say that if there were other means available, inheritance wouldn&amp;#39;t have to be [ab]used so much.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10368223" width="1" height="1"&gt;</description></item><item><title>re: Why is deriving a public class from an internal class illegal?</title><link>http://blogs.msdn.com/b/ericlippert/archive/2012/11/13/why-is-deriving-a-public-class-from-an-internal-class-illegal.aspx#10368180</link><pubDate>Tue, 13 Nov 2012 19:16:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10368180</guid><dc:creator>David V. Corbin</dc:creator><description>&lt;p&gt;What I have always wondered was why public members are allowed on internal classes! If one was to prevent internal classes from declaring public members then the restriction on public intheritance of internal classes would not present these problems.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10368180" width="1" height="1"&gt;</description></item><item><title>re: Why is deriving a public class from an internal class illegal?</title><link>http://blogs.msdn.com/b/ericlippert/archive/2012/11/13/why-is-deriving-a-public-class-from-an-internal-class-illegal.aspx#10368175</link><pubDate>Tue, 13 Nov 2012 19:11:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10368175</guid><dc:creator>Jon Skeet</dc:creator><description>&lt;p&gt;&amp;quot;Finally, I note that as an alternative, it is legal for a public class to implement an internal interface.&amp;quot;&lt;/p&gt;
&lt;p&gt;Yes, but unfortunately only either with explicit interface implementation or with public methods (unless I&amp;#39;ve missed something). In some cases neither of these is an appealing choice. I really wish the implicit access modifier of internal interfaces was internal rather than public...&lt;/p&gt;
&lt;p&gt;One interesting point to note around all of this is that although a public interface can&amp;#39;t have a member which refers to an internal type, a public abstract class can have an internal abstract member which refers to an internal type. The result of having an internal abstract class is that while other assemblies can call the public members of the abstract class on any instances provided, no other assembly can create a concrete derived type.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10368175" width="1" height="1"&gt;</description></item></channel></rss>