What is the difference between 'protected' and 'protected internal'? - c#

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

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)

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

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.

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.

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.

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

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.

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:
Everyone inside the city are allowed to visit museum (internal).
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.

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.

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.

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.

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.

Related

What is the purpose of protected internal? [duplicate]

Can someone please explain the difference between the protected and protected internal modifiers in C#? It looks like their behavior is identical.
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)
This table shows the difference. protected internal is the same as protected, except it also allows access from other classes in the same assembly.
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.
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.
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.
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
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.
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:
Everyone inside the city are allowed to visit museum (internal).
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.
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.
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.
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.
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.

If I declare an internal class, what the default access level of internal members be?

I'm building a DLL with some basic functions. Long story short, I'm making a few static classes for the use by devs. These classes use some other classes that do the dirty work, which I marked as internal because I don't want people to access them.
The question is: If I declare a class as internal, what the access level of his members will be?
I'll have to mark as internal all of its members or they are automatically labeled as internal too?
It's a good 2 hours I'm googling and searching in stackoverflow and I'm struggling to find a clear and straight answer which doesn't include 1000 speculations, technical not-so-probable hypotesis and useless decorations...
MSDN is confusing as usual (never found a clear answer on msdn).
From what I can read here http://msdn.microsoft.com/en-us/library/ms173121.aspx I guess that no matter how you set a class access level, all his members will be private (methods, variables and so on).
Help, I don't know
The question is: If I declare a class as internal, what the access
level of his members will be?
The default will be private. If anything else, then it depends. If they are anything other than public, then the access modifier applies as described on MSDN (e.g. not visible outside of the assembly).
However, in the link you posted, there is one gotcha which applies to non-static classes:
Normally, the accessibility of a member is not greater than the
accessibility of the type that contains it. However, a public member
of an internal class might be accessible from outside the assembly if
the member implements interface methods or overrides virtual methods
that are defined in a public base class.
In relation to the last paragraph, since static classes cannot implement interfaces or inherit other classes then you can rest assured. As long you declare your static class internal, the members will not be available in other assemblies (unless your devs use reflection).
To exemplify how it does work for non-static classes:
Assembly 1
public interface ISomePublicInterface
{
int GetValue();
}
internal class InternalClass : ISomePublicInterface
{
public int GetValue()
{
return 100;
}
}
public static class SomeFactory
{
public static ISomePublicInterface GetInternalInstanceAsInterface()
{
return new InternalClass();
}
}
Assembly 2
ISomePublicInterface val = SomeFactory.GetInternalInstanceAsInterface();
Console.WriteLine(val.GetValue()); //-->> Calls public method in internal class
Console.WriteLine(val.GetType());
Guess what the output is?
Assembly1.InternalClass
So, now you have access to the type outside of the assembly and via reflection someone could call other internal methods (it's not the only way to get it).
From MSDN only
The access level for class members and struct members, including
nested classes and structs, is private by default.
interfaces default to internal access.
Hope this table helps:
Members of Default member accessibility
---------- ----------------------------
enum public
class private
interface public
struct private
Also check this MSDN
Private unless otherwise stated. However, public will have same result as internal.
If you later promote a class from internal to public, then creating public class objects will become visible, whilst internally scoped methods will stay internal.
You might want to consider behaviour in case your class scope gets updated.
Another stack overflow question.
If u Declare any class as "internal" then its means you can access this class in same assembly. But what kind of access specifier you use for class member is decide they are accessible are not in different class in same assembly.
All the members of internal class would be internal and will be accessible with in the same assembly and will not be outside neither class nor members.
If you want to access class in other assemblies, make the class public and member you don't want to be accessed outside assembly make them internal.

Object Oriented Programming Basic Concept(C#)

As We know
Default Modifier of
Class
Struct
Delegate
Interface
Enum
is Internal.
Enum & Interface members by default are public.
And Class, Struct , Delegate members by default are private.
Non-derived class of same class-library can have access to public and internal class
(and public, internal, protected internal-members).
Non-derived class of different class-library can have access to public class
(public members only).
Derived class of different class-library can have access to public class
(public, protected, protected-internal members).
Now I want to understand the core concept that why is so that...
Protected members are having more scope than internal?
From the docs:
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.
4) Non-derived class of different class-library can have access to public class (public, protected, protected-internal members).
This is not correct.
4) Non-derived class of different class-library can have access to public class (public members only).
And that means that the actual question is also debatable:
why is so that... Protected members are having more scope than internal?
protected and internal have different scopes. Which one is 'larger' is difficult to say. Comparing them in this way is simply not useful.
Protected simply means, that this member or method can not be overridden in a derieving class. Otherwise they behave like public members/methods.
Internal members are not declare for export, so thats why they can not been seen out side the module, the dll of them.
why is so that ... Protected members are having more scope than internal?
Because you can have a protected member accessible across assemblies but that's not true with internal.
internal Internal members are accessible only within files in the same assembly. its scope is limited to assembly only.
protected can be accessible outside assembly. A protected member is accessible from within the class in which it is declared, and from within any class derived from the class that declared this member. This derived class can be outside assembly.
hence it has more scope than internal modifier.

My C# Private Class is accessible anywhere inside the DLL, then whats the use of internal?

I have ClassLibrary project in C# and all my 'private classes' (under different namespace) are accessible to each other inside the same assembly (project).
Class1.cs
----------------------------
namespace MyClass1App
{
private class Class1{}
}
Class2.cs
----------------------------
namespace MyClass2App
{
private class Class2{}
}
Now Class1() can access and create instance of Class2() class [like... new MyClass2App.Class2() ]. and yes, these classes (Class1() and Class2()) are not accessible outside the assembly. Its the same behavior when these classes are made as 'Internal'. Can someone help me understanding whats the actual use/difference of 'private' and 'internal' access specifiers when applied on class level?
Thanks!
For normal classes you can only apply public and internal other access modifiers don't make sense.
Nested classes can have all access modifiers types.
You should not be able to declare a class as private at the namespace level. You can only have a private class if it is embedded within another class.
I get an error if I try to do this:
namespace MyApp
{
private class Class1
{
}
}
This is the error message:
Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal
Access Modifiers (C# Programming Guide)
Class or struct members can be
declared with one of five types of
access. They can be public or
internal, like the classes and structs
themselves. A class member can be
declared as protected using the
protected keyword, meaning that only
derived types using the class as a
base can access the member. By
combining the protected and internal
keywords, a class member can be marked
protected internal — only derived
types or types within the same
assembly can access that member.
Finally, a class or struct member can
be declared as private with the
private keyword, indicating that only
the class or struct declaring the
member is allowed access to that
member.
Duplicate Question:
Internal vs. Private Access Modifiers

What are the access-specifiers available in c#? What is the default one?

What are the access-specifiers available in c#? What is the default one?
The possible access modifiers are :
private
protected
internal
protected internal
public
The default modifier that is used, depends on the element.
For classes, the default specifier is
'internal'
For class-members, the default
specifier is private
For nested types (inner classes), the
default is private
You can also find it on MSDN
You might find it also interesting to have a look at this
A copy of the C# Language Specification is included when you install Visual Studio (in 2008 at least), at VSRoot\VC#\Specifications\1033\CSharp Language Specification.doc. This is, of course, the definitive place to look.
Access Modifiers (Access Specifiers) describes as the scope of accessibility of an Object and its members. All C# types and type members have an accessibility level . We can control the scope of the member object of a class using access specifiers. We are using access modifiers for providing security of our applications. When we specify the accessibility of a type or member we have to declare it by using any of the access modifiers provided by CSharp language.
C# provide five access specifiers , they are as follows :
public, private , protected , internal and protected internal .
public :
public is the most common access specifier in C# . It can be access from anywhere, that means there is no restriction on accessibility. The scope of the accessibility is inside class as well as outside. The type or member can be accessed by any other code in the same assembly or another assembly that references it.
private :
The scope of the accessibility is limited only inside the classes or struct in which they are declared. The private members cannot be accessed outside the class and it is the least permissive access level.
protected :
The scope of accessibility is limited within the class or struct and the class derived (Inherited )from this class.
internal :
The internal access modifiers can access within the program that contain its declarations and also access within the same assembly level but not from another assembly.
protected internal :
Protected internal is the same access levels of both protected and internal. It can access anywhere in the same assembly and in the same class also the classes inherited from the same class .

Categories