What is the difference between 'protected' and 'protected internal'?
The "protected internal" access modifier is a union of both the "protected" and "internal" modifiers.
From MSDN, Access Modifiers (C# Programming Guide):
protected:
The type or member can be accessed only by code in the same class or
struct, or in a class that is derived from that 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 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.
Note that: protected internal
means "protected
OR internal
" (any class in the same assembly, or any derived class - even if it is in a different assembly).
...and for completeness:
private:
The type or member can be accessed only by code in the same class or
struct.
public:
The type or member can be accessed by any other code in the same
assembly or another assembly that references it.
private protected:
Access is limited to the containing class or types derived from the
containing class within the current assembly.
(Available since C# 7.2)
Difference between private protected and internal protected
From Access Modifiers (C# Programming Guide)
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.
And
Private Protected : The type or member can be accessed only within its
declaring assembly, by code in the same class or in a type that is
derived from that class.
Another useful link C# 7 Series, Part 5: Private Protected
Confusion: Internal, Protected and Protected Internal
The following five accessibility levels can be specified using the access modifiers:
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.
Taken directly from Microsoft's MSDN library.
Protected vs protected internal (Again) in c#
protected
means that you can access the member from any subtype (and of course from the declaring type itself). So regardless of where that subtype is, even if it is in another assembly, you will still have access to all protected members.
internal
means that you can access the member from any type in the same assembly. So even a completely unrelated class that lives in the same assembly can access the member.
protected internal
combines both, meaning that both apply separately. So you can access the member from any subtype, and you can also access the member from any type in the same assembly.
// Assembly 1
class A {
protected int foo;
internal int bar;
protected internal int baz;
}
class B : A {} // can access: foo, bar, baz
class C {} // can access: bar, baz
// Assembly 2
class D : A {} // can access: foo, baz
class E {} // can access neither
Is there a difference between Protected Internal and Internal Protected?
Is there a difference between
protected internal
andinternal protected
members?
There is no difference between them.
protected internal
means protected
OR internal
.
internal protected
means internal
OR protected
.
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.
Of the two, protected internal
is commonly used. There is no reference to internal protected
on the MSDN page about Access Modifiers.
Also check out Phil Haack's blog post What Does Protected Internal Mean?
What is the difference between private and protected Internal ?
- A
protected internal
member is visible to any code in the current assembly or in a derived class in another assembly. In technical words, it's the logical disjunction ofprotected
andinternal
. - A
private
member is visible only to code in the same class.
protected internal
is actually the second most permissive access modifier after public
.
It's worth noting that protected
is arguably more permissive than internal
, since it allows access from code that you have no control over (i.e. other assemblies). While internal
allows access from all code in the current assembly, this code is yours and you have control over it!
To paraphrase, protected
(and protected internal
) members are part of the public API of your assembly (and should therefore be documented). internal
members are not.
what is the difference between protected and private protected in c#?
It's about the acces modifier. More specific: inheritance and multiple assemblies. Consider the following:
For normal protected
(explained with along private
):
class Base
{
private bool X;
protected bool Y;
}
class A : Base
{
public void Foo()
{
X = false; //error: cannot access private member.
Y = true; //can access protected member, but only from classes with `: Base`
}
}
class B
{
public void Foo()
{
A a = new A();
a.X = false; //error: cannot access private member.
a.Y = false; //error: cannot access protected member.
}
}
Now the difference with private protected
is that it must live in the same assembly to be accessible:
So:
class A : Base
{
public void Foo()
{
X = false; //error: cannot access private member.
Y = true; //can access protected member, but only from classes with `: Base` AND
//they need to be defined in the same assembly as Base
}
}
Is valid, but only if both A
and Base
are compiled in the same assembly/dll/exe etc.
Now, since that clear, when would you use
an actual private protected
?
A lot can be said about this. Some (including me) would argue that the use of private protected
is an anti-pattern, because in my oppinion it's closly related to the friend keyword. And I must say, although in contradiction to friend
, private protected
keeps "the dirt" isolated, it still is arbitrary behavior, logic, depending on the location of it's definition.
Having said that the question remains, when to use it. You might be surprised I punctually used it once, and it was quite helpful.
Consider the following case:
- Having a conceptual, decorator pattern styled code base, for example some
graphical object
system. - All the objects will be "renderable", in various ways.
- Because you have a lot, it comes in handy to create a base class which only you are using for convinience.
- You don't want to let other user be using this functionality, since it's not well documented and really specifi to your implementation
- The classes themselves are public.
.... then I would use private protected
;-)
What the different between protected VS internal?
AFAIK there is not such thing as protected or internal projects.Projects contain classes and other type and are those elements inside the projects that can have different access modifiers.
Internal modifier: Can be referenced/accesed only within the same assembly (same .exe or .dll)
Protected modifier: Can be referenced/accesed from classes that inherit from him, the class that inherit it could potentially be in another assembly (for example, a protected method inside a public class, could be called only from another class that inherits from it, but that class could be in another assembly/project because the class was declared public).
There are also other accessibility modifiers, one of them I have just mentioned:
Public modifier: Can be referenced/accesed from classes from the same assembly or other assemblies.
Private modifier: Can be referenced/accesed from the same class it has been declared.
Example
Assembly/Project1
public class ClassA {
private string variable1;
protected string variable2;
public string variable3;
internal string variable4;
public void MyFancyProcess()
{
Console.Write(variable1);
}
}
public class ClassB : ClassA {
public string variable5;
private void DoSomeStuff()
{
Console.Write(variable1); // This would raise an error, its is private and only could be called from ClassA *even if ClassB is child of ClassA*
Console.Write(variable2); // I can access variable2 because it is protected *AND ClassB is child of ClassA*
Console.Write(variable3); // This would work, public has no restrictions
Console.Write(variable4); // This would work, ClassB and ClassA share same assembly
}
}
internal class ClassC
{
ClassA myAClassInstance = new ClassA();
Console.Write(myAClassInstance.variable1); // This would raise an error, it is exclusive to ClassA and we are in ClassC
Console.Write(myAClassInstance.variable2); // This would raise an error, because ClassC does not inherit ClassA
Console.Write(myAClassInstance.variable3);
Console.Write(myAClassInstance.variable4); // This would work, ClassC and ClassA share same assembly
}
Assembly/ProjectB
public class ClassD : ClassA // Please note that if ClassA where not public it would raise an error
{
public void ExecuteMyLogic()
{
Console.Write(variable1); // This would raise an error, its is private and only could be called from ClassA
Console.Write(variable2); // I can access variable2 because it is protected *AND ClassD is child of ClassA despite being a different assembly*
Console.Write(variable3); // This would work, public has no restrictions
Console.Write(variable4); // This would raise an error, they are different assemblies
}
}
What is the difference between protected internal static and internal static in C#?
protected
means that it can also be accessed from derived classes.
In your case, since the class itself is internal, you cannot have derived classes outside the project, so it doesn't add anything to internal
.
Related Topics
How to Rethrow Innerexception Without Losing Stack Trace in C#
"The Controls Collection Cannot Be Modified Because the Control Contains Code Blocks"
Conditional Compilation and Framework Targets
Compiling/Executing a C# Source File in Command Prompt
Detect If Running as Administrator with or Without Elevated Privileges
How to Know If a Process Is Running
Inner Join of Datatables in C#
Conversion of a Datetime2 Data Type to a Datetime Data Type Results Out-Of-Range Value
Compiler Ambiguous Invocation Error - Anonymous Method and Method Group with Func<> or Action
How to Change Datatype of a Datacolumn in a Datatable
How to Embed a Text File in a .Net Assembly
Generics Open and Closed Constructed Types