Introduction to ASP.NET - Namespaces
(Page 4 of 10 )
Namespaces, a key part of the .NET Framework, provide scope to both preinstalled framework classes and custom-developed classes. Namespaces are declared for a given set of classes (types) by enclosing those classes in one of the following declarations:
// C#
namespace myNamespace
{
class myClass
{
// class implementation code
}
}
' VB.NET
Namespace myNamespace
Class myCls
' class implementation code
End Class
End Namespace
Namespaces may also be nested, as shown here:
' VB.NET
Namespace myFirstNamespace
Public Class myCls
' class implementation code
End Class
Namespace mySecondNamespace
Public Class myCls
' class implementation code
End Class
Public Class myCls2
' class implementation code
End Class
End Namespace
End Namespace
This code is perfectly valid because we’ve declared the second myCls in the nested namespace mySecondNamespace. If we tried to declare two identically named classes within the same namespace, we would get a compiler error informing us that there was a naming conflict, because each class name must be unique within its namespace. To use the classes we just declared, we can do something like the following:
' VB.NET
Imports System
Imports myFirstNamespace
Imports myFirstNamespace.mySecondNamespace
Module namespaces_client_vb
Sub Main()
Dim newClass As New myFirstNamespace.myCls
Dim newClass2 As New myCls2 Console.WriteLine("Object creation succeeded!")
End Sub
End Module
We use the Imports keyword in Visual Basic .NET to enable the use of member names from these namespaces without explicitly using the namespace name. However, because we used the class name myCls in both the myFirstNamespace and mySecondNamespace namespaces, we need to use the fully qualified name for this class, while we are able to instantiate myCls2 with only the class name. We can just as easily use these classes from C#, as shown here:
using System;
using myFirstNamespace;
using myFirstNamespace.mySecondNamespace;
class namespaces_client
{
public static void Main()
{
myFirstNamespace.myCls newClass = new myFirstNamespace.myCls();
myCls2 newClass2 = new myCls2();
Console.WriteLine("Object creation succeeded!");
}
}
C# uses the using keyword for importing namespaces. Notice that in both cases, in addition to importing the namespaces we defined, we’ve also imported the System namespace. This is what allows us to use the Console class defined in the System namespace to write to a console window without referring explicitly to System.Console.
Classes that are part of the .NET Framework are organized by functionality into namespaces that make them easier to locate and use. All classes that are a part of the .NET Framework begin with either “System” or “Microsoft.” Examples include:
System -- Contains all the .NET primitive data types as well as utility classes such as Console and Math that are apt to be widely used in .NET applications.
System.Collections -- Contains classes used to implement various kinds of collections in .NET, including ArrayList, Dictionary, and Hashtable.
System.Data -- Contains classes used to access and manipulate data, as well as child namespaces such as System.Data.SqlClient, which contain data access classes specific to a particular data provider.
System.Web -- Contains classes used to process web requests, as well as child namespaces such as System.Web.UI, which contains such classes as the Page class, the basis for all ASP.NET pages.
 | If you've enjoyed what you've seen here, or to get more information, click on the "Buy the book!" graphic. Pick up a copy today!
Visit the O'Reilly Network http://www.oreillynet.com for more online content. |
Next: Assemblies >>
More ASP.NET Articles
More By O'Reilly Media