Classes and Namespaces


In this chapter, we've been looking at different types of collections. Now, we're going behind the scenes of collections, and we'll be introducing some new terminology that will expand your understanding of what's actually going on.

The array is a special sort of collection that is slightly different from the other types of collection we've been looking at. Arrays are created as instances of the System.Array class. The other collections are members of the System.Collections namespace, and are created as instances of the System.Collections.ArrayList, System.Collections.Hashtable, and System.Collections.SortedList classes. Classes and namespaces are a core part of .NET.

Important

Every class that is part of the .NET Framework is part of a namespace that groups classes of similar types – there are over 60 namespaces grouping the thousands of classes that are part of the .NET Framework.

As you saw in the previous paragraph, we used a period (.) character as a separator for namespaces and classes. The last item in the list is the item that we are considering, for example:

  • System.Array refers to the Array class, which is part of the System namespace.

  • System.Collections is a namespace and refers to the System.Collections namespace.

  • System.Collections.ArrayList refers to the ArrayList class that is part of the System.Collections namespace.

Naturally there is a possibility for confusion, but once you understand the principle, you will quickly learn to identify classes from namespaces. There are far fewer namespaces than classes, and you will gradually get used to them. So, let's explain a bit more about how namespaces and classes work.

The .NET Framework is at the core of every piece of code you write. Classes describe functionality, and when we create a new instance of a class, we have a new object. So, the controls we use on a page are described in general classes, which are a bit like templates. When we actually start to use a control, we use a new instance, or copy, of that template, and we can then work with that copy as an object with the specific functionality (methods, properties, and so on) of the class it was originally based on.

So, when we create a new ArrayList object, for example, we are creating it from the ArrayList class that describes what every ArrayList object can do. There are several thousand classes in .NET, and looking through a long, long list of classes could get confusing, so to categorize all of these classes, they are grouped together into namespaces.

Let's look at some of the namespaces we've already worked with:

Namespace

Description

System

Contains core functionality for .NET, including the Array class, and also includes classes describing the basic data types we met in the previous chapter, such as String, Integer, Boolean, and so on.

System.Collections

Contains the classes that store collections of data, including the ArrayList, Hashtable, and SortedList classes.

System.Drawing

Contains classes for graphical drawing. We met this namespace briefly when we used the Font class earlier in this chapter.

System.Web.UI.WebControls

Contains the classes for all of the web controls we've used, including the Button, the TextBox, and the Label controls.

If you have the .NET documentation installed, or if you search MSDN online for the reference information on the .NET Framework (via the following URL: http://msdn.microsoft.com), you can see the full list of namespaces available to all .NET programmers:

click to expand

When we meet the chapters on database access (Chapter 8 onwards), we'll learn more about classes contained within the System.Data namespace. We'll also meet many other classes as we progress through the book.

Note

We can even create our own classes, each with their own methods, properties, and so on, which is a topic a bit beyond the scope of this book. For more information, you can refer to Beginning ASP.NET 1.0 with Visual Basic .NET, Wrox Press, ISBN: 1-86100-733-7.




Beginning Dynamic Websites with ASP. NET Web Matrix
Beginning Dynamic Websites: with ASP.NET Web Matrix (Programmer to Programmer)
ISBN: 0764543741
EAN: 2147483647
Year: 2003
Pages: 141

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net