Namespaces in C#

Namespaces in C# are containers that organize code elements, like classes and methods, into distinct groups. They prevent naming conflicts and make code more maintainable and readable.

Syntax of Namespaces:
// Namespace definition
namespace MyNamespace
{
    // Code elements (classes, structs, etc.) go here
    // e.g., class MyClass { ... }
}
 
// Using the namespace
using MyNamespace;
 
// Accessing types from a namespace without importing it
MyNamespace.TypeName myObject = new MyNamespace.TypeName();

In C#, you define a namespace using “namespace” followed by its name, containing related code elements. The “using” directive allows easy access to types without full names, and direct access to namespace types is possible without importing everything.

Implementation of Namespaces:

Here is source code of the C# Program to Implement Namespaces. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Implement Namespaces
 */
 
using System;
namespace Sanfoundry.Csharp.Codes
{
    class TestClass
    {
        public TestClass()
        {
            Console.WriteLine("Class to Demonstrate Namespace");
        }
    }
}
class MyClient
{
    public static void Main()
    {
      Sanfoundry.Csharp.Codes.TestClass mc = new Sanfoundry.Csharp.Codes.TestClass();
      Console.ReadLine();
    }
}
Program Explanation

1. This C# program demonstrates namespaces by defining a class TestClass in the namespace Sanfoundry.Csharp.Codes.
2. The Main method creates an object of TestClass and displays a message before waiting for user input.

advertisement
advertisement
Program Output
Class to Demonstrate Namespace
Nested Namespaces in C#

In C#, nested namespaces help organize code by creating a hierarchical structure within existing namespaces. This improves categorization of related code elements and enhances separation between different components, especially in larger projects.

Syntax of Nested Namespaces:

namespace ParentNamespace
{
    // Code elements go here
 
    namespace ChildNamespace
    {
        // Code elements go here
    }
}
Program/Source Code:
/*
 * C# Program to Implement Nested Namespaces
 */
 
using System;
 
namespace Sanfoundry.Csharp.Codes
{
    class TestClass
    {
        public TestClass()
        {
            Console.WriteLine("Class to Demonstrate Nested Namespaces");
        }
    }
 
    namespace MyClientApp
    {
        class Program
        {
            static void Main()
            {
                Sanfoundry.Csharp.Codes.TestClass mc = new Sanfoundry.Csharp.Codes.TestClass();
                Console.ReadLine();
            }
        }
    }
}
Program Explanation

1. The code demonstrates nested namespaces. It contains the Sanfoundry.Csharp.Codes namespace with the TestClass class and a nested MyClientApp namespace with the Program class.
2. The Main method creates an object of TestClass and prints a message. This structure helps organize code elements and maintain clarity within namespaces.

advertisement
Program Output
Class to Demonstrate Nested Namespaces
How to prevent naming conflicts in C#?

To prevent naming conflicts in C#, you can use namespaces. Namespaces help organize code elements like classes and methods, ensuring they remain separate and do not interfere with each other, creating a more organized codebase.

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

advertisement
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.