14

Possible Duplicate:
What is the difference between 'protected' and 'protected internal'?
What is the difference between Public, Private, Protected, and Nothing?

Code is as mentioned below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace testanotherlib
{
    public class A
    {
        internal void InternalDisplay()
        {
            Console.WriteLine("Internal Display Method.");
        }

        protected void ProtectedDisplay()
        {
            Console.WriteLine("Protected Display Method.");
        }

        protected internal void ProtectedInternalDisplay()
        {
            Console.WriteLine("ProtectedInternal Display Method.");
        }

        public void PublicDisplay()
        {
            Console.WriteLine("Public Display Method.");
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace testanotherlib
{
    public class B : A
    {
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using testanotherlib;
namespace testlib
{
    public class C:A
    {
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using testlib;
using testanotherlib;

namespace testapp
{
    class Program
    {
        static void Main(string[] args)
        {
            B objB = new B();
            C objC = new C();
        }
    }
}

I am trying to understand the difference between Internal, Protected and Protected Internal. For that I have created an example using the code above.

In a class library project testanotherlib I have class A & class B. In a class library project testlib I have class C. The program class is in a separate console application. Inside the main method of Program class I have created object for class B (objB) and class C (objC). For objB and and objC only the public method of class A are accessible. I was expected for class B all the methods of class A will be accessible. Kindly help me to understand this. If you need any other information about the project, feel free to ask me.

Regards, Priyank

Community
  • 1
  • 1
Priyank Thakkar
  • 4,752
  • 19
  • 57
  • 93
  • Where were you expecting to be able to access all the methods of class A, with a reference to a class A? Your code never tries to *use* the members, which makes it hard to talk about... – Jon Skeet Mar 10 '12 at 14:11
  • @JonSkeet: I was expecting to be able to access all the methods if class A, with reference objB. – Priyank Thakkar Mar 10 '12 at 14:14
  • 1
    @PriyankThakkar: From `testApp`? *Why* were you expecting that? The code in `testApp` isn't in the same assembly as `A` so any internal members aren't visible, for example. – Jon Skeet Mar 10 '12 at 14:16
  • Why doesn't Microsoft provide a topic on PROTECTED INTERNAL in MSDN? – ashveli May 04 '17 at 16:25

3 Answers3

15

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.

Chris Gessler
  • 22,727
  • 7
  • 57
  • 83
5

internal

Only visible in the current and friendly assemblies.

protected

Only visible within classes that inherit A.

protected internal

Visible within classes that inherit A. And also visible within the current and friendly assemblies.

Terkel
  • 1,575
  • 8
  • 9
  • protected internal means protected OR internal; you described it as protected AND internal. – Tuan Jul 07 '15 at 18:47
4

protected methods and members can only be accessed from another Class that derives from the class declaring the procted method.

class A 
{
    protected void Method() {}
}

class B : A
{
    public void Foo()
    {
        Method(); // works!
    }
}

class C 
{
    public void Foo()
    {
        Method(); // won't work, obviously

        var tmp = new A();
        tmp.Method(); // won't work either because its protected
    }
}

internal makes the method only visible in the same assembly. For classes in the same assembly the method can be used like it were public. for classes outside of your current assebmly its like private.

Now combining protected and internal makes a method usable in the same assembly for all classes in that assembly. And the protected makes the method usable in all derived classes no matter which assembly.

dowhilefor
  • 10,971
  • 3
  • 28
  • 45
  • You got protected internal wrong, see my answer or Chris' for that matter. – Terkel Mar 10 '12 at 14:15
  • @SimonBangTerkildsen Oh you are right. i corrected my answer. Didn't knew that. Well, never used it myself. So internal effectivly overrules the protected keyword. And the internal is ignored when derived. Didn't knew that, and in fact didn't like it :) But there must be a good reason for this. – dowhilefor Mar 10 '12 at 15:13
  • @dowhilefor: thanks for explaining it with wonderful example :) :) – Priyank Thakkar Mar 11 '12 at 12:01