Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Part 50 - C# Tutorial - Internal and Protected Internal Access Modifiers

In this part we will understand internal and protected internal access modifiers. If you want to learn about private, public and protected access modifiers, please watch this video

Internal:A member with internal access modifier is available any where with in the containing assembly. It's a compile time error to access, an internal member from outside the containing assembly.

To understand inernal access modifier, we need 2 assemblies. To generate, the 2 assemblies we need follow these steps.
1. Open Solution Explorer (From the Viiew menu, select Solution Explorer)
2. From the Solution Explorer, right click on the project and select Add -> New Project
3. In the Add New Project Dialog Box, Select Visual C# from the Installed Templates section, and select Class Library from the center pane, and then enter AssemblyOne for the Name of the project and click OK.
4. Follow steps 2 & 3, to create AssemblyTwo project.
5. If you have followed these steps correctly, you should now see three projects in the soultion explorer.


Part 50 - C# Tutorial - Internal and Protected Internal Access Modifiers



Now, if we build the solution we should have 3 assemblies generated. Two dll's and one exe. To locate the physical assembly follow these steps.
1. Right click on AssemblyOne project, in solution explorer and select Open Folder in Windows Explorer.
2. Open bin folder
3. Now open Debug folder
4. In the Debug folder you should see AssemblyOne.dll, which is the physical assembly.


Copy and paste the following code in class1.cs file of AssemblyOne project
using System;
namespace AssemblyOne
{
    public class AssemblyOneClassI
    {
        internal int ID = 999;
    }

    public class AssemblyOneClassII
    {
        public void Test()
        {
            AssemblyOneClassI instance = new AssemblyOneClassI();
            // Can access inetrnal member ID, AssemblyOneClassII and AssemblyOneClassI
            // are present in the same assembly
           
            Console.WriteLine(instance.ID);
        }
    }
}

In this example, AssemblyOneClassI has an internal member ID. We can access ID member from AssemblyOneClassII, because this class is also present in the same assembly as AssemblyOneClassI.

Now, Copy and Paste the following code, in Class1.cs of AssemblyTwo project.using System;
using AssemblyOne;
namespace AssemblyTwo
{
    public class AssemblyTwoClassI
    {
        public void Test()
        {
            AssemblyOneClassI instance = new AssemblyOneClassI();
            //Console.WriteLine(instance.ID);       

        }
    }
}

Note: You will get 3 compiler errors at this point. To solve this we need to add an assembly reference. Follow these steps.
1. Expand References folder under AssemblyTwo project, from Solution Explorer.
2. Right Click on References folder and select Add Reference
3. From the Add Reference dialog box, select Projects tab
4. From the list, select AssemblyOne project and click OK.

At, this point all the compiler errors should have gone.

Uncomment the following line from Class1.cs file from AssemblyTwo project and rebuild the solution.
Console.WriteLine(instance.ID);

Now, you will get a compiler error stating 'AssemblyOne.AssemblyOneClassI' does not contain a definition for 'ID' and no extension method 'ID' accepting a first argument of type 'AssemblyOne.AssemblyOneClassI' could be found (are you missing a using directive or an assembly reference?).

This is because, AssemblyTwoClassI is not present in AssemblyOne assembly and hence cannot access the internal ID member defined in AssemblyOne assembly. This proves that internal members are only accessible with in the same assembly. Code outside of the containing assembly cannot access internal members.


Protected Internal:Protected Internal members can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly. It is a combination of protected and internal. If you have understood protected and internal, this should be very easy to follow.


Now, change the access modifier from internal to protected internal for ID member in AssemblyOneClassI of class1.cs file in AssemblyOne project.
internal int ID = 999; to protected internal int ID = 999;


Finally modify the code in Class1.cs file in AssemblyTwo project as shown below.using System;
using AssemblyOne;
namespace AssemblyTwo
{
    // Make AssemblyTwoClassI inherit from AssemblyOneClassI   
    public class AssemblyTwoClassI : AssemblyOneClassI
    {
        public void Test()
        {
            AssemblyOneClassI instance = new AssemblyOneClassI();
            // Access the base class member using the base keyword           
            Console.WriteLine(base.ID);
        }
    }
}

So, this shows protected internal ID member, defined in AssemblyOne is accessible in AssemblyTwo.

2 comments:

  1. Hi there,

    A few comments:

    Might want to indicate in the text that each
    class file is in a different Class Library project and thus in a different assembly.

    The code below instantiates the inherited base class unnecessarily.

    namespace AssemblyTwo
    {
    // Make AssemblyTwoClassI inherit from AssemblyOneClassI
    public class AssemblyTwoClassI : AssemblyOneClassI
    {
    public void Test()
    {

    AssemblyOneClassI instance = new AssemblyOneClassI(); // W H Y

    // Access the base class member using the base keyword
    Console.WriteLine(base.ID);
    }
    }
    }

    ReplyDelete
  2. Hi Venkat,

    I created one console application which contains employee class in that created two variables which is of protected and protected internal string varaiable

    using System;

    namespace ConsoleApplication2
    {
    public class Employee
    {
    protected internal string e_name;
    protected string e_designation;
    }

    class Program
    {
    static void Main(string[] args)
    {

    }
    }
    }

    I created one class library as a assembly in that I created derived class which inherits from Employee class.

    I created a object of derived class and try to access the variable from base(employee) class.

    using System;
    using ConsoleApplication2;

    namespace LibraryAssembly3
    {
    public class Class1:Employee
    {
    public void toPrint()
    {
    Class1 c = new Class1();
    c.e_designation; // protected acess modifer
    c.e_name;// protected internal access modifier
    }
    }
    }

    from the above example I'm able to access to variable which is of modifier protected in the different which should not happen(which should not able to access) so can you please whether this expected result or not.

    can you please let me the difference between protected and protected internal.

    Thanks in advance

    ReplyDelete

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.