Linux

C# Internal

Access Modifiers are a must-have in any object-oriented programming. The access modifiers are used to protect your data by using them with the data members and member functions. These access modifiers are: public, private, protected, and internal as per the sources. The internal access modifier works the same as the private access member does but on different levels of programming. The private modifier works at the class level, while the internal modifier works at the assembly level. Today, we will be deliberating the use of internal access modifiers.

Example 01

Before using the internal access modifier, we first have to look at the “private” access modifier in C# programming. So, we have been using the user-defined namespace named “New,” containing two classes. The class “Test” contains the main() function 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 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 take 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 must compile it using the “mcs” C# compiler already configured in Linux. This must-have step shows us the error has occurred at line 12 of our C# code, i.e., variable “x” cannot be accessed within the Test class as 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, we saved 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 executed the “exe” file created by the compiler of C# on the shell, and the output was displayed, i.e., a value of “x” got updated.

Example 02

Now, we will be taking a look at the use of internal access modifiers to define the scope of some variables in the classes of C# code. So, we have initiated this C# example with the addition of the System library and created a namespace “New” in it. This namespace contains two independent classes named “New” and “Test.” 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 using the “new” keyword followed by the name of a class. This new class object 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 an 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 return to the terminal again. We have been running the “mcs” command for C# code compilation to run the “internal.cs” file in the shell. This compilation has been successful, and the compiled “exe” file for the internal.cs has been created in our current working directory. We use 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 gets 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 C# code when used within the same namespace. Let’s make changes to our code to see how an internal access modifier affects the execution of C# code when used among more than one assembly. 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 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 update 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 after this update, it got executed successfully and displayed the modified value of variable “x” on the shell regardless of its internal type.

Conclusion

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 no namespace defined within the class. We have elaborated its use to protect data member variables and function in C#.

Source link

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button