Namespaces and Using directive in C#

Goud.Kv
Posted by in C# category on for Beginner level | Points: 250 | Views : 4616 red flag
Rating: 4 out of 5  
 1 vote(s)

C# is an Object-oriented programming language. C# comes with simplicity, expressiveness and great performance to meet the programmer productivity.
Recommendation
Read Statements and types of statements in C# before this article.

Introduction

So far, we have seen statements in C#. Now let's discuss with namespace and using directive of C# in this chapter.

Objective

The main objective of this article is to learn namespaces with scoping and hiding and also using directive in C# programming.

Namespace

Namespace is a keyword used to declare a set of related objects. It is a domain for the type names. It is used to organize all the elements to avoid conflicts which creates unique types.

Example:
Let's take RSA type (It is a class of Cryptography) that handles the public key encryption.
System.Security.Cryptography.RSA
and the namespace of RSA is,
System.Security.Cryptography
  • Namespaces are independent of assemblies (units of deployment such as .dll or .exe).
  • They do not have any impact on the member visibility such as public, private etc.
  • It defines the namespace for all the types with in that block. For example,
    namespace Country.State.City // Dots indicates the nested namespaces
    {
        class Bangalore { }
        class Chennai { }
        class Hyderabad { }
    }
    Now, the above code is exactly similar to the below one,
    namespace Country
    {
        namespace State
        {
            namespace City
            {
                class Bangalore { }
                class Chennai { }
                class Hyderabad { }
            }
        }  
    }
    We can call particular type from that namespace like below,
    Country.State.City.Hyderabad
  • Some of the types which are not defined in any namespace will comes under global namespace. It also contains the high-level namespaces.
Name Scoping
Types declared in the outer namespace can be used in the inner namespace. This is known as Name scoping.
Example:
Let's recall our example as,
namespace Country
{
    namespace State  
    // Outer namespace
    {
        class Andhrapradesh { }
        class Karnataka { }
        namespace City   
        // Inner namespace
        {
            class Bangalore : Karnataka { }
            class Hyderabad : Andhrapradesh { }
            class Chennai { }
        }
    }  
}
In the above code, we are using the Name (Type) of outer namespace (State) in the inner namespace (City).

We can also refer the names (types) in a different block of our structure i.e,
namespace Country
{
    namespace State  
    // Outer namespace
    {
        class Andhrapradesh { }
        class Karnataka { }
        namespace City   
        // Inner namespace
        {
            class Bangalore : State.Karnataka { }      // Refering the type in different branch
            class Hyderabad : State.Andhrapradesh { }  // Refering the type in different branch
            class Chennai { }
        }
    }  
}

Name Hiding
Name hiding comes into the picture when the type for both inner and outer namespace are with the same name.
Example:
namespace Country
{
    namespace State  
    // Outer namespace
    {
        class Delhi { }
        namespace City   
        // Inner namespace
        {
            class Delhi { }

            class Population
            {
                Delhi p1;        // State.City.Delhi (inner namespace got high priority) 
                State.Delhi p2;  // State.Delhi
            }
        }
    }  
}
In the above example, inner (City) namespace gets the higher priority and calls its type by default. We have to refer the outer namespace (State) to call the type of that namespace.

Repeated Namespace
We can also repeat a namespace until the types in them doesn't get conflict.
Example:
namespace Country.State.City
{
    class Bangalore { }
}
namespace Country.State.City
{
    class Hyderabad { }
}
In the above code, we are using namespace repeatedly. And also we can divide the above example as two source files such as,
City 1:
namespace Country.State.City
{
    class Bangalore { }
}
City 2:
namespace Country.State.City
{
    class Hyderabad { }
}

Using directive

It imports a namespace and allows us to refer all of the types in that namespace without declaring them.
Example:
Let's take our above example,
using Country.State.City

class Population
{
    static void Main()
    {
        Hyderabad p1; // No need of all types 
    }
}
In the above example, we are importing the namespace with the help of using directive in which it imports all the classes (types) in that directory. 

Note: There might be same type names in different namespaces. For example, In the .NET Framework, we have the Textbox class in both System.Windows.Controls (WPF) and System.Web.UI.WebControls (ASP.NET).

Conclusion

In this article, we have looked briefly about namespaces and using directive of C#. Hope you understand.

Thanks for reading.

Regards,
Krishna.

Recommendation
Read Class, Object and Class Inheritance in C# after this article.
Page copy protected against web site content infringement by Copyscape

About the Author

Goud.Kv
Full Name: Krishna Vamshi Goud
Member Level: Gold
Member Status: Member,MVP
Member Since: 2/12/2014 2:34:09 AM
Country: India
Thanks & Regards, Krishna


Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)