C# Tutorial

C# String

C# Array

C# Flow Control

C# Class and Object

C# Inheritance

C# Interface

C# Collection

C# Generic

C# File I/O

C# Delegate and Event

C# Exception

C# Process and Thread

C# ADO.NET Database Operations

C# Namespace

Namespaces in C# are used to organize and group related types, such as classes, interfaces, and structs, to avoid naming conflicts and provide better maintainability. In this tutorial, we'll cover the basics of using namespaces in C#:

  • Declaring a namespace
  • Using a namespace
  • Nested namespaces
  • The global namespace
  • The using directive

Let's begin!

  • Declaring a namespace

To declare a namespace, use the namespace keyword followed by the namespace name and a pair of curly braces enclosing the related types:

namespace MyNamespace
{
    public class MyClass
    {
        public void MyMethod()
        {
            Console.WriteLine("Hello from MyNamespace!");
        }
    }
}
  • Using a namespace

To access a type defined in a namespace, you can use the fully qualified name, which is the namespace followed by a dot (.) and the type name:

public static void Main(string[] args)
{
    MyNamespace.MyClass myObject = new MyNamespace.MyClass();
    myObject.MyMethod();
}
  • Nested namespaces

Namespaces can be nested to create a hierarchy. You can declare nested namespaces by separating the levels with a dot (.):

namespace LevelOne
{
    namespace LevelTwo
    {
        public class MyClass
        {
            // ...
        }
    }
}

Or by nesting the namespace declarations:

namespace LevelOne
{
    namespace LevelTwo
    {
        public class MyClass
        {
            // ...
        }
    }
}
  • The global namespace

All types that are not explicitly placed in a namespace are part of the global namespace. To reference a type in the global namespace from within a named namespace, you can use the global keyword:

namespace MyNamespace
{
    public class MyClass
    {
        public void MyMethod()
        {
            global::MyGlobalClass myGlobalObject = new global::MyGlobalClass();
            // ...
        }
    }
}
  • The using directive

The using directive allows you to use types from a namespace without specifying the fully qualified name. Place a using directive at the beginning of your file, followed by the namespace you want to use:

using System;
using MyNamespace;

public static void Main(string[] args)
{
    MyClass myObject = new MyClass(); // No need to write MyNamespace.MyClass
    myObject.MyMethod();
}

That's it! You now have a basic understanding of how to use namespaces in C#. They provide a convenient way to organize and group related types, preventing naming conflicts and improving maintainability.

  1. How to use namespaces in C#

    Namespaces are used to organize code into logical groups. Here's an example:

    namespace MyNamespace
    {
        class MyClass
        {
            // Class members
        }
    }
    
  2. Organizing code with namespaces in C#

    Namespaces help organize code, making it more modular and maintainable.

    namespace Geometry
    {
        class Circle { /* ... */ }
        class Square { /* ... */ }
    }
    
  3. Nested namespaces in C#

    Namespaces can be nested for further organization:

    namespace Company
    {
        namespace Department
        {
            class Employee { /* ... */ }
        }
    }
    
  4. Avoiding naming conflicts with namespaces in C#

    Namespaces help prevent naming conflicts by encapsulating code.

    namespace CompanyA
    {
        class Employee { /* ... */ }
    }
    
    namespace CompanyB
    {
        class Employee { /* ... */ }
    }
    
  5. C# using directive and namespaces

    The using directive simplifies code by eliminating the need to fully qualify types:

    using System;
    
    class Program
    {
        static void Main()
        {
            Console.WriteLine("Hello, C# Namespaces!");
        }
    }
    
  6. Global namespace in C#

    Code without a namespace belongs to the global namespace:

    class MyClass { /* ... */ }
    
  7. Namespace aliases in C#

    Aliases can be used for brevity:

    using MyAlias = MyNamespace.MyClass;
    
    class Program
    {
        static void Main()
        {
            MyAlias myObject = new MyAlias();
        }
    }
    
  8. Namespace and assembly in C#

    Namespaces help organize code within an assembly, which is a compiled unit.

  9. Importing and exporting namespaces in C#

    using directives import namespaces, and public types are exported for use in other assemblies.

  10. Using external namespaces in C#

    Import external namespaces using using:

    using ExternalNamespace;
    
    class Program
    {
        static void Main()
        {
            ExternalClass myObject = new ExternalClass();
        }
    }