c sharp

C# Namespace

As the name suggests, the namespace holds data structures that are similar in nature and attributes. Namespaces in C# can be user-defined and built-in according to the situation. The namespaces are said to be a group of similar namespaces, data structures, classes, functions, and interfaces. This article will be discussing the methods to use and access namespaces and their respective data members in C# programs. Before going further, let’s have a look at the syntax of using namespace in a C# code. The namespace will be started with the keyword “namespace” followed by any user-defined title “name” for a namespace. Within this namespace, we can use more namespaces, classes, structures, interfaces, and functions.

Let’s start with the update of your system first. Try out the shown instruction in the image and your system will be up to date in a while.

Example 01:

Make sure to install and configure the “mcs” compiler and “mono” framework executor of C# on your end. After settings everything, it’s time to create a new C# file in which we will be adding our code. The Ubuntu 20.04 system is providing the “touch” instruction to create files through the terminal. We have created a C# file “namespace” with a “cs” extension at its end.

One can open it in the “text” editor of Ubuntu 20.04 as below. We will be starting our first example of C# with the use of the most-used standard built-in namespace “System” which occupies many read, write, and other functions as well. The word “using” is must when you define a built-in namespace in a C# code. We have created a user-defined namespace “New” using the keyword “namespace”. Within it, we have been creating a new user-defined class Test” and the main() function for this class. This main() function is using the WriteLine() function to display some message on screen, this function has been utilized here by the use of its “Console” class that is part of a “System” namespace. As there is a single main() function in the class “Test”, there is no need to create an object for this class. Just save this code right now.

Coming back to the shell terminal, we have used the “mcs” compiler of C# to compile the file “namespace.cs” and make an “exe” file for it. We will use the “mono” runtime command to execute the exe file i.e., namespace.exe. The program got executed and displayed a simple text.

This was all about using the built-in main() function solely in the user-defined New namespace. We will be updating our code to add a new user-defined “Show()” function in the code to create different results. As the main() function is the code controller and starter, it will be initializing a function call to the “Show()” function without using any class name. The Show() function will use the same WriteLine() function of the Console class for the “System” namespace to display some text messages on the screen. Let’s save and execute this code.

Upon compiling, the main() function got executed first and then the “show()” function.

Example 02:

We have done with the use of a single namespace in the program other than the System namespace. Now, we will be looking at the two different and separate namespaces. Both namespaces A and B contain their respective classes i.e., One and Two respectively. Namespace A contains the user-defined “Show()” function and namespace B contains the built-in main() driver method. Within namespace B, the main() function of class Two is calling the “show()” method of class One, namespace A. The function call is not using any object or namespace name to call the “show()” function which will lead us to an error.

The error occurred during the compilation of this file as expected.

It’s time to update our code. As the “show()” function is from another class, we need to create the object of that class, or use the class name to call it along with the namespace name. So, we have been calling the “Show” function in the main() method of class Two, namespace B. We have been calling it with the namespace name “A” and class name “One” as shown.

While utilizing the namespace and class name in the function call to another method for the respective function, we have successfully removed the error as per the shown compilation. The main() method of Class Two and namespace B got executed first. After that, the show() function from class One of namespace A was executed.

Example 03:

Within this example, we will take a look at the program containing nested namespaces. So, we have created nested namespaces A and B containing the same class “One” and function “Show()”. The namespace Test has a class Two and the same main() function. Now, when you want to call some function from the class of nested namespaces, you need to use the name of both namespaces in the function call in sequence or alphabetical order. So, we have used the namespace A and B along with their class name “One” to execute the “Show” function.

On execution, the main() method of the Test class got executed first. It calls the function “Show” from class One of nested namespace A and B and the Show() method got executed.

Example 04:

Within this illustration, we will be demonstrating the use of namespaces having the same name classes and functions. Thus, we have been using 3 namespaces in the C# program i.e., A, B, and C. The namespace A and B contains the same name class Test, and the same name function in the class i.e., Show(). The namespace C contains a main() driver function that is calling both functions from A and B namespaces separately with the use of their respective namespace and class title.

The output shows that the main() function has executed the function “Show()” of namespace A first and then the function Show() of namespace B.

The very same output can be achieved by creating objects for both the same name classes “Test”, of namespace A and B. These objects have been used to call the Show() functions directly without using their respective class name or namespace name.

You can take a look that the output is the same.

Within the below-updated program, we have been defining 2 namespaces A, and B with the same class name “Test” and the same name functions “Show”. Class C is using the main() function to display some text using the WriteLine() function and making a call to the “Show” method of class Test and namespace A. Within namespace “A”, we have been calling the same name function Show of same name class Test and different namespace B.

On execution, the main() method runs the function Show() of namespace A. After that, the show() function calls the function show() of the namespace B using the function call.

Conclusion:

Within the introductory paragraph, we have defined the namespace quite simply and discussed its syntax as well. To make it understand easily, we have used different illustrations for C# programs. Starting from the use of a single namespace with a single function and more than 1 function, we have elaborated the use of more than 1 namespace, and nested namespaces in C#. We have discussed the use of objects, namespace names, and function names to perform function calls for within and another namespace.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.