internal or protected

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... 

posted @ 2010-06-09 11:01  pursue  阅读(287)  评论(0编辑  收藏  举报