Email: info@zenconix.com

Learn C# – Namespaces

Published on: 07/19/20 11:07 PM

Category:C# Code Tags: ,

What 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.

  1. Namespaces (nested)
  2. Classes
  3. Interface
  4. Delegates
  5. 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();
        }
    }
}

2 thoughts on "Learn C# – Namespaces"

  • Gclub says:

    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!

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

How Angular Application starts?

In this post we are going to take look on how angular application starts. For any angular application, entry point is index.html. In index.html file there is tag called as <app-root> In main.ts file <app-root> selector is picked up by the AppModule. Below is code snippet from main.ts file AppModule is module which is bootstrapped […]

Steps by step procedure to connect the database to Lumen

Create database in phpmyadmin Open .env file. NOTE: if .env file is with name, .env.sample, then rename it to .env Change following parameter in file DB_DATABASE=lumen DB_USERNAME=root DB_PASSWORD= Also check for following parameters if they are different than default setting DB_HOST=127.0.0.1 DB_PORT=3306 Next, as we are going to use Eloquent ORM, we need to un-comment […]

Laravel – How to create Seed users

Create migration file to create new users table To create migration, open command prompt and execute following command php artisan make:migration create_users_table This will create new file in database/migration folder by name datatime_create_users_table.php Open file and modify as below. <?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Hash; class CreateUsersTable extends Migration { /** * […]