-
C# Namespace
-
In C#, namespaces are a way to organize code and avoid name conflicts by grouping related classes, interfaces, structs, enums, and delegates under a specific name. For example, there are few built-in namespaces within .NET such as
System
,System.Linq
,System.Web
, etc.The .NET Framework uses namespaces to organize its built-in classes and provides a context for identifiers to avoid ambiguity when similar names are used in different parts of a program.
Key Points about Namespace:
- Purpose: Grouping of related classes and types together.
- Avoid Naming Conflicts: When two or more libraries have the same class name, namespaces helps to distinguish between them.
Syntax:
In C#, a namespace can be defined by using the namespace keyword.
namespace NamespaceName { // Class, interface, struct public class MyClass { public void MyMethod() { Console.WriteLine("Hello from MyClass!"); } } }
Example 1: Basic Namespace Example
using System; namespace MyNamespace { public class Greeting { public void SayHello() { Console.WriteLine("Hello from MyApplication namespace!"); } } } namespace AnotherNamespace { public class Greeting { public void SayHello() { Console.WriteLine("Hello from AnotherNamespace!"); } } } class Program { static void Main() { MyApplication.Greeting myGreeting = new MyApplication.Greeting(); myGreeting.SayHello(); // Outputs: Hello from MyApplication namespace! AnotherNamespace.Greeting anotherGreeting = new AnotherNamespace.Greeting(); anotherGreeting.SayHello(); // Outputs: Hello from AnotherNamespace! } }
In the above example, two Greeting classes are defined in different namespaces (
MyApplication
andAnotherNamespace
). By using namespaces, both classes can co-exist in the same program without causing any conflicts.Example 2: Nested Namespace
C# language also supports nested namespaces, it is a namespace within another namespace.
using System; namespace OuterNamespace { namespace InnerNamespace { public class InnerClass { public void Display() { Console.WriteLine("Inside InnerNamespace"); } } } } class Program { static void Main() { OuterNamespace.InnerNamespace.InnerClass obj = new OuterNamespace.InnerNamespace.InnerClass(); obj.Display(); // Outputs: Inside InnerNamespace } }
Example 3: Using using Directive
Instead of fully qualifying the namespace every time, you can use the using directive to simplify access to the classes in a namespace.
using MyApplication; // Import the namespace class Program { static void Main() { Greeting greeting = new Greeting(); // Now you don't need to write MyApplication.Greeting greeting.SayHello(); } }
Example 4: Aliasing a Namespace
Sometimes, you want to use an alias for a namespace to shorten its name or to avoid naming conflicts.
using SystemCollection = System.Collections; using Program { static void Main() { SystemCollection.ArrayList list = new SystemCollection.ArrayList(); list.Add("Hello, world!"); Console.WriteLine(list[0]); // Outputs: Hello, world! } }
Example 5: Importing Multiple Namespaces
You can also import multiple namespaces to use classes from them without fully qualifying the names or without creating
object
separately.using System; using System.Collections.Generic; class Program { static void Main() { List<string> myList = new List<string>(); myList.Add("Hello"); myList.Add("World"); foreach (var item in myList) { Console.WriteLine (item); } } }
Namespaces in C# are basically used to:
- Organize the code logically.
- Avoid naming conflicts between different
libraries
,classes
or parts of the program. - Group related types together for better readability and maintainability.
By using namespaces, you can structure your code, making it easier to navigate and maintain it, especially in very large applications.