Sams Teach Yourself Visual Studio .NET 2003 in 21 Days

Sams Teach Yourself .Net in 21 Days

By Jason Beres

What Are Namespaces?

Namespaces are groups of classes, structures, interfaces, enumerations, and delegates, organized in a logical hierarchy by function, that enable you to access the core functionality you need in your applications.

Each namespace in the FCL can contain multiple namespaces, or they can contain classes that expose properties and methods that you call in your applications. The namespaces within the FCL are grouped by the functionality they provide, which makes it very easy to find what you're looking for. It can be compared to a phone book, in which you look under B for bugs, and are led to exterminators, which helps you find all the information you need to squash all the bugs. Similarly, you look for common words in what a namespace offers, and you'll most likely find the information you need to implement the task you're trying to accomplish.

To separate the top-level namespace from the second-level namespace from the class to the actual property or method, the FCL uses the . (period) notation to differentiate the hierarchy. For example, if you want to work with data, and you're using SQL Server, you'll find the System.Data.SqlClient namespace. In the System.Data.SqlClient namespace are the SqlDataReader class and the SqlDataAdapter class, among many others. When you reference these object in code, you use the following syntax:

vbnet_icon.gif
Dim rdr as System.Data.SqlClient.SqlDataReader

c_icon.gif
System.Data.SqlClient.SqlDataReader rdr;

All through Week 1, you used the System.Web namespace, the System.Web.UI namespace, the System.Windows.Forms namespace, and the System.Windows.Forms.Design namespace. Each namespace gave you all the base functionality you needed to create ASP.NET and Windows Forms applications. Each time you referenced a form object, either on the Windows Form or the Web Form, you were referencing a type in one of the namespaces I just mentioned. So, you're using namespaces and their types to accomplish everything you need to do when developing .NET applications.

There are a couple things to note about cthe naming conventions you use when working with namespaces:

When you get to Day 14, "Components and .NET," you learn how to create your own namespaces when writing components. You also see how you can use your own naming convention to avoid name collision with other namespaces in the FCL or third parties.

Share ThisShare This

Informit Network