Published on: 07/19/20 11:07 PM
Category:C# Code Tags: C#, namespacesWhat is Namespaces in C#?
C# Programs are organized using namespaces. Namespaces are used to add separation of Code in C#.
Namespaces can have following members inside it.
- Namespaces (nested)
- Classes
- Interface
- Delegates
- Structures
Why to use Namespaces?
Code Separation:
With help of Namespaces, you can separate out set of code in C# Project.
Example: if you have Project name “Bank” and it is having different class libraries separated by logic written in that libraries by name Data, Business, FrontEnd, etc. Then you may add namespaces by name as Bank.Data, Bank.Business, Bank.FrontEnd respectively.
By this way, you can separate out your code in different namespaces which will be easier to manage your project.
Resolve Conflict:
Using namespaces, you can resolve name conflicts in your project.
Example: if you have some class by name “User”, you cannot create another class with same name. But using namespaces, it is possible. Let’s continue with above example, if you are working on project – “Bank”, you can create a class named “User” in Bank.Data Namespace and again inside Bank.Business Namespace too and so on.
How to create Namespaces?
Namespaces in C# can be create using keyword “namespace”. To create namespace, just add keyword “namespace” before your namespace-name .
Example:
namespace namespace-name{
//do something
}
Let’s go ahead and create a namespace
using System;
namespace Bank.Data
{
class User
{
public void GetUser()
{
//do something
}
}
}
In above example, we can see, we have created namespace by name “Bank.Data” and class inside it.
How to Use name namespaces
To access this method in another class, you can use fully qualified name for accessing it’s members as below.
class Program
{
static void Main(string[] args)
{
Bank.Data.User user = new Bank.Data.User();
user.GetUser();
}
}
In above example, you can see, we have used fully qualified name of as Bank.Data.User then we have created object of class User and then called its methods.
One also can use keyword “using” to call other namespace’s members to some other namespaces as below.
using System;
using Bank.Data;
namespace Learning.Nameapces
{
class Program
{
static void Main(string[] args)
{
User user = new User();
user.GetUser();
}
}
}
In above example, you can see we have added “using” keyword to add namespace “Bank.data” in our project. Once we added this namespace to our project, its member is readily available to add in our class.
Nested Namespaces
In C#, we can have namespaces inside namespaces, that is called as nested namespaces. To use its members we can use dot (.) operator.
How to created nested namespace? To create nested namespace, add namespace inside of namespaces as below.
We have added namespace by name “Database” inside our old namespace “Bank.Data”
using System;
namespace Bank.Data
{
namespace Database
{
class User
{
public void GetUser()
{
//do something
}
}
}
class User
{
public void GetUser()
{
//do something
}
}
}
In above example, you can see, we have created namespace “Bank.Data” and added namespace inside it named “Database” which having some other classes
How to call nested namespaces?
As we seen before, we have 2 ways to call namespaces- using fully qualified name, by “using” keyword.
We can use any of these method to call nested namespaces too.
By fully qualified name
class Program
{
static void Main(string[] args)
{
Bank.Data.User user = new Bank.Data.User();
user.GetUser();
}
}
By “Using” keyword
using System;
using Bank.Data.Database;
namespace Learning.Nameapces
{
class Program
{
static void Main(string[] args)
{
User user = new User();
user.GetUser();
}
}
}
great put up, very informative. I ponder why the other specialists of
this sector don’t understand this. You must continue your writing.
I am sure, you have a great readers’ base already!
Thank you for comment, this replies encourage us to write. Thank you.