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

旧时模样 提交于 2019-12-21 12:58:06

问题


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


回答1:


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.



回答2:


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




回答3:


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




回答4:


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




回答5:


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.




回答6:


- Update answer 2019 -

Hi You can find accessibility via below table




回答7:


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)




回答8:


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




回答9:


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.



来源:https://stackoverflow.com/questions/3500298/what-is-the-difference-between-access-specifier-protected-and-internal-protected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!