Defining Member and Type Visibility - Internal and Protected Internal Members
(Page 5 of 6 )
As far as internal and protected internal members go, they are mostly useful when you are creating libraries (managed dlls). internal defines members that are accessible by any type in the same assembly. protected internal defines a member whose access is limited to the current assembly or to types derived from the defining class in the current assembly. Here is an example that illustrates their use:
class Program
{
static void Main(string[] args)
{
MyClass c = new MyClass();
c.MyInternalMethod();
c.MyProtectedInternalMethod();
}
}
class MyClass
{
internal void MyInternalMethod()
{
Console.WriteLine("I am an internal method");
}
protected internal void MyProtectedInternalMethod()
{
Console.WriteLine("I am a protected internal method");
}
}
After discussing member visibility, one question comes up: how about type visibility? Should the class be accessed by other classes outside the assembly, or should its accessibility only be limited to the defining assembly?
When it comes to type visibility, there are two access modifiers of concern: public or internal.
Next: Type Visibility >>
More C# Articles
More By Ayad Boudiab