The System.Collections Namespace[1]

[入库:2006年2月23日] [更新:2007年3月24日]

本文简介:

The System.Collections Namespace
Use the System.Collections namespace when you need to create a data
structure that can hold a group of similar objects. The System.Collections
namespace contains all the classes and interfaces needed to define
collections of objects. Some of the classes that you might use most often
are listed in Table 4.4.

Table 4.4. System.Collections Classes Class  Description 
ArrayList  An ArrayList is similar to an array, but the ArrayList class
allows you to add items without managing the size of the list yourself
(much like a VB Collection object). 
CollectionBase  This class is the base for a strongly typed collection,
which is critical for creating collection classes. 
DictionaryBase  This class is the base for a strongly typed collection
utilizing associated keys and values. This is similar to the Dictionary
object found in VBScript and used by many ASP developers. 
SortedList  This class represents a collection of keys and values that you
insert into the list. A SortedList object is always automatically sorted by
the key value. You can access the values with either the key value or an
index number. 


A common use of the System.Collections namespace is to create strongly
typed collections. Consider the following scenario:

Assume you create a class called Customer. The Customer class has a number
of properties, methods, and events. You now want to create several Customer
objects within one structure so you can iterate through the collection and
print each customer to the printer. You could just create a variable of
type Array, ArrayList, or SortedList, but these standard collections can
hold any type of object. In other words, you could add a Customer object as
the first item in the collection, but you could then add an Employee object
as the second item, a String object as the third, and so on. If you try to
iterate over this collection and apply the Update method, it might work on
the Customer and Employee objects, but when your loop attempts to call the
Update method of a String object, it will certainly fail.

To get around this sticky situation, you can create your own class that
looks like a generic collection object but restricts its members to only
one data type. This new class will include the standard methods of the
collection base type, such as the Add and Remove methods, as well as an
Item property, but your class's Add method will only allow you to add
Customer objects.

In Visual Basic 6.0, you could provide this functionality, and you
generally started by creating an empty class and then creating an Add
method, a Remove method, an Item property, a Count property, and so on.
This is easier in .NET, thanks to the base classes in the
System.Collections namespace.

A System.Collections Example
Assume that you need to create a Customer object to hold values from a
Customer table in your database. The structure of the Customer object is
very simple, containing just a CompanyName property. Imagine that you want
to create a collection class named Customers (note the letter s on the end)
to hold a set of Customer objects. You first need to create the Customer
class as shown in the following code snippet:

Public Class Customer
    Public CustomerName As String

End Class

Next, you create your new Customers collection class. Your class must
inherit from the System.Collections.CollectionBase class. (This base class
gives you all the functionality of a normal collection but allows you to
override any of the base methods, such as Add and Item.) Listing 4.3
provides the new class and its overridden Add and Item members:

Listing 4.3 Building Collection Classes Is Easy with the .NET Framework
Class CollectionBase
Public Class Customers
    Inherits System.Collections.CollectionBase

    Public Sub Add(ByVal cust As Customer)
        Me.List.Add(cust)
    End Sub
    Public ReadOnly Property Item( _
     ByVal Index As Integer) As Customer
        Get
            If Index > Count - 1 Or Index < 0 Then
                ' Return error message
            Else
                Return CType( _
       &n

本文关键:The System.Collections Namespace
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top