What Does Protected Internal Mean?

0 comments suggest edit

Pop quiz for you C# developers out there. Will the following code compile?

//In Foo.dll
public class Kitty
  protected internal virtual void MakeSomeNoise()
    Console.WriteLine("I'm in ur serverz fixing things...");

//In Bar.dll
public class Lion : Kitty
  protected override void MakeSomeNoise()

If you had asked me that yesterday, I would have said hell no. You can’t override an internal method in another assembly.

Of course, I would have been WRONG!

Well the truth of the matter is, I was wrong. This came up in an internal discussion in which I was unfairly complaining that certain methods I needed to override were internal. In fact, they were protected internal. Doesn’t that mean that the method is both protected and internal?

Had I simply tried to override them, I would have learned that my assumption was wrong. For the record…

protected internal means protected OR internal

It’s very clear when you think of the keywords as the union of accessibility rather than the intersection. Thus protected internal means the method is accessible by anything that can access the protected method UNION with anything that can access the internal method.

A Donkey Named Lester - Creative Commons By Attribution -
ninjapoodlesAs the old saying goes, when you assume, you make an ass out of u and me. I never understood this saying because when I assume, I only make an ass of me. I really think the word should simply be assme. As in… 

Never assme something won’t work without at least trying it.

UPDATE: Eilon, sent me an email to point out that…

BTW the CLR does have the notion of ProtectedANDInternal, but C# has no syntax to specify it. If you look at the CLR’s System.Reflection.MethodAttributes enum you’ll see both FamANDAssem as well as FamORAssem (“Family” is the CLR’s term for C#’s protected and “Assem” is C#’s internal).

If you don’t know Eilon, he’s a freaking sharp developer I get to work with on the MVC project and was the one who kindly disabused me of my ignorance on this subject. He keeps a blog at http://weblogs.asp.net/leftslipper/.

Apparently he’s the one with the clever idea of using a C# 3.0 anonymous type as a dictionary, that many of you saw in ScottGu’s ALT.NET Conference talk. Very cool.

Found a typo or error? Suggest an edit! If accepted, your contribution is listed automatically here.



28 responses

  1. Avatar for Scott Hanselman
    Scott Hanselman October 29th, 2007

    Beat me to this post by 4 minutes!

  2. Avatar for Jon Limjap
    Jon Limjap October 29th, 2007

    OT: Dotnetkicks has a swastika for its avatar. Hmmm...

  3. Avatar for Frans Bouma
    Frans Bouma October 29th, 2007

    Shouldn't MakeSomeNoise be virtual?

  4. Avatar for Shannon
    Shannon October 29th, 2007

    I can't believe I never realised that. I was confused by the use of them together.

  5. Avatar for Eber Irigoyen
    Eber Irigoyen October 29th, 2007
  6. Avatar for manovich
    manovich October 29th, 2007

    I'm always asking this question on interviews :)

  7. Avatar for Dave
    Dave October 29th, 2007

    Whats with the dotnetkicks.com swastika??

  8. Avatar for Haacked
    Haacked October 29th, 2007

    The "swastika" is an Identicon, which is generated based on the ip address.
    Eric Kemp and I did some work to make them less swastika like, but apparently that one did not take. I may have overwritten the newer Identicon handler inadverdently.

  9. Avatar for Scott
    Scott October 29th, 2007

    Wow, that is an unfortunate IP gravitar for DotNetKicks.
    So if the CLR contains a scope declaration, but there's no syntax available for using it. Does it really exist?
    I thought of those two as independent. protected just means "off-limits for people who aren't me" and internal means "for people who are in my family"
    What are the implications for protected AND internal? That restricts accessibility/overideability to only classes that derive from the base and are in the same assembly correct?

  10. Avatar for Haacked
    Haacked October 29th, 2007

    ep, I accidentally overwrote the newer Identicon.dll with the older one. The newer one will be included in the next version of Subtext.
    Apologies to anyone who took offense. I understand it's a powerful symbol, but it was unintentional.

  11. Avatar for Anthony
    Anthony October 29th, 2007

    Base method is not virtual and neither method has a return type. Phil, you must have done that in notepad or the IDE would have complained! haha, great tidbit of knowledge, though. I would have never guessed that internal didnt actually mean internal to the assembly in this case!

  12. Avatar for Haacked
    Haacked October 29th, 2007

    @Anthony, I have no idea what you're talking about. The method is virtual and returns void.
    Ok, I admit it. I wrote that in WLW and just now fixed it.
    @Scott - yep. That is the implication. Not altogether useful except for internal extensibility.

  13. Avatar for TunnelRat
    TunnelRat October 29th, 2007

    Without reading the answer, I took a SWAG and guessed that it would compile. I just saw some kinda has-a relationship...
    But it would make a good interview question, to at least weed out the VB guys (which I used to be) who are wading into C# (which I am).

  14. Avatar for Norapinephrine
    Norapinephrine October 30th, 2007

    Ahem... I don't think that Eilon disabused you of your ignorance. Rather, I believe he brought your attention to your ignorance this subject. ;)

  15. Avatar for Haacked
    Haacked October 30th, 2007

    @Norapinephrine, I probably should have said he "disabused me of that faulty understanding". I had read this in CLR via C# a long time ago, and totally forgot about it.

  16. Avatar for David Crowell
    David Crowell October 30th, 2007

    I also wrongly assumed what protected internal would do, but never tried it.
    What's worse, I asked a few developer candidates about it, and didn't know myself. Oddly, none of them tried to answer the question, and instead brushed it off. No we didn't hire them.

  17. Avatar for Adel
    Adel October 31st, 2007

    Man, i beat Scott Hanselman and Phill Haack, i already knew that long ago... :) yay

  18. Avatar for Arun
    Arun October 31st, 2007

    In one of the companies I worked with I would interview C# devs we wanted to hire. If someone was doing particularly well I'd throw this question at him/her and see what they had to say. A bit mean you might think but it was still quite fun :D
    Also, there's that other chestnut. Is string a reference or value type. And if the answer was that its a reference type (which is correct) then how come the + operator works to concatenate strings? You'd be surprised how many of the new breed of devs don't know about operator overloading (especially those from a Java background ;))

  19. Avatar for Stephen
    Stephen October 31st, 2007

    Personally I've got this thing against internal anyway, so I try to avoid using it :S..
    I still have the illusion that I can make code somewhat 'protected'.. despite the fact I know a simple reflection call can destroy that..

  20. Avatar for Dotmad (on .Net)
    Dotmad (on .Net) November 11th, 2007

    Did some blog reading today, and found out several assumptions I had which were wrong: Assumption : "

  21. Avatar for David Nelson
    David Nelson February 22nd, 2008

    It always surprises me when long-time C# developers don't know the answer to this question. I ask it in interviews and nobody EVER knows. Some of them guess right, but nobody knows it off the top of their head. I understand its not something you ever use, but how do you not learn it the same day you are learning about the other accessibility modifiers?
    On a separate note, did you really refer to Eilon's anonymous-type-as-a-dictionary trick as "clever"? I think its the ugliest thing I have ever seen, and it scares me to death that the future of C# programming might be headed in that direction.

  22. Avatar for John Monagle
    John Monagle January 24th, 2011

    I know why long-term developers don't know the answer at interview:
    When was the last time you felt the need to write a protected internal method?
    To me it's a stupid interiew question that bears no resemblence to day-to programming.

  23. Avatar for Alok Rawat
    Alok Rawat March 3rd, 2011

    This type of member can be accessed from the current project or from the type inherited from their containing type.

  24. Avatar for James Chen
    James Chen May 7th, 2011

    If I remove "internal" from Kitty's virtual method MakeSomeNoise(), this method can still be overridden by Lion. Actually any sub-class (no matter it's internal or external) can override its base class' virtual methods.

    So, I think your example is not a good one to illustrate "protected internal" modifier. Here is my example:
    //in Foo.dll
    public class Kitty
    protected internal void Method1()
    Console.WriteLine("This is from Kitty's Method1");
    public class B
    public void Method1()
    var c = new Kitty();
    //Kitty's Method1 is accessible, because B and Kitty are in the same assembly, even though Kitty's Method1 is protected.

    //in Bar.dll
    public class Lion : Kitty
    public void Method2()
    //Kitty's Method1 is accessible, because Lion inherits from Kitty, even though they are not in the same assembly.

  25. Avatar for Sam Adam
    Sam Adam June 5th, 2011

    Urgent please

  26. Avatar for Dominic Efosa
    Dominic Efosa August 1st, 2011

    Was asked this question last week by a nerd.
    Didn't find it funny as l don't think it was ever used in building their products.
    All is done is make me what to make a potential junior developer's interview experience a brutal one.

  27. Avatar for sharmin
    sharmin December 14th, 2011

    This article is very helpful as I was having hard time understanding by reading from MSDN. Thank you very much!

  28. Avatar for siri
    siri June 11th, 2012

    its very nice