In C#, namespace (Namespace) is a way to organize a large number of classes, structures, enumerations, interfaces, delegates, and other namespaces together, which helps to avoid naming conflicts and facilitates code organization and manage.
In the .NET Framework, namespaces are organized in a hierarchical fashion. For example, System.Collections.Generic
a namespace represents such a hierarchical structure: the top level is System
a namespace, which contains a Collections
sub-namespace named , which Collections
in turn contains a sub-namespace Generic
named . In Generic
the namespace, we can find commonly used collection classes such as List<T>
, and so on.Dictionary<TKey, TValue>
In C# code, we use using
keywords to introduce namespaces, so that the types in the namespace can be used directly in the file without fully qualified names. for example:
using System;
using System.Collections.Generic;
public class Example
{
public void PrintNumbers(List<int> numbers)
{
foreach (var number in numbers)
{
Console.WriteLine(number);
}
}
}
In this example, we introduced System
and System.Collections.Generic
two namespaces, so we can directly use Console
and List<T>
these two types without writing System.Console
and System.Collections.Generic.List<T>
.