The value of the secure internal access specifier in the code below

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 can call func() from the base class, therefore, it shows that the access modifier property is protected , but as for the access modifier property internal . Should it be allowed access to func() inside another assembly, since it is declared internal. If so, then why call it protected internal and not just protected

+4
source share
4 answers

You might want to give this read.

Secure internal accessibility means secure OR internal, not secure AND internal. In other words, the protected internal member can be accessed from any class in the same assembly, including derived classes. To restrict access to only derived classes in the same assembly, declare the inner class and declare its members as protected.

+4
source

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

MSDN topic for access modifiers for reference:

secure inner

Access to the type or member can be obtained by any code in the assembly in which it is declared, or from a derived class in another assembly. Access from another assembly must occur in the class declaration, which comes from the class in which the protected inner element is declared, and it must be executed through an instance of the type of the derived class.

+4
source

From MSDN (click for more information):

protected

Only a code in the same class or structure or in a derived class can access a type or member.

interior

Access to a type or member can be obtained by any code in the same assembly, but not from another assembly.

protected internal :

Access to a type or member can be obtained by any code in the same assembly or any derived class in another assembly.

0
source

"What is the use of some thing, such as the protected inner, when the" inner "in the protected inner has no meaning whatsoever":

In Assembly BL, Class X, you can use the new BasicClass (). func () directly, because you have set the "internal" flag. If this flag has not been set, to access the func () function, you will need to get the class X from BasicClass.

0
source

Source: https://habr.com/ru/post/1469248/


All Articles