Saturday, December 6, 2008

C# Protected Internal

In Microsoft's C# programming language, members that are marked "protected internal" acts as protected OR internal. Many developers believe "protected internal" acts as protected AND internal, meaning that the members could be accessed from a derived class within the same assembly only. In fact, I did a search on Google on this and it yielded two forum messages from MSDN where the first person who posted an answer got it wrong. Thankfully, helpful individuals later gave the correct answer.

The access modifier "protected internal" actually means the member could be accessed from a derived class OR any other class in the same assembly.

Both definitions don't feel right for the following reason:
-The incorrect definition means there is a base class that exposes different members or methods depending on whatever the derived class is in the same assembly.

-The correct definition means the method that is used by any other classes in the same assembly and derived class in another assembly.

I really have trouble visualizing a use for this. If the method is used by other classes in the same assembly, why not let other classes in a different assembly use it too? Why couldn't the class/method be refactored out? It just seems to add complexity.

0 comments: