iJade
iJade

Reputation: 23811

Significance of protected Internal access specifier in the below code

ok, let me start with an example.This is my base class in another assembly

namespace BL
{
    public class BasicClass
    {
        protected internal void func()
        {
            //Code Logic
        }
    }
}

Now this is my derived class in another assembly

namespace DL
{
    public class DerivedClass:BasicClass
    {
        private void hello()
        {
            func();
        }
    }
}

I'm able to call the func() from base class , hence it shows that the protected access modifier property but what about the internal access modifier property.Should it be allowed to access func() inside another assembly since its declared internal.If so then why call it protected internal and not simple protected

Upvotes: 4

Views: 3768

Answers (4)

bob the builder
bob the builder

Reputation: 138

"What is the use of some thing like protected internal when the "internal" in protected internal has no significance at all":

In Assembly BL, Class X you can use new BasicClass().func() directly, because you have the "internal" flag set. If this flag was not set, class X would need to derive from BasicClass in order to access func().

Upvotes: 0

शेखर
शेखर

Reputation: 17614

From MSDN (click for more information):

protected:

The type or member can only be accessed by code in the same class or struct, or in a derived class.

internal:

The type or member can be accessed by any code in the same assembly, but not from another assembly.

protected internal:

The type or member can be accessed by any code in the same assembly, or by any derived class in another assembly.

Upvotes: 0

Steffan Donal
Steffan Donal

Reputation: 2354

You might want to give this a read.

The protected internal accessibility level means protected OR internal, not protected AND internal. In other words, a protected internal member can be accessed from any class in the same assembly, including derived classes. To limit accessibility to only derived classes in the same assembly, declare the class itself internal, and declare its members as protected.

Upvotes: 5

J0HN
J0HN

Reputation: 26951

Internal means that the member of the class is available for all classes in the same assembly, but not available to any class outside the assembly. Protected internal means the ,member is accessible to any class in the same assembly and any subclass in any other assembly.

MSDN topic on access modifiers for reference:

protected internal

The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.

Upvotes: 3

Related Questions