开发者

What is the difference between access specifier protected and internal protected in C#

What is the diffe开发者_Python百科rence between access specifier protected and internal protected in C# ?


Internal can be seen within the assembly.

Protected can be seen by classes inheriting from the class where it is defined.

Protected internal can be seen within the assembly OR types derived from the class where it is defined (including types from other assemblies).

See: http://msdn.microsoft.com/en-us/library/ba0a1yw2.aspx

Copied from the page:

public              Access is not restricted.
protected           Access is limited to the containing class or types derived from the containing class.
internal            Access is limited to the current assembly.
protected internal  Access is limited to the current assembly or types derived from the containing class.
private             Access is limited to the containing type.


protected means only the current class and any classes deriving from it have access to the member.

internal means any class within the current assembly has access to the member.

protected internal essentially means protected or internal; i.e., all classes deriving from the current class (in any assembly) have access to the member, as do all classes in the current assembly. This is in contrast with what many developers expect -- that protected internal would mean the same thing as protected and internal (it doesn't).


  • internal - Visible by anything within the same assembly (.dll or .exe).
  • protected - Visible by any sub-classes, no matter where they are.
  • internal protected - Visible by anything within the same assembly and any sub-classes, no matter where they are.

The way Jeff Mattfield says "internal further reduces that visibility" makes it unclear. internal and protected are simply different visibilities. Having both together makes the member more visible. The default visibility of something with no explicit access modifiers, is as small as possible. Adding any access modifiers always increases the visibility.


internal protected or protected internal which is the same means externally protected (from outside the current assembly) and internally public (from within the same assembly).


Protected internal and protected access specifier relate to the concept of inheritance.

Let us take example to explain protected and protected internal.

There are two namespaces named namespace A and namespace B.

In namespace A, there is a class named classA which consists of a method named accept() using protected access specifier.

In namespace B, there is another class, named classB, which inherits from classA of namespace A.

Now with the help of this protected specifier we can access that accept() method in the classB of namespace B.

But that concept is not true when using the protected internal access specifier: if accept() function of classA of namespace A was using protected internal access specifier, then classB of namespace B cannot access it because that accept() function can only be accessed within the inherited class within the same namespace.


- Update answer 2019 -

Hi You can find accessibility via below table

What is the difference between access specifier protected and internal protected in C#


internal protected allows you to access members within the same assembly from classes which are not derived from the same object, but also allows the standard protected access you get for accessing the members from another assembly. It's internal | protected, not internal & protected (although the CLR allows the latter, C# does not)


To better Understand difference between protected and Protected Internal.it is better to first know what is difference between Protected and internal.

Internal variable refer to same assembly.Yo can't access in different assembly. protected variable like private variable but you can access in drived class in same assembly or diffrent assembly.

 namespace InternalTest   ----This namespace in assembly One
 {
    Public class A
    {
       B ol=new B();
        Console.WriteLine(ol.x);//Output:5  
        Console.WriteLine(ol.y);//error will occured. Because protected is like Private variable
    }

    public class B
     {
        Internal int x=5;
         Protected int y=5;

      }
 }

2) Take diffrent assembly.

     using InternalTest;   
     namespace InternalTest1   ----This namespace in assembly Two
     {
       Public class A1:B
        {
           Public void GetInternalValue()
          {                     return x; //error can't access because this is internal

          }

          Public void GetProtectedValue()
          {
                return y;//Work because it's protected

          }
      }
       public class C
       {


       }

    }

from above example it clears you can access internal in same assembly but not in different assembly.You can say in same assembly it look likes public variable.you can assign value by creating object of class

3)protected internal have goodness of both in same assembly it look like public variable. in diifrent assembly you use like protected variable


PROTECTED

If the class is declared as "protected" means it can be accessed by the child class within the assembly as well as child class from outside the assembly.

INTERNAL

If the class is declared as "internal" means it can be accessed by any class within the assembly.

PROTECTED INTERNAL

If the class is declared as "protected internal" means it can be accessed within the assembly by their derived class only.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜