Chapter 7

Arrays, Collections, and Generics

What's in this chapter?

Working with arrays

Iteration (looping)

Working with collections

Generics

Nullable types

Generic collections

Generic methods

Covariance and contravariance

Wrox.com Code Downloads for this Chapter

The wrox.com code downloads for this chapter are found at www.wrox.com/remtitle.cgi?isbn=9781118314456 on the Download Code tab. The code is in the chapter 7 download and individually named according to the code filenames throughout the chapter.

In the beginning there were variables, and they were good. The idea that you map a location in memory to a value was a key to tracking a value. However, most people want to work on data as a set. Taking the concept of a variable holding a value, you've moved to the concept of a variable that could reference an array of values. Arrays improved what developers could build, but they weren't the end of the line.

Over time, certain patterns developed in how arrays were used. Instead of just collecting a set of values, many have looked to use arrays to temporarily store values that were awaiting processing, or to provide sorted collections. Each of these patterns started as a best practice for how to build and manipulate array data or to build custom structures that replicate arrays.

The computing world was very familiar with these concepts—for example, using a linked list to enable more flexibility regarding how data is sorted and retrieved. Patterns such as the stack (first in, last out) or queue (first in, first out) were in fact created as part of the original base Class Libraries. Referred to as collections, they provide a more robust and feature-rich way to manage sets of data than arrays can provide. These were common patterns prior to the introduction of .NET, and .NET provided an implementation for each of these collection types.

However, the common implementation of these collection classes relied on the Object base class. This caused two issues. The first, which is discussed in this chapter, is called boxing. Boxing wasn't a big deal on any given item in a collection, but it caused a slight performance hit; and as your collection grew, it had the potential to impact your application's performance. The second issue was that having collections based only on the type Object went against the best practice of having a strongly typed environment. As soon as you started loading items into a collection, you lost all type checking.

Solving the issues with collections based on the Object type is called generics. Originally introduced as part of .NET 2.0, generics provide a way to create collection classes that are type-safe. The type of value that will be stored in the collection is defined as part of the collection definition. Thus .NET has taken the type-safe but limited capabilities of arrays and combined them with the more powerful collection classes that were object-based to provide a set of collection classes which are type-safe.

This chapter looks at these three related ways to create sets of information. Starting with a discussion of arrays and the looping statements that process them, it next introduces collections and then moves to the use of generics, followed by a walk-through of the syntax for defining your own generic templates. Note that the sample code in this chapter is based on the ProVB2012 project created in Chapter 1. Rather than step through the creation of this project again, this chapter makes reference to it. A copy of all of the code is also available as part of the download for this book.

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

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