For this article, I would like to thank Vidya Vrat Agarwal sir for bringing to light this very small, but very important concept in his recent webinar on Object Oriented Programming. There were so many concepts to be learned from that seminar and this is one of them. So I decided to share with all of you.
Internal access modifier in C#
An internal modifier allows access to the elements of an assembly, within the same assembly only. This means if we have one class library type project with two classes, Class1 and Class2, any internal classes in then it will be accessible only within this class library project. When we add a reference of it's DLL to any other project, the internal classes of this library will not be accessible in that project.
By default, class is internal in nature.
So, using an internal access modifier is like telling our children not to talk with strangers. We tell our assembly not to share the internal classes with the stranger assemblies.
Access internal members outside the assembly
That was the basics of the internal modifier. But suppose we have created a product based library. Now we need to provide access to one of our internal member classes to some of our customers and not all of them. The very first thing that will occur to us is to make the class public. But this will expose the class to all the customers, that they do not even require. Moreover, it creates a security vulnerability. This is where friend assemblies are useful. We will simply tell our assembly or DLL that this class is your friend and you can share your internal members with it. To do this, we just need to add the following line of code to our namespace.
[assembly: InternalsVisibleToAttribute("Name_of_Assembly")]
And that's it. We are done. Build the assembly and we can now access the internal class members as well.
Adding this attribute instructs our assembly to share the details with stranger assemblies. It's like telling our children name of the people with whom they can talk.
Access internal members of the class outside the assembly: Example
For this, we will create a new project solution and add two project types, one class library named ClassLibrary and the other a console application named Consumer. In the class library, we add two classes, one internal named InternalClass and the other public named PublicClass.
Next, we build this project and add its reference to the Consumer project. Then we try to access these two classes in the console application. We will be able to access the public class but not the internal class.
Now in order to access the internal class in the console application, we add the InternalsVisibleTo attribute, with the name of the assembly in which we would like to access the internal members. In our case, it is named Consumer. See the code below.
Build the code and we can now easily access the internal members. In this way, we can add as many assembly names as we need.
So this is all we had to do to allow access to internal classes. We can add multiple attributes, with the names of the assemblies with which we would like to share these internal classes. But the condition here is that we need to add this attribute at the namespace level. I hope you enjoyed reading it!