What Does Protected Internal Mean?

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()
  {
    Console.WriteLine("LOL!");
  }
}

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.

What others have said

Requesting Gravatar... Scott Hanselman Oct 29, 2007 10:20 PM
# re: What Does Protected Internal Mean?
Beat me to this post by 4 minutes!
Requesting Gravatar... Jon Limjap Oct 30, 2007 12:58 AM
# re: What Does Protected Internal Mean?
OT: Dotnetkicks has a swastika for its avatar. Hmmm...
Requesting Gravatar... Frans Bouma Oct 30, 2007 2:36 AM
# re: What Does Protected Internal Mean?
Shouldn't MakeSomeNoise be virtual?
Requesting Gravatar... Shannon Oct 30, 2007 6:14 AM
# re: What Does Protected Internal Mean?
I can't believe I never realised that. I was confused by the use of them together.
Requesting Gravatar... Jason Haley Oct 30, 2007 6:59 AM
# Interesting Finds: October 30, 2007
Requesting Gravatar... Eber Irigoyen Oct 30, 2007 8:11 AM
# re: What Does Protected Internal Mean?
been there done that...
Requesting Gravatar... manovich Oct 30, 2007 8:36 AM
# re: What Does Protected Internal Mean?
I'm always asking this question on interviews :)
Requesting Gravatar... Dave Oct 30, 2007 8:58 AM
# re: What Does Protected Internal Mean?
Whats with the dotnetkicks.com swastika??
Requesting Gravatar... Haacked Oct 30, 2007 9:13 AM
# re: What Does Protected Internal Mean?
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.
Requesting Gravatar... Scott Oct 30, 2007 9:16 AM
# re: What Does Protected Internal Mean?
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"
http://msdn2.microsoft.com/en-us/library/ba0a1yw2(VS.71).aspx

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?
Requesting Gravatar... Haacked Oct 30, 2007 9:19 AM
# re: What Does Protected Internal Mean?
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.
Requesting Gravatar... Anthony Oct 30, 2007 9:21 AM
# re: What Does Protected Internal Mean?
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!
Requesting Gravatar... Haacked Oct 30, 2007 9:29 AM
# re: What Does Protected Internal Mean?
@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.
Requesting Gravatar... TunnelRat Oct 30, 2007 10:03 AM
# re: What Does Protected Internal Mean?
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).
Requesting Gravatar... Norapinephrine Oct 30, 2007 11:54 AM
# re: What Does Protected Internal Mean?
Ahem... I don't think that Eilon disabused you of your ignorance. Rather, I believe he brought your attention to your ignorance this subject. ;)
Requesting Gravatar... Haacked Oct 30, 2007 12:46 PM
# re: What Does Protected Internal Mean?
@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.
Requesting Gravatar... David Crowell Oct 30, 2007 1:06 PM
# re: What Does Protected Internal Mean?
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.
Requesting Gravatar... Adel Oct 31, 2007 11:05 AM
# re: What Does Protected Internal Mean?
Man, i beat Scott Hanselman and Phill Haack, i already knew that long ago... :) yay
Requesting Gravatar... Arun Nov 01, 2007 4:34 AM
# re: What Does Protected Internal Mean?
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 ;))
Requesting Gravatar... Stephen Nov 01, 2007 8:29 AM
# re: What Does Protected Internal Mean?
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..

Nevermind!
Requesting Gravatar... Dotmad (on .Net) Nov 11, 2007 11:05 AM
# Three wrong assumptions
Did some blog reading today, and found out several assumptions I had which were wrong: Assumption : "
Requesting Gravatar... David Nelson Feb 22, 2008 3:47 PM
# re: What Does Protected Internal Mean?
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.

What do you have to say?

(will show your gravatar)
Please add 3 and 1 and type the answer here: