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.
// 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.
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(); } }
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.
Class to Demonstrate Namespace
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 } }
/* * 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(); } } } }
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.
Class to Demonstrate Nested Namespaces
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.
- Get Free Certificate of Merit in C# Programming
- Participate in C# Programming Certification Contest
- Become a Top Ranker in C# Programming
- Take C# Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Practice Computer Science MCQs
- Buy C# Books
- Apply for C# Internship
- Apply for Computer Science Internship
- Practice MCA MCQs