276

Can someone please explain the difference between the protected and protected internal modifiers in C#? It looks like their behavior is identical.

mfluehr
  • 2,832
  • 2
  • 23
  • 31
Embedd_0913
  • 16,125
  • 37
  • 97
  • 135

12 Answers12

433

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)

M4N
  • 94,805
  • 45
  • 217
  • 260
  • 3
    Can I have a member `protected internal` so that's it's `protected` in current assembly and completely unavailable externally? – Shimmy Weitzhandler Oct 24 '15 at 23:16
  • 8
    That would be "Protected", would it not? – CAD bloke Mar 25 '16 at 20:59
  • 3
    @Shimmy: you can have an internal *class* with protected *methods*. But then the whole class will be unavailable from external assemblies. – M4N Mar 27 '16 at 20:31
  • 1
    @Shimmy take a look at this proposal for a future version of C# https://github.com/dotnet/roslyn/blob/features/privateProtected/docs/features/private-protected.md – Nate Cook Apr 30 '16 at 01:35
  • Protected Internal should not be "EITHER OR" because separately they're already used instead its AND to provide feature of protected outside of current Assembly. – Vishal Patwardhan Sep 24 '18 at 11:03
  • 1
    @CADBloke No, 'protected' allows access from subclasses in other assemblies. What Shimmy is looking for is not available until C# 7.2 ('private protected'). – John B. Lambe Feb 12 '19 at 17:42
115

This table shows the difference. protected internal is the same as protected, except it also allows access from other classes in the same assembly.

Comparison of C# modifiers

mfluehr
  • 2,832
  • 2
  • 23
  • 31
Andi AR
  • 2,678
  • 2
  • 23
  • 28
  • 9
    Beautiful answer, it very clearly communicates the differences between each access modifier. – e_i_pi Sep 26 '19 at 23:21
  • Did you create this table yourself or is there an external source for it? – O. R. Mapper Apr 18 '21 at 22:53
  • 4
    @O.R.Mapper, i created in excel and captured as image. Its not external source. – Andi AR May 14 '21 at 18:19
  • 1
    "protected internal is the same as protected - except when it isn't" - seriously though a great table, thanks for sharing. – Richard Moore Jan 12 '23 at 14:33
  • 1
    Guys, Microsoft now following my style ... https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers#summary-table – Andi AR Sep 01 '23 at 11:43
94

protected can be used by any subclasses from any assembly.

protected internal is everything that protected is, plus also anything in the same assembly can access it.

Importantly, it doesn't mean "subclasses in the same assembly" - it is the union of the two, not the intersection.

Rudi Visser
  • 21,350
  • 5
  • 71
  • 97
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 3
    Just an FYI for the readers that CLR supports the concept of the intersection of protected and internal accessibility as well, but C# doesn't support this. C# supports only union of the two as mentioned in this post. – RBT Dec 26 '16 at 05:24
  • 1
    Just another FYI for readers, "subclasses in the same assembly" can be achieved with the `private protected` access modifier that was introduced in C# 7.2 – LordWilmore Apr 13 '18 at 15:02
23

In practice, about methods:

protected - accessible for inherited classes, otherwise private.

internal - public only for classes inside the assembly, otherwise private.

protected internal - means protected or internal - methods become accessible for inherited classes and for any classes inside the assembly.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • 1
    I would use OR to express that cause it is either not both that has to be true. – Brian Rasmussen Feb 25 '09 at 13:21
  • I do not completely agree with the part "for changing base class behavior" in the description of "protected". I'd say this is where you use "virtual" (on the base class) and "override" (on the derived class). – M4N Feb 25 '09 at 13:24
  • Is there a way to mark a member as `protected` AND `internal`? – Shimmy Weitzhandler Oct 24 '15 at 23:16
  • @Shimmy: yes, `protected internal`. – abatishchev Oct 24 '15 at 23:19
  • No, it's `protected` OR `internal` whereas I need `protected` AND `internal`, meaning it should be available for subclasses in current assembly only. I believe this is not possible directly. – Shimmy Weitzhandler Oct 24 '15 at 23:32
  • @Shimmy: make class `internal` and methods `protected`? – abatishchev Oct 24 '15 at 23:34
  • No. I want a public class to have a protected property that is only available internally. I've researched it. Impossible. A workaround, is make the property `internal` with a private setter, and have a `protected` set method. Anyway, you're answer made it clear to me what the `protected internal` modifier does, your use of the OR emphasized to me what it literally means. Thanks for that. – Shimmy Weitzhandler Oct 25 '15 at 01:35
  • 1
    @Shimmy two years later, and yes. Now there is a way in C# 7.2. Its called `private protected` https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/private-protected – Pauli Østerø Nov 17 '17 at 19:19
10

There is still a lot of confusion in understanding the scope of "protected internal" accessors, though most have the definition defined correctly. This helped me to understand the confusion between "protected" and "protected internal":

public is really public inside and outside the assembly (public internal / public external)

protected is really protected inside and outside the assembly (protected internal / protected external) (not allowed on top level classes)

private is really private inside and outside the assembly (private internal / private external) (not allowed on top level classes)

internal is really public inside the assembly but excluded outside the assembly like private (public internal / excluded external)

protected internal is really public inside the assembly but protected outside the assembly (public internal / protected external) (not allowed on top level classes)

As you can see protected internal is a very strange beast. Not intuitive.

That now begs the question why didn't Microsoft create a (protected internal / excluded external), or I guess some kind of "private protected" or "internal protected"? lol. Seems incomplete?

Added to the confusion is the fact you can nest public or protected internal nested members inside protected, internal, or private types. Why would you access a nested "protected internal" inside an internal class that excludes outside assembly access?

Microsoft says such nested types are limited by their parent type scope, but that's not what the compiler says. You can compiled protected internals inside internal classes which should limit scope to just the assembly.

To me this feels like incomplete design. They should have simplified scope of all types to a system that clearly consider inheritance but also security and hierarchy of nested types. This would have made the sharing of objects extremely intuitive and granular rather than discovering accessibility of types and members based on an incomplete scoping system.

Stokely
  • 12,444
  • 2
  • 35
  • 23
7

protected: the variable or method will be available only to child classes (in any assembly)

protected internal: available to child classes in any assembly and to all the classes within the same assembly

BenMorel
  • 34,448
  • 50
  • 182
  • 322
3

I have read out very clear definitions for these terms.

Protected : Access is limited to within the class definition and any class that inherits from the class. 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 : Access is limited to exclusively to classes defined within the current project assembly. The type or member can be accessed only by code in same class.

Protected-Internal : Access is limited to current assembly or types derived from containing class.

Ammar Asjad
  • 2,920
  • 6
  • 29
  • 42
2

Think about protected internal as applying two access modifier (protected, and internal) on the same field, property or method.

In the real world, imagine we are issuing privilege for people to visit museum:

  1. Everyone inside the city are allowed to visit museum (internal).
  2. Everyone outside of the city that their parents live here are allowed to visit museum (protected).

And we can put them together in these way:

Everyone inside the city (internal) and everyone outside of city that their parents live here (protected) are allowed to visit the museum (protected internal).

Programming world:

internal: The field is available everywhere in the assembly (project). It is like saying it is public in its project scope (but can not being accessed outside of project scope even by those classes outside of assembly which inherit from that class). Every instance of that type can see it in that assembly (project scope).

protected: simply means that all derived classes can see it (inside or outside of assembly). For example derived classes can see the field or method inside its methods and constructors using: base.NameOfProtectedInternal.

So, putting these two access modifier together (protected internal), you have something that can being public inside the project, and can be seen by those which have inherited from that class inside their scope.

They can be written in the internal protected, and does not change the meaning, but it is convenient to write it protected internal.

Hassan Monjezi
  • 1,060
  • 1
  • 8
  • 24
1

public - The members (Functions & Variables) declared as public can be accessed from anywhere.

private - Private members cannot be accessed from outside the class. This is the default access specifier for a member, i.e if you do not specify an access specifier for a member (variable or function), it will be considered as private. Therefore, string PhoneNumber; is equivalent to private string PhoneNumber.

protected - Protected members can be accessed only from the child classes.

internal - It can be accessed only within the same assembly.

protected internal - It can be accessed within the same assembly as well as in derived class.

Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
Piush shukla
  • 165
  • 1
  • 4
  • 16
1

Protected Member

Protected Member of a class in only available in the contained class (in which it has been declared) and in the derived class within the assembly and also outside the assembly.

Means if a class that resides outside the assembly can use the protected member of the other assembly by inherited that class only.

We can exposed the Protected member outside the assembly by inherited that class and use it in the derived class only.

Note: Protected members are not accessible using the object in the derived class.

Internal Member

Internal Member of a class is available or access within the assembly either creating object or in a derived class or you can say it is accessible across all the classes within the assembly.

Note: Internal members not accessible outside the assembly either using object creating or in a derived class.

Protected Internal

Protected Internal access modifier is combination Protected or Internal.

Protected Internal Member can be available within the entire assembly in which it declared either creating object or by inherited that class. And can be accessible outside the assembly in a derived class only.

Note: Protected Internal member works as Internal within the same assembly and works as Protected for outside the assembly.

Mostafa Bouzari
  • 9,207
  • 3
  • 16
  • 26
0

Protected internal best suites when you want a member or type to be used in a derived class from another assembly at the same time just want to consume the member or type in the parent assembly without deriving from the class where it is declared. Also if you want only to use a member or type with out deriving from another class, in the same assembly you can use internal only.

0

This description might be helpful

Internal Member

Internal Member of a class is available or access within the assembly either creating object or in a derived class or you can say it is accessible across all the classes within the assembly.

Protected Member

Protected Member of a class in only available in the contained class (in which it has been declared) and in the derived class within the assembly and also outside the assembly.

Protected Internal

Protected Internal access modifier is combination Protected or Internal.

Protected Internal Member can be available within the entire assembly in which it declared either creating object or by inherited that class. And can be accessible outside the assembly in a derived class only.

R15
  • 13,982
  • 14
  • 97
  • 173