How to make use of Namespaces in the .Net Framework?

How to make use of Namespaces in the .Net Framework?

In computer programming, namespaces are logical containers of several similar blocks of codes. These blocks of codes each carry out a specific task, and several number of these blocks which are functionally related to each other are grouped together as one namespace. Namespaces are basically meant to be used to organize your code better.

Introduction to Namespaces




.Net Framework including its various languages and Visual Studio comes with a great Class Library. A Class Library is simply a library of code used in a specific programming framework or language.

Microsoft developers have developed thousands of lines of codes and managed them into different classes, methods, namespaces and etc. These classes, methods, and namespaces are built-in and can be referenced and used in your code anytime and anywhere.

So, what are namespaces? To better understand the concept of namespaces let’s consider the following illustration:

Inside a society, there are several families. Each family has several members and each member has his/ her name. For example; when I say “Bill is a generous man.” I may not be very much clear about which “Bill” I am talking. In fact, there might be ten person named “Bill” in a small society. But when I say “Bill Gates is a generous man.” I become very much specific about which “Bill” I am talking.

Namespace illustration 1

Likewise; the codes written in programming languages such as all the .Net framework, Java, PHP and etc. are grouped into methods, classes, subs, procedures, functions, namespaces and etc. Several lines of code that perform a specific task is called a method, sub, function, or procedure. Several methods, subs, functions, or procedures that carry out functionally similar task in a program is grouped as a class or family. Several classes or families that are related to each other are grouped together as a namespace. So namespace is just as a last name to a person while his/ her first name is the name of class or family.

Namespace illustration

Coding Illustration of Namespaces

In the code below;

WriteLine() & ReadLine() are both methods. Console is the name of the class which contains the two methods in this code and several other methods. Finally System is the namespace which contains the class Console in this code and several other classes in the .Net framework.

The methods, classes and namespaces discussed in this example are built-in. You can simply access them in your code and make use of them.


Accessing/ Referencing Namespaces

Read: Validation Controls in ASP.Net

By default, when you open an IDE such as the Visual Studio and choose a template for your project like, “Console Application”, “Windows Forms Application”, “Class Library” and etc. you automatically add some namespaces to your code. These namespaces might not be visible in your code, but you can check them by going to Solution Explorer Window => right clicking on the name of your project => going to properties. There under the References Tab you can find the imported namespaces for your project.

Namespaces 1

 

Namespaces 2

Some of the default namespaces imported for a Console Application are as following;

Microsoft.VisualBasic
System
System.Collections
System.Data

When you want to access the classes or methods of a relative namespace, that namespace has to be declared (imported)  in your code. By default when you create a template, some namespaces are declared. For the namespaces that are not declared by default you can use the keyword “Imports” and declare the namespace that you want. Check the code below; it generates error because the code corresponding to the class StreamReader() is not declared in the project.

Hence; at the top of my project, I will declare the namespace “System.IO” which contains the class StreamReader() and code will run just fine.

Nested Namespaces




Just like the nested if conditions, you can make nested namespaces. i.e, namespaces can further contain child namespaces and the child namespace(s) can contain classes and further methods.

One of the biggest namespaces used in the .Net framework is the “System” namespace. System namespace itself has tens of classes that can be used to form your code. However; system namespace also contains several other child namespaces such as “System.Data”, “System.Deployment”, “System.Drawing”, “System.IO”, …

While you refer to the system namespace in your code, it only includes that specific namespace’s classes. If you want to make reference to the classes included in one of the child namespaces, you must import that child namespace into your code specifically.

Related: Working with Multiple Forms in Visual Basic.Net

User-defined namespace(s)

First and foremost, it is important to know that when you define your own namespace in a project, the system itself builds a root namespace for your project which will be identical to the name of your project. You can change this name in Your Project Properties Window => Under the References.

namespaces 3

The general syntax for building a namespace is:

Example;

Note: One namespace can have unlimited number of classes. But all the classes inside a namespace should have its unique and identical name. However; two classes with the same name can exist in two different namespaces. But when you make references to those two classes, their corresponding namespaces should also be mentioned using a (.) operator. Finally, all the namespaces should have unique name of their own. No two namespaces can carry the same name.

All the user-defined namespace should be referenced at the top of your project including the root namespace using the Imports keyword.

User-Defined Namespace(s) full project source code

Output:

namespaces 4

Conclusion



  • Namespaces are logical containers of code blocks in programming languages.
  • Namespace(s) are basically used to organize the code and eliminate the name collision of classes, methods and procedures.
  • Namespace(s) can be built-in or user-defined.
  • Each namespace has its own unique name and can have further child namespace(s).
  • The namespace(s) which are not declared in the project be default, should be declared at the top of the project using the Imports
  • Each namespace can contain several classes, but no two classes can have the same name.
  • Two classes in two different namespace(s) can have the same name, but when those classes are referred in the code, their namespace(s) should also be included using the (.) operator.

Feel free to post your feedbacks!

Previous How do Validation Controls in ASP.Net work?
Next MySQL Database Storage Engines

About author

Bahonar Javid
Bahonar Javid 43 posts

<p>* Computer Application Student<br /> * Co-Founder of www.codejow.com<br /> * Love to code and Learn new languages<br /> * Enjoy playing football</p>

View all posts by this author →

You might also like