Introduction to Namespaces in C#,Namespaces in C#,Namespaces C#, c# Namespaces
Hello friends, I hope you all are doing great. In today's tutorial, we are gonna have a look at detailed Introduction to Namespaces in C#. Namespaces are considered as libraries in C#. If you have worked on any other programming language then you must be aware of the term library which contains all classes & methods etc. In C#, this functionality is performed by namespaces. In 12th tutorial, we have seen Introduction to Methods and in 13th part, we have seen Classes in C#. Methods are simple storage units and stores code only, Classes are slightly bigger storage units and can store methods. Now Namespaces are giant storage units, which can save anything in them i.e. classes, methods, namespaces etc. So, let's have a look at them in detail:

Introduction to Namespaces in C#

  • Namespaces are giant code storage units, can be referred as libraries in C#, and are used for optimization & orientation of the code.
  • Namespaces are included in the project with the help of using directive at the top.
  • If you look at our previous codes, then you will find using Systems; at the top of your code, basically this Systems is a namespace and with the help of using directive, we have included it in our project.
  • Console, which we use for printing our data, is a member of this System Namespace.
  • Our whole project is also placed between { } brackets of namespace TEPProject.
Why we need namespaces ?
  • Using Namespace, we can organize the code pretty well, it doesn't have much impact in simple projects but in complex projects, you can't ignore namespaces.
  • Throughout our course, we have discussed classroom data and in C# Classes lecture, I have asked you to get data of all classes of a school and now in namespace case, think of data coming from all schools of British School System.
  • So, in bigger projects, there's always a need to make different teams, which will be working on separate code departments.
  • In such cases, each team can create project with its own namespace and at the end you can use all those namespaces in your Main code and can get access to its functions etc. without disturbing each other's code.
Creating Namespaces in C#
  • So, now let's create two namespaces in our project for two different schools, named as SchoolA & SchoolB, as shown in below figure:
Introduction to Namespaces in C#,Namespaces in C#,Namespaces C#, c# Namespaces
  • In above figure, you can see our first namespace structure is: Namespace SchoolA > Class TeamA > Method printSchoolName.
  • Our second namespace structure is: Namespace SchoolB > Class TeamB > Method printSchoolName.
  • Now in my Main function, which is in TEPProject Namespace, I am calling both of these printSchoolName Method.
  • In order to invoke the method in first namespace, I have used dot operator and the sequence is SchoolA.TeamA.printSchoolName();
  • For the second namespace, I have placed using SchoolB; at the top of the code and now we can call TeamB class directly and that's why I have used TeamB.printSchoolName(); to invoke method in second namespace.
  • So, we can use namespaces using these two ways and I prefer the second one as it makes the code smooth, we don't need to write SchoolB every time.
Create Project for Namespaces in C#
  • Now you got the idea of what are namespaces and how to use them in C#.
  • So now it's time to create separate projects for these two namespaces and you will see our code will become clear & simple.
  • Right click on your Project's Name in Solution Explorer and then click on Add and then click on New Item, as shown in below figure:
Introduction to Namespaces in C#,Namespaces in C#,Namespaces C#, c# Namespaces
  • When you click on New Item, a new window will open and here you need to select C# class, as shown in below figure:
Introduction to Namespaces in C#,Namespaces in C#,Namespaces C#, c# Namespaces
  • I have given it a name SchoolA.cs and then click Add Button.
  • Similarly, I have created a new project for second namespace SchoolB.cs and now my Solution Explorer is shown in below figure:
Introduction to Namespaces in C#,Namespaces in C#,Namespaces C#, c# Namespaces
  • Both of my projects' codes and Main file code are shown in below figure:
Introduction to Namespaces in C#,Namespaces in C#,Namespaces C#, c# Namespaces
  • Now you can see in the above figure that our codes are now quite simple & clear and we have created separate files for our new namespaces.
  • C# Classes created in separated files and namespaces are now accessible in our Main Function.
So, that was all about Namespaces in C#, I hope you have understood the main idea. In our next tutorial, we will have a look at Inheritance in C#. Till then take care and have fun !!! :)