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.

Note

The System namespace contains all the base types you learned about yesterday, such as Int16, Int32, Decimal, and Short.


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:


Dim rdr as System.Data.SqlClient.SqlDataReader



System.Data.SqlClient.SqlDataReader rdr;

Note

Namespaces are grouped by functionality. Because a namespace may cross over several different physical assemblies, you can't assume that each type within a specific namespace is in the same physical assembly.


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:

  • Namespaces supplied by Microsoft are prefixed with System or Microsoft (for example, Microsoft.VisualBasic and System.IO). The System prefix means the namespace has come from the .NET team at Microsoft, and the Microsoft prefix means the namespace has come from one of the product groups. An example is Microsoft.Office.Excel.

  • Third-party namespaces are prefixed by the name of the company that distributes the namespace; for example, Infragistics.UltraWinGrid.

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.22.161