What is the difference between "secure" and "secure internal" modifiers in .NET?

What is the difference between secure and secure internal modifiers in .NET?

+4
source share
4 answers

private

Access is permitted only in a certain type

protected

private access extends to include inherited types

interior

private access extends to include other types in the same assembly

And therefore it follows that:

secure internal

private access is expanded to allow access for types that are either inherited or in the same assembly as this type, or both.

Basically, first think of everything as private , and anything else that you see as expanding on it.

+10
source

protected

Members are visible only to inherited types.

protected internal

Members are visible only for inheritance types, as well as for all types that are also contained in the same assembly as the declaration type.

Here is a C # example:

 class Program { static void Main() { Foo foo = new Foo(); // Notice I can call this method here because // the Foo type is within the same assembly // and the method is marked as "protected internal". foo.ProtectedInternalMethod(); // The line below does not compile because // I cannot access a "protected" method. foo.ProtectedMethod(); } } class Foo { // This method is only visible to any type // that inherits from "Foo" protected void ProtectedMethod() { } // This method is visible to any type that inherits // from "Foo" as well as all other types compiled in // this assembly (notably "Program" above). protected internal void ProtectedInternalMethod() { } } 
+7
source

As usual, from one of Fabulous Eric Lippert 's Posts :

Many people think that [ protected internal ] means that "M is available for all derived classes that are in this assembly." Is not. In fact, this means that "M is available for all derived classes and for all classes in this assembly." That is, it is a less restrictive combination, not a more restrictive combination.

This is contrary to most people. I tried to understand why, and I think I have it. I think that people understand internal , protected and private as restrictions from the "natural" state of public . With this model, protected internal means "apply both a protected constraint and an internal constraint."

This is the wrong way to think about it. Rather, internal , protected and public are weakenings of the "natural" state of private . private - the default value in C #; if you want to make something more accessible, you have to say it. With this model, it becomes clear that protected internal is a weaker restriction than one.

+2
source

for the difference between protected and protected internal lemme give a brief example and I will follow the example ...

Country A : One Build

Country B : another different build

X (base class) is a pair of Y (inherited class) in country A

Z (Inherited Class X ) is another son of X in country B.

X has the property.

  • if X mentions the property as protected , then X says: all my sons are Y and Z , only you both can access my property wherever you stay ... god bless you. No one can access my property except you.

  • if X mentions the property as protected internal , then X says: All the people in my country A , including my son Y , can access my property. dear son Z , you can access my property in Country B

hope you guys understand ...

Thank you.

+1
source

All Articles