Home » .Net

Nested Namespace in C#

In this article, we are going to learn about Nested namespace in C#, how to implement a C# program by using nested namespaces in C#?
Submitted by IncludeHelp, on August 11, 2018

Prerequisite: Namespace in C# and The 'using' Keyword in C#

Namespaces can also be nested in C#. It means we can define namespace inside another namespace.

Syntax:

namespace <namespace_name>
{
	namespace <nested_namespace_name>
	{
		//Write code her
	}
}

We can access nested namespace using . (Dot) Operator.

Example:

using System;
using System.Collections;

using namespace1;
using namespace1.namespace2;

namespace namespace1
{
    class ABC
    {
        public void fun()
        {
            Console.WriteLine("Inside Namespace1");
        }
    }

    namespace namespace2
    {
        class XYZ
        {
            public void fun()
            {
                Console.WriteLine("Inside Namespace2");
            }
        }
    }
}
    
class Program
{
    static void Main()
    {
        ABC OB1 = new ABC();
        XYZ OB2 = new XYZ();

        OB1.fun();
        OB2.fun();

    }
}

Output

    Inside Namespace1
    Inside Namespace2


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.