<?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 Can't I Access A Protected Member From A Derived Class, Part Three</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/04/24/why-can-t-i-access-a-protected-member-from-a-derived-class-part-three.aspx</link><description>Holy goodness, I've been busy. The MVP Summit was fabulous for us; thanks to all who attended and gave us great feedback on our ideas for evolving the languages and tools. And I've been doing some longer-lead thinking and working on language futures,</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Why Can't I Access A Protected Member From A Derived Class, Part Three</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/04/24/why-can-t-i-access-a-protected-member-from-a-derived-class-part-three.aspx#10051388</link><pubDate>Wed, 18 Aug 2010 10:02:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10051388</guid><dc:creator>Roman</dc:creator><description>&lt;p&gt;&amp;gt; If the member is already marked as internal then the only people who can use it are your coworkers.&lt;/p&gt;
&lt;p&gt;It is clear that C# has been designed around this concept, and that&amp;#39;s fair enough. I do find it limiting though. Oftentimes I want to protect my own code _from myself_ misusing it by accident. I want to be able to write bullet-proof code that I _cannot_ possibly abuse - even if I don&amp;#39;t have malicious intent.&lt;/p&gt;
&lt;p&gt;Unfortunately, in C#, if the code consists of at least two classes interacting with each other then almost always the only way to make it bullet-proof to accidental abuse is to stick it into a separate assembly.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10051388" width="1" height="1"&gt;</description></item><item><title>re: Why Can't I Access A Protected Member From A Derived Class, Part Three</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/04/24/why-can-t-i-access-a-protected-member-from-a-derived-class-part-three.aspx#10051222</link><pubDate>Tue, 17 Aug 2010 23:35:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10051222</guid><dc:creator>Timwi</dc:creator><description>&lt;p&gt;&amp;gt; You get NO additional security by going to &amp;quot;protected and internal&amp;quot; because your coworker can simply write a subclass to party on the member.&lt;/p&gt;
&lt;p&gt;Isn’t this the same argument as saying, you get no additional security by going “private” because your coworker can simply write a public method to party on the member? If you think this is a strawman, please elucidate.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10051222" width="1" height="1"&gt;</description></item><item><title>re: Why Can't I Access A Protected Member From A Derived Class, Part Three</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/04/24/why-can-t-i-access-a-protected-member-from-a-derived-class-part-three.aspx#10038367</link><pubDate>Thu, 15 Jul 2010 00:39:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10038367</guid><dc:creator>LeatHakkor</dc:creator><description>&lt;p&gt;Forgot to mention that there is a way to make members virtual, I have never had a need to do it, but it does work using delegates, Function&amp;lt;T&amp;gt;, Action&amp;lt;T&amp;gt;, and poor-man&amp;#39;s dependency injection (&lt;a rel="nofollow" target="_new" href="http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/07/03/how-not-to-do-dependency-injection-in-nerddinner.aspx"&gt;www.lostechies.com/.../how-not-to-do-dependency-injection-in-nerddinner.aspx&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;If you NEED the sample email me, its substantially longer than this snippet.&lt;/p&gt;
&lt;p&gt;var email = string.Concat( @&amp;quot;leat&amp;quot; + &amp;quot;hakkor&amp;quot;, &amp;quot;@&amp;quot;, &amp;quot;hot&amp;quot;, &amp;quot;mail&amp;quot;, @&amp;quot;.&amp;quot;, &amp;quot;com&amp;quot; );&lt;/p&gt;
&lt;p&gt;^^ Take that spammers&lt;/p&gt;
&lt;p&gt;[Scott Youngblut (MCPD)]&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10038367" width="1" height="1"&gt;</description></item><item><title>re: Why Can't I Access A Protected Member From A Derived Class, Part Three</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/04/24/why-can-t-i-access-a-protected-member-from-a-derived-class-part-three.aspx#10038112</link><pubDate>Wed, 14 Jul 2010 13:56:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10038112</guid><dc:creator>LeatHakkor</dc:creator><description>&lt;p&gt;It&amp;#39;s a common belief that you cannot make some members both protected AND internal.&lt;/p&gt;
&lt;p&gt;And its true that you cannot do so in a single line, as many, including myself, would wish, but with some cleverness it is 100% do-able. I&amp;#39;ll Let my (hopefully) self-documenting code speak for itself.&lt;/p&gt;
&lt;p&gt;[START C# CODE]&lt;/p&gt;
&lt;p&gt;//Code below is 100% tested&lt;/p&gt;
&lt;p&gt;/* FROM ProtectedAndInternal.dll */&lt;/p&gt;
&lt;p&gt;namespace ProtectedAndInternal&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public class MyServiceImplementationBase&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;protected static class RelevantStrings&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;internal static string AppName = &amp;quot;Kickin&amp;#39; Code&amp;quot;;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;internal static string AppAuthor = &amp;quot;Scott Youngblut&amp;quot;;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public class MyServiceImplementation : MyServiceImplementationBase&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public void PrintProperties()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// WORKS PERFECTLY BECAUSE SAME ASSEMBLY!&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(RelevantStrings.AppAuthor);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public class NotMyServiceImplementation&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public void PrintProperties()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// FAILS - NOT THE CORRECT INHERITANCE CHAIN&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Error CS0122: &amp;#39;ProtectedAndInternal.MyServiceImplementationBase.Relevant&amp;#39; is inaccessible due to its protection level&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Console.WriteLine(MyServiceImplementationBase.RelevantStrings.AppAuthor);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;/* From AlternateAssemblyService.dll which references ProtectedAndInternal.dll */&lt;/p&gt;
&lt;p&gt;namespace AlternateAssemblyService&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public class MyServiceImplementation : MyServiceImplementationBase&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public void PrintProperties()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// FAILS - NOT THE CORRECT ASSEMBLY&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Error CS0117: &amp;#39;ProtectedAndInternal.MyServiceImplementationBase.RelevantStrings&amp;#39; does not contain a definition for &amp;#39;AppAuthor&amp;#39;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Console.WriteLine(RelevantStrings.AppAuthor);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;[END C# CODE]&lt;/p&gt;
&lt;p&gt;[CopyRight - Scott Youngblut (MCPD)]&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10038112" width="1" height="1"&gt;</description></item><item><title>re: Why Can't I Access A Protected Member From A Derived Class, Part Three</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/04/24/why-can-t-i-access-a-protected-member-from-a-derived-class-part-three.aspx#9995049</link><pubDate>Tue, 13 Apr 2010 12:23:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9995049</guid><dc:creator>Timwi</dc:creator><description>&lt;p&gt;I don't entirely agree with your analysis of people's perception. I don't think people conceive of &amp;quot;internal&amp;quot;, &amp;quot;protected&amp;quot; and &amp;quot;private&amp;quot; as restrictions because they think of &amp;quot;public&amp;quot; as some sort of &amp;quot;natural&amp;quot; state. I think they conceive of them as restrictions because the words themselves emphasise that they are a restriction; especially &amp;quot;protected&amp;quot; and &amp;quot;internal&amp;quot;. They both sound much more like &amp;quot;stop someone from doing something&amp;quot; than &amp;quot;allow someone to do something&amp;quot;.&lt;/p&gt;
&lt;p&gt;I have no idea what they could have been called instead, but this explains why the &amp;quot;wrong&amp;quot; intuition is so common.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9995049" width="1" height="1"&gt;</description></item><item><title>re: Why Can't I Access A Protected Member From A Derived Class, Part Three</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/04/24/why-can-t-i-access-a-protected-member-from-a-derived-class-part-three.aspx#8575643</link><pubDate>Thu, 05 Jun 2008 19:27:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8575643</guid><dc:creator>JD</dc:creator><description>&lt;p&gt;Customer feedback is right on this one. 'protected internal' should be FamilyAndAssembly.&lt;/p&gt;
&lt;p&gt;The main paractical effect of these attributes is to give compiler errors when someone does something wrong. So do I want to give a compiler error to my coworker who is not derived from my class? Yes.&lt;/p&gt;
&lt;p&gt;You can argue that it also affects CAS and verification, and that is a very powerful argument. But by the measure of security, the C# team got the decision wrong. Because nearly everyone adding 'protected internal' is expecting it to be restrictive, and is surprised to learn of the contrary *if* they check their assumptions. So C# has undermined (not helped) CAS by making it more likely for developers to introduce vulnerabilities in their code. &lt;/p&gt;
&lt;p&gt;I would cut some slack based on design goals though. Because C# was clearly built to work well (better!) for developers with Java and C++ backgrounds. In Java is there a &amp;quot;package protected&amp;quot; (I honestly have no clue)? In C++ there aren't really sassemblies as such, but can 'friend' access 'portected' members (since they can access privates, I assume so).&lt;/p&gt;
&lt;p&gt;To put it a different way, who does the current 'protected internal' help? It only helps non-subclassed code in the assembly have access to protected members. This is trivially possible (and arguably better modeled) by adding a regular internal method anyway. Maybe Java &amp;quot;protected package&amp;quot; did soemthing like this and it was a competitive response? I really don't understand how this made the bar to get into the language spec as is. &lt;/p&gt;
&lt;p&gt;Honestly, from reading your article it sounds like there was some champion who really believed the 'access modifiers are a loosening of the private constraint' and no one else cared enough to argue back strongly. &amp;nbsp;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8575643" width="1" height="1"&gt;</description></item><item><title>VCS Team Links for May 22, 2008</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/04/24/why-can-t-i-access-a-protected-member-from-a-derived-class-part-three.aspx#8533401</link><pubDate>Thu, 22 May 2008 22:23:28 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8533401</guid><dc:creator>Charlie Calvert's Community Blog</dc:creator><description>&lt;p&gt;Rather than place the links to the most recent C# team content directly in Community Convergence , I&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8533401" width="1" height="1"&gt;</description></item><item><title>re: Why Can't I Access A Protected Member From A Derived Class, Part Three</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/04/24/why-can-t-i-access-a-protected-member-from-a-derived-class-part-three.aspx#8447681</link><pubDate>Fri, 02 May 2008 00:33:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8447681</guid><dc:creator>Marcel Popescu</dc:creator><description>&lt;p&gt;&amp;quot;If it is intended to be accessible to internal subclasses, then what is the harm of it being accessible to internal non-subclasses? &amp;nbsp;You get NO additional security by going to &amp;quot;protected and internal&amp;quot; because your coworker can simply write a subclass to party on the member.&amp;quot;&lt;/p&gt;
&lt;p&gt;Ahem. &lt;/p&gt;
&lt;p&gt;double d = 5.0;&lt;/p&gt;
&lt;p&gt;int i = d;&lt;/p&gt;
&lt;p&gt;Why isn't the above allowed? You can simply write&lt;/p&gt;
&lt;p&gt;int i = (int)d;&lt;/p&gt;
&lt;p&gt;The whole point is to force you to cast it, so that you have to think if that's what you want. Same thing with writing a subclass to allow access to a &amp;quot;protected AND internal&amp;quot; member - it means that you thought about it and decided that yes, you really really want access to that member.&lt;/p&gt;
&lt;p&gt;&amp;quot;Not a high enough priority&amp;quot; is a good argument. &amp;quot;Not useful&amp;quot; is a lousy one.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8447681" width="1" height="1"&gt;</description></item><item><title>re: Why Can't I Access A Protected Member From A Derived Class, Part Three</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/04/24/why-can-t-i-access-a-protected-member-from-a-derived-class-part-three.aspx#8437498</link><pubDate>Tue, 29 Apr 2008 16:15:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8437498</guid><dc:creator>Josh Berke</dc:creator><description>&lt;p&gt;I was one of the peolpe who had always thought that protected internal would work to make things more restrictive. I never sat back and thought about how private is default so it would as you would say loosen restrictions. &lt;/p&gt;
&lt;p&gt;I disagree with your thought that there is no need for a &amp;quot;Proternal&amp;quot; since your trying to protect your own co-workers. One case where this comes into play is when a project enters maintneance mode and the original developers are no longer there. Something marked as &amp;quot;Proternal&amp;quot; helps to clearify the intent that only derived classes within this assembly should be using this method. &lt;/p&gt;
&lt;p&gt;On the same token I'd rather see other cooler language enhancements then this feature which would only be applicable a small percentage of time.&lt;/p&gt;
&lt;p&gt;I agree on sealing but I like that classes are not sealed by default. If developers had to consiously unseal thigns, then I am going to guess that a lot of the controls which I like to derive would be sealed and I'd end up having to use some sort of delegation. &lt;/p&gt;
&lt;p&gt;Nice Post;-)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8437498" width="1" height="1"&gt;</description></item><item><title>re: Why Can't I Access A Protected Member From A Derived Class, Part Three</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/04/24/why-can-t-i-access-a-protected-member-from-a-derived-class-part-three.aspx#8435927</link><pubDate>Mon, 28 Apr 2008 21:58:48 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8435927</guid><dc:creator>Kor Nielsen</dc:creator><description>&lt;p&gt;James: The main problem with using a protected method with an internal constructor is that sometimes you want to use internal types as parameters to your protected method. If your class is public, that is not a possibility. So far, the best approach I've found is to make the class internal and expose a public interface.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8435927" width="1" height="1"&gt;</description></item></channel></rss>