C# Namespaces with Examples

  Keywords : what is the use of namespaces in c# with examples, Namespaces in c# are the collection of classes to use existing class methods

Here we will learn what is the namespace in c# with examples, c# access the namespaces with using keyword, c# define multiple namespaces and nested namespaces in c# with examples.

C# Namespaces

In c#, Namespace is used to organize the multiple classes in our applications and it will reduce the code redundancy in our .NET applications. By using namespace keyword, we can define the namespaces in our c# applications.

 

Following is the syntax of defining a namespace in c# programming language.

 

namespace Namespace_Name

{

 // Body of Namespace

}

If you observe above syntax, we defined a namespace in c# using namespace keyword .

 

Following is the example of defining the namespace in c# programming language.

 

namespace Tutlane

{

    class Welcome

    {

        public void GreetMessage()

        {

            // your method code

        }

    }

}

If you observe above code, we defined a namespace Tutlane and it consist a class Welcome as its member and GreetMessage() is a method of Welcome class.

 

By default .NET Framework provided a lot of namespaces to make the application implementation easily.

 

Following is the example of using .NET Framework namespace “System” in c# programming language.

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            System.Console.WriteLine("Welcome to Tutlane");

            System.Console.WriteLine("Press Any Key to Exit..");

            System.Console.ReadLine();

        }

    }

}

In above example, System is a namespace, Console is a class in that namespace, WriteLine and ReadLine are the methods of Console class.

 

When we execute the above c# program, it will return the result like as shown below.

 

C# Namespaces with .NET Framework System namespace Example Result

C# Access Namespace with using Keyword

If you observe above example, every time we used a System namespace to access the Console class methods instead, we can import the System namespace once in our application and use the associated classes and methods with using keyword.

 

In c#, using keyword is useful to import the namespaces and provide an access to the associated classes and methods to use it in our .NET applications.

 

Following is the example of importing System namespace with using keyword in c# programming language.

 

using System;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine("Welcome to Tutlane");

            Console.WriteLine("Press Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above example, we imported a “System” namespace with “using” keyword to access Console class and associated methods (WriteLine, ReadLine) directly without using the System namespace every time.

 

To know more details about above program, check this C# Hello World Program Example.

 

When we execute the above c# program, it will return the result like as shown below.

 

C# Access Namespaces with Using Keyword Example Result

 

Like System namespace, the .NET Framework is having a various namespaces, those are

 

NamespaceDescription
System It contains a classes that allows you to perform basic operations such as mathematical operation and data conversation.
System.IO It contains a classes to perform Input and Output operations.
System.Net It contains a classes that are useful to with network protocols.
System.Data It contains a classes that are useful to work with ADO.Net architecture.
System.Collection It contains a classes that are useful to implement the collection of objects such as lists.
System.Drawing It contains a classes that are useful to implement GUI functionalities.
System.Web It contains a classes that are helpful to perform HTTP requests.

In simple words, we can say that Namespace is a collection of classes and classes are the collection of objects and methods. So by using namespaces we can easily access all the class methods just by importing the namespace in our application.

C# Define a Multiple Namespaces

In c#, we can define and access multiple namespaces in our application with using keyword.

 

To access the custom namespace classes, we need to import the custom namespace with using keyword and need to create an instance for that classes in our application.

 

Following is the example of defining and accessing the multiple namespaces in c# programming language.

 

using System;

using CustomNameSpace;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            Welcome w = new Welcome();

            w.GreetMessage();

            Console.WriteLine("Press Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

 

namespace CustomNameSpace {

    class Welcome {

        public void GreetMessage() {

            Console.WriteLine("Welcome to Tutlane");

        }

    }

}

If you observe above example, we created a multiple namespaces and accessing the namespace CustomNamespace classes in Tutlane namespace by creating the instance for the class.

 

When we execute the above c# program, it will return the result like as shown below.

 

C# Define Multiple Namespaces Example Result

 

This is how we can create and access multiple namespaces in our c# applications.

Nested Namespaces in C#

In c#, a namespace can contain another namespace and it is called a Nested Namespace. The nested namespace classes and its members can be accessed by using dot (.) operator.

 

Following is the syntax of defining the nested namespace in c# programming language.

 

namespace namespace1 {

    namespace nestednamespace {

    // Nested Namespace Code

    }

}

If you above syntax, we defined a namespace “nestednamespace” within another namespace “namespace1”.

 

Following is the example of using nested namespace in c# programming language.

 

using System;

using CustomNameSpace.Nested;

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            Welcome w = new Welcome();

            w.GreetMessage();

            Console.WriteLine("Press Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

namespace CustomNameSpace {

    namespace Nested

    {

        class Welcome

        {

            public void GreetMessage()

            {

                Console.WriteLine("Welcome to Tutlane");

            }

        }

    }

}

If you observe above code, we imported a nested namespace with using keyword like using CustomNameSpace.Nested; and accessing the nested namespace classes by creating the instance.

 

When we execute the above c# program, it will return the result like as shown below.

 

C# Nested Namespaces Example Result

 

This is how we can use the nested namespaces in our c# applications based on our requirements.