Example # 01:
Before looking at the use of the internal access modifier, we have to look at the “private” access modifier in the C# programing first. We have been initiating our first illustration with the use of the “System” library. So, we have been using the user-defined namespace named “New” containing a total of two classes in it.
The class “Test” contains the main() function in it to start the execution of this code. The Console.WriteLine() statement in this main() driver function is used to display the text “Main method of test class” on our screens. An object “obj” for the class “New” has been created using the class name. The class “New”, contains a private data member “x” of integer type with the value “14”. This means that the value of variable “x” cannot be accessed by any other function of other classes even if they are derived.
The main() function is using the object “obj” of the New class to update the value of private data member “x” of the class “New” i.e. not possible right now. After calling the show() function with the object “obj” in the main() method, the execution of the show() method with the “public” access modifier must be taken place. It doesn’t seem like that because of the private access modifier with the “x” variable. Both the classes are completed and the code is ready for use. We have to save it first with Ctrl+S and exit the editor by closing it using the cross sign from the text editor.
After the code is saved, you have to compile it through the use of the “mcs” C# compiler already configured in Linux. This step is showing us the error which has occurred at line 12 of our C# code, i.e. variable “x” cannot be accessed within the Test class as it has been defined with the private access modifier. So, we need to fix this error.
We have opened the C# file again in the text editor and updated the code by replacing the private access modifier of variable “x” with the public access modifier. We will not be changing the remaining code as it is not required. So, save the newly updated code once again.
When we have used the mcs compiler command on the shell followed by the name of a C# file, the code got compiled successfully. After that, we have executed the “exe” file created by the compiler of C# on the shell and the output has been displayed i.e. a value of “x” got updated.
Example # 02:
Now, we will be taking a look at the use of the internal access modifiers to define the scope of some variables in the classes of C# code. So, we have started this C# example with the use of the System library and created a namespace “New” in it. This namespace contains two independent classes named “New” and “Test” in it. The class “Test” contains the main() driver code function, while the “New” class contains the integer variable “x” having a value of 14 defined with the internal access modifier and the show() function.
The New class object “obj” has been generated with the use of the “new” keyword followed by the name of a class. This object of the New class has been utilized at the next line to update the value of the “x” variable by “0”. Now, the variable “x” will be successfully updated as the variable “x” is defined with internal access modifier within the same namespace assembly “New”. The show() function has been called with this same object “obj”. When the show() function gets executed, it will display the updated value of “x” on the shell via the Console.WriteLine() function statement.
It’s time to quickly save our code file with Ctrl+S and come back to the terminal again. In the shell, we have been running the “mcs” command for the C# code compilation to run the “internal.cs” file. This compilation has been successful and the compiled “exe” file for the internal.cs has been created in our current working directory. We are using that “exe” file to run it with the “mono” runtime command in the shell. The output has been successfully displayed as below. The main() function got executed first as per the string displayed and after that, the updated value “0” of variable “x” is displayed successfully.
This is how an internal access modifier works in the C# code when it has been used within the same namespace. Let’s make changes to our code to see how an internal access modifier affects the execution of the C# code when used among more than one assembly. Let’s say you have created two namespaces in your same C# code i.e. New and Test.
Within the New namespace, we have created a class New and initialized a variable “x” of internal access modifier with the value 14. The very same class of namespace New contains the show() function to display the value of “x”. On the other hand, the namespace Test contains a class Test having a main() function. This main() function is creating an object of class New from the other namespace “New”. The same object has been used to modify the value of variable “x” and call the show() function to display the updated value of variable “x”. Now, the variable “x” is defined in the namespace “New”, while the object is trying to access it within the other namespace “Test”. Due to the internal access modifier with variable “x”, we may encounter an error. Let’s see that now.
After compiling this code, we have got the error as expected, i.e. type protected.
To avoid this error on compilation, we have to do some updates to the code as we did in the code above from the last code. So, we have removed the namespace “New” from the code and didn’t remove the Test namespace from the code as shown beneath.
After the code compilation and execution, it got executed successfully and displayed the modified value of variable “x” on the shell regardless of its internal type.
We are done implementing the examples for the internal access modifier in C#. We have started this article with the example of discussing the private access modifier to compare it with the internal access modifiers as both do the same job at different levels of programming. We have tried to use the internal access modifier within the same namespace, two different namespaces, and within the class having no namespace defined. This way, we have elaborated its use for the protection of data member variables and function in C#.