14

I just want to know what is the actual difference between private and protected internal access specifier. As i know

Visible to own class members: private and protected internal YES

Visible to object of other classes: Both NO

Visible to objects of other classes outside the namespace collection: Both NO

Visible to object of child classes outside the namespace collection: Both NO

If private doing the same as protected internal then why we need the both just one should be enough or not?

5

7 Answers 7

27
  • 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 of protected and internal.
  • 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.

2
  • So when should I determine to use protected internal instead of public?
    – avirk
    Commented Jun 9, 2012 at 10:55
  • @François Your last sentence isn't actually true; protected internal is the union of the two, not the intersection. Commented Feb 22, 2014 at 22:31
26

A graphical overview (summary in a nutshell)

Visibility

2
  • There should be another column called Derived Classes in another assembly - for the protected internal keyword. Commented May 22, 2015 at 10:48
  • @Lyubomir Velchev: True, there should be two rubrices anyway (same assembly, different assembly), and in this graphic the internal "derived class" no-value should be hatched. Commented Jun 11, 2015 at 10:10
4

private is only visible to own class members, whereas protected internal is visible to child classes as well as to other classes inside the namespace collection.

2
  • So private is visible to child classes as well as to other classes inside the namespace collection.
    – avirk
    Commented Jun 9, 2012 at 10:54
  • @avirk No, that's protected internal. private is the strictest access modifier: only available to the defining class. Commented Jun 9, 2012 at 10:56
3

private

The type or member can only be accessed by code in the same class or struct.

protected internal

The type or member can be accessed by any code in the same assembly, or by any derived class in another assembly

1

I was trying to understand the differences between protected internal and internal in .NET context by reading the description provided different forums and blogs. I really failed to understand then I created created 2 separate assembly using VS2015. Probably now I have basic understanding. I would like to share with you, it might be helpful to somebody. I tried to use the fields declared in one assembly from another assembly. Also I tried derived from a class that was declared in another assembly. Here is the code for class1.cs from assembly 1

namespace Z_Dll_1
{
    public class PublicBaseClassAssemblyOne
    {
        internal int _myinternal = 200;
        protected internal int _protectedinternal = 100;
        protected int _myProtected = 123;
        private int _myPrivate = 2;
        public int _myPublic = 45;
    }

    public class DerivedClassAssemblyOne : PublicBaseClassAssemblyOne
    {
        protected internal int intM = 10;
    }

    internal class MyInternalClass
    {
        public void MyMethod()
        {
            Console.WriteLine("Method one with internal class");
            PublicBaseClassAssemblyOne cl1 = new PublicBaseClassAssemblyOne();
            cl1._myinternal = 1000; //Internal type is available since it is in same assembly
            cl1._protectedinternal = 10; // protected internal is available
            cl1._myPublic = 2;  // Public OK
            //cl1.myPrivate = ?? // nor available since it is private

            DerivedClassAssemblyOne drOne = new DerivedClassAssemblyOne();
            drOne._myinternal = 30; // Internal and available from derived class
            drOne._myPublic = 1; // Public 
            drOne._protectedinternal = 2; // Able to be accessed from same assembly or derived class from other assembly
        }
    }
}

Here is code from another assembly, class2.cs using Z_Dll_1;

namespace Z_Dll_2
{
    public class ClassAssembly2
    {
        public ClassAssembly2()
        {
            PublicBaseClassAssemblyOne classfromOtherAssembly = new PublicBaseClassAssemblyOne();
            classfromOtherAssembly._myPublic = 0; //Only public is available
        }
    }

    public class ClassDerivedFromOtherAssemblyClass : PublicBaseClassAssemblyOne
    {
        public ClassDerivedFromOtherAssemblyClass()
        {
        }
        void ClassDerivedFromOtherAssemblyClassTestMethod()
        {
            //_myinternal = 200; // can't access since it was internal to other assembly
            _protectedinternal = 100; // this can be accessed as it is  derived class from other class that has protected internal 
            _myProtected = 123; // Ordinary protected data accessed from derived class
            //_myPrivate = 2; //Private member can't be accessed from  derived class
            _myPublic = 45; // Public can be accessed anyway

            //Try to create an instance of internal class
            //MyInternalClass intClass = new MyInternalClass(); //Not accessible from this assembly
        }
    }
}
0

I think that protected internal means that only classes that inherit and are in the same assembly can see that property. Those that derive the class and are from different assembly, cannot see it.

LE: read Mattias Buelens comment for this.

3
  • Actually, this is incorrect. protected internal means protected union internal, so both child classes and other classes in the same assembly can see it. Commented Jun 9, 2012 at 10:49
  • @Mattias Buelens...My english was a bit bad, but what I wanted to say is now written. Commented Jun 9, 2012 at 10:50
  • Yeah, it's a bit puzzling at first but it makes sense when you see that it's the union of both modifiers. Commented Jun 9, 2012 at 10:52
0

Speaking practically, I typically only use private for variables to ensure they can't be misused by other classes.

Protected internal, however, I will often use for methods that I don't want most other classes to be able to use, but that I want to be able to access to write test cases. It is very handy in that it allows test classes to be created in the sane namespace or package structure that can then access those protected internal methods without inappropriately opening them up to the rest of the world.

This approach does require an approach to coding where writing easily "testable" code is a priority. If this wasn't my approach, I'm not sure I would find many occasions to use protected internal.

2
  • 1
    The choice of access modifier should not be determined by your need for testing. If for example you have a property which does not require a setter you would not add a public setter simply to allow it to be set for testing. Test frameworks along with mocking frameworks are able to inject into private properties and so on. Testing influences your choice in patterns or possibly even abstraction, after all, it is hard to unit test something if you can't mock any dependencies. Testing should not impact your choice of access modifiers though.
    – Nope
    Commented Jun 9, 2012 at 11:55
  • +1 Francois, I also often see people making private members 'internal', together with the InternalsVisibleToAttribute to expose them to a unit test assembly.
    – MattDavey
    Commented Feb 22, 2014 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.