Nested Namespace in C#

In this article, we will find out about Nested namespace in C#, how to execute a C# program by using settled namespaces in C#?

Namespaces can likewise be settled in C#. It implies we can characterize namespace inside another namespace.

Syntax:

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

We can get to a settled 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

Leave a Comment

error: Alert: Content is protected!!