Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion: Internal, Protected and Protected Internal [duplicate]

Tags:

c#

oop

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

like image 253
Priyank Thakkar Avatar asked Mar 10 '12 14:03

Priyank Thakkar


2 Answers

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.

like image 180
Chris Gessler Avatar answered Oct 06 '22 00:10

Chris Gessler


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.

like image 30
Terkel Avatar answered Oct 05 '22 23:10

Terkel