Chapter 10. Collections and Lists
Object-oriented programming (OOP) has been successful because it gives programmers a way to model physical reality in code. The easiest systems to understand are those that effectively model a familiar reality. If you're trying to represent a person in real-life, for example, you might create a class Person
. After you create a Person
class, what's the next most obvious thing for that person to do? Well, have a party and congregate with other persons, of course! As soon as you have more than one Person
, you need a place to put them all—that's where lists, arrays, hash tables, and other collections come in.
This chapter explains the collections made available to you in the .NET Framework 2.0. Although the concept of collections is not specific to ASP.NET, this chapter shows you how to use them in the context of an ASP.NET 2.0 application. It also looks at the differences between strongly typed collections and generics, as well as exploring the unusual Microsoft.VisualBasic.Collection
class and contrasting it with the System.Collections
namespace.
Arrays
Most folks would say that the simplest collection of objects is an array. Create some objects and put them into an array. Start with a basic Person
class with a simple constructor that initializes the Person
's first and surname (last name), as well as a public property that returns the full name (see Listing 10-1).
Example 10.1. A simple Person class
VB
Public Class Person Dim FirstName As String Dim LastName ...
Get Professional ASP.NET 2.0 Special Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.