A namespace is a method of organizing a group of assemblies, classes, or types. A namespace acts as a container, like a folder, for classes organized into groups usually based on functionality.
To access the built-in input-output (I/O) classes and members, use the System.IO namespace. Or, to access Web-related classes and members, use the System.Web namespace. All C# programs should call the System namespace
Using namespace
Example:
using System;
The using keyword allows us to use the classes of the referred namespace. By using the System namespace we can access all the classes of it.
Keyword ‘using’ only allows accessing the classes in the referred namespace and not its internal or child namespaces. Hence in order to access the classes defined in Data namespace which is a child namespace of System namespace we have to write the following.
using System.Data;
Commonly used namespaces
The following is list of some important and frequently used .NET namespaces:
- System.Collections
- System.Data
- System.Diagnostics
- System.Drawing
- System.IO
- System.Net
- System.Reflection
- System.Runtime
- System.Security
- System.Threading
- System.Web
- System.Windows.Forms
- System.Xml
Creating namespace
A namespace can be created via the Namespace keyword.
Example
namespace Books { { class Authors { { //Do something } }
This is simple namespace example. We can also build hierarchy of namespace.
Example
namespace Books
{
namespace Inventory
{
using System;
class AddInventory
{
public void MyMethod()
{
Console.WriteLine("Adding Inventory via MyMethod!");
}
}
}
}
Anonymous User
16-Mar-2019Very nice article.
Sushant Mishra
14-Jul-2017It was really helpful to read this post.