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
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#:
using
directiveLet's begin!
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!"); } } }
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(); }
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 { // ... } } }
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(); // ... } } }
using
directiveThe 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.
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 } }
Organizing code with namespaces in C#
Namespaces help organize code, making it more modular and maintainable.
namespace Geometry { class Circle { /* ... */ } class Square { /* ... */ } }
Nested namespaces in C#
Namespaces can be nested for further organization:
namespace Company { namespace Department { class Employee { /* ... */ } } }
Avoiding naming conflicts with namespaces in C#
Namespaces help prevent naming conflicts by encapsulating code.
namespace CompanyA { class Employee { /* ... */ } } namespace CompanyB { class Employee { /* ... */ } }
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!"); } }
Global namespace in C#
Code without a namespace belongs to the global namespace:
class MyClass { /* ... */ }
Namespace aliases in C#
Aliases can be used for brevity:
using MyAlias = MyNamespace.MyClass; class Program { static void Main() { MyAlias myObject = new MyAlias(); } }
Namespace and assembly in C#
Namespaces help organize code within an assembly, which is a compiled unit.
Importing and exporting namespaces in C#
using
directives import namespaces, and public types are exported for use in other assemblies.
Using external namespaces in C#
Import external namespaces using using
:
using ExternalNamespace; class Program { static void Main() { ExternalClass myObject = new ExternalClass(); } }