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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-1225.png" data-lazy- height="107" src="data:image/svg xml,” width=”483″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-1226.png" data-lazy- height="37" src="data:image/svg xml,” width=”543″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-1227.png" data-lazy- height="18" src="data:image/svg xml,” width=”418″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-1228.png" data-lazy- height="110" src="data:image/svg xml,” width=”614″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-1230.png" data-lazy- height="129" src="data:image/svg xml,” width=”559″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-1231.png" data-lazy- height="166" src="data:image/svg xml,” width=”629″>

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

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-1233.png" data-lazy- height="73" src="data:image/svg xml,” width=”421″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-1235.png" data-lazy- height="218" src="data:image/svg xml,” width=”720″>

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

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-1237.png" data-lazy- height="186" src="data:image/svg xml,” width=”555″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-1239.png" data-lazy- height="213" src="data:image/svg xml,” width=”717″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-1240.png" data-lazy- height="71" src="data:image/svg xml,” width=”398″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-1241.png" data-lazy- height="233" src="data:image/svg xml,” width=”713″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-1242.png" data-lazy- height="74" src="data:image/svg xml,” width=”408″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-1243.png" data-lazy- height="305" src="data:image/svg xml,” width=”714″>

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

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-1244.png" data-lazy- height="92" src="data:image/svg xml,” width=”441″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-1245.png" data-lazy- height="343" src="data:image/svg xml,” width=”714″>

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

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-1246.png" data-lazy- height="89" src="data:image/svg xml,” width=”401″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-1247.png" data-lazy- height="344" src="data:image/svg xml,” width=”708″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-1248.png" data-lazy- height="125" src="data:image/svg xml,” width=”454″>

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

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/Author-Image-150×150.jpg6265c66c358c5.jpg" height="112" src="data:image/svg xml,” width=”112″>

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.